Skip to content

Commit

Permalink
Allow tzinfo objects for Timezone (#56)
Browse files Browse the repository at this point in the history
* Allow `tzinfo` objects for `Timezone`

* Add section about changes
  • Loading branch information
Viicos authored Nov 2, 2023
1 parent 18584df commit 657ded9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,14 @@ See [issue #23](https://github.com/annotated-types/annotated-types/issues/23) fo
are allowed. `Annotated[datetime, Timezone(None)]` must be a naive datetime.
`Timezone[...]` ([literal ellipsis](https://docs.python.org/3/library/constants.html#Ellipsis))
expresses that any timezone-aware datetime is allowed. You may also pass a specific
timezone string or `timezone` object such as `Timezone(timezone.utc)` or
`Timezone("Africa/Abidjan")` to express that you only allow a specific timezone,
though we note that this is often a symptom of fragile design.
timezone string or [`tzinfo`](https://docs.python.org/3/library/datetime.html#tzinfo-objects)
object such as `Timezone(timezone.utc)` or `Timezone("Africa/Abidjan")` to express that you only
allow a specific timezone, though we note that this is often a symptom of fragile design.

#### Changed in v0.x.x

* `Timezone` accepts [`tzinfo`](https://docs.python.org/3/library/datetime.html#tzinfo-objects) objects instead of
`timezone`, extending compatibility to [`zoneinfo`](https://docs.python.org/3/library/zoneinfo.html) and third party libraries.

### Predicate

Expand Down
6 changes: 3 additions & 3 deletions annotated_types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import math
import sys
from dataclasses import dataclass
from datetime import timezone
from datetime import tzinfo
from typing import TYPE_CHECKING, Any, Callable, Iterator, Optional, SupportsFloat, SupportsIndex, TypeVar, Union

if sys.version_info < (3, 8):
Expand Down Expand Up @@ -286,13 +286,13 @@ class Timezone(BaseMetadata):
``Timezone[...]`` (the ellipsis literal) expresses that the datetime must be
tz-aware but any timezone is allowed.
You may also pass a specific timezone string or timezone object such as
You may also pass a specific timezone string or tzinfo object such as
``Timezone(timezone.utc)`` or ``Timezone("Africa/Abidjan")`` to express that
you only allow a specific timezone, though we note that this is often
a symptom of poor design.
"""

tz: Union[str, timezone, EllipsisType, None]
tz: Union[str, tzinfo, EllipsisType, None]


@dataclass(frozen=True, **SLOTS)
Expand Down

0 comments on commit 657ded9

Please sign in to comment.