-
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
15 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,7 +199,6 @@ For that, `attr.asdict` offers a callback that decides whether an attribute shou | |
|
||
.. doctest:: | ||
|
||
>>> from typing import List | ||
>>> from attr import asdict | ||
|
||
>>> @define | ||
|
@@ -209,7 +208,7 @@ For that, `attr.asdict` offers a callback that decides whether an attribute shou | |
|
||
>>> @define | ||
... class UserList: | ||
... users: List[User] | ||
... users: list[User] | ||
|
||
>>> asdict(UserList([User("[email protected]", "s33kred"), | ||
... User("[email protected]", "p4ssw0rd")]), | ||
|
@@ -503,12 +502,12 @@ If you don't mind annotating *all* attributes, you can even drop the `field` and | |
>>> @define | ||
... class AutoC: | ||
... cls_var: typing.ClassVar[int] = 5 # this one is ignored | ||
... l: typing.List[int] = Factory(list) | ||
... l: list[int] = Factory(list) | ||
... x: int = 1 | ||
... foo: str = "every attrib needs a type if auto_attribs=True" | ||
... bar: typing.Any = None | ||
>>> fields(AutoC).l.type | ||
typing.List[int] | ||
list[int] | ||
>>> fields(AutoC).x.type | ||
<class 'int'> | ||
>>> fields(AutoC).foo.type | ||
|
@@ -522,35 +521,39 @@ If you don't mind annotating *all* attributes, you can even drop the `field` and | |
|
||
The generated ``__init__`` method will have an attribute called ``__annotations__`` that contains this type information. | ||
|
||
If your annotations contain strings (e.g. forward references), | ||
If your annotations contain forward references, | ||
you can resolve these after all references have been defined by using :func:`attr.resolve_types`. | ||
This will replace the *type* attribute in the respective fields. | ||
|
||
.. doctest:: | ||
|
||
>>> import typing | ||
>>> from attr import fields, resolve_types | ||
|
||
>>> @define | ||
... class A: | ||
... a: typing.List['A'] | ||
... a: 'list[A]' | ||
... b: 'B' | ||
... | ||
>>> @define | ||
... class B: | ||
... a: A | ||
... | ||
>>> fields(A).a.type | ||
typing.List[ForwardRef('A')] | ||
'list[A]' | ||
>>> fields(A).b.type | ||
'B' | ||
>>> resolve_types(A, globals(), locals()) | ||
<class 'A'> | ||
>>> fields(A).a.type | ||
typing.List[A] | ||
list[A] | ||
>>> fields(A).b.type | ||
<class 'B'> | ||
|
||
.. note:: | ||
|
||
If you find yourself using string type annotations to handle forward references, wrap the entire type annotation in quotes instead of only the type you need a forward reference to (so ``'list[A]'`` instead of ``list['A']``). | ||
This is a limitation of the Python typing system. | ||
|
||
.. warning:: | ||
|
||
``attrs`` itself doesn't have any features that work on top of type metadata *yet*. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters