Skip to content

Commit bd0d0cc

Browse files
Tinchehynek
andauthored
Modernize docs some more (#885)
* Modernize docs some more * Fix doctest * Tweak docs * Update docs/examples.rst Co-authored-by: Hynek Schlawack <[email protected]>
1 parent 3833e4f commit bd0d0cc

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

docs/examples.rst

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ For that, `attr.asdict` offers a callback that decides whether an attribute shou
199199

200200
.. doctest::
201201

202-
>>> from typing import List
203202
>>> from attr import asdict
204203

205204
>>> @define
@@ -209,7 +208,7 @@ For that, `attr.asdict` offers a callback that decides whether an attribute shou
209208

210209
>>> @define
211210
... class UserList:
212-
... users: List[User]
211+
... users: list[User]
213212

214213
>>> asdict(UserList([User("[email protected]", "s33kred"),
215214
... User("[email protected]", "p4ssw0rd")]),
@@ -503,12 +502,12 @@ If you don't mind annotating *all* attributes, you can even drop the `field` and
503502
>>> @define
504503
... class AutoC:
505504
... cls_var: typing.ClassVar[int] = 5 # this one is ignored
506-
... l: typing.List[int] = Factory(list)
505+
... l: list[int] = Factory(list)
507506
... x: int = 1
508507
... foo: str = "every attrib needs a type if auto_attribs=True"
509508
... bar: typing.Any = None
510509
>>> fields(AutoC).l.type
511-
typing.List[int]
510+
list[int]
512511
>>> fields(AutoC).x.type
513512
<class 'int'>
514513
>>> fields(AutoC).foo.type
@@ -522,35 +521,39 @@ If you don't mind annotating *all* attributes, you can even drop the `field` and
522521

523522
The generated ``__init__`` method will have an attribute called ``__annotations__`` that contains this type information.
524523

525-
If your annotations contain strings (e.g. forward references),
524+
If your annotations contain forward references,
526525
you can resolve these after all references have been defined by using :func:`attr.resolve_types`.
527526
This will replace the *type* attribute in the respective fields.
528527

529528
.. doctest::
530529

531-
>>> import typing
532530
>>> from attr import fields, resolve_types
533531

534532
>>> @define
535533
... class A:
536-
... a: typing.List['A']
534+
... a: 'list[A]'
537535
... b: 'B'
538536
...
539537
>>> @define
540538
... class B:
541539
... a: A
542540
...
543541
>>> fields(A).a.type
544-
typing.List[ForwardRef('A')]
542+
'list[A]'
545543
>>> fields(A).b.type
546544
'B'
547545
>>> resolve_types(A, globals(), locals())
548546
<class 'A'>
549547
>>> fields(A).a.type
550-
typing.List[A]
548+
list[A]
551549
>>> fields(A).b.type
552550
<class 'B'>
553551

552+
.. note::
553+
554+
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']``).
555+
This is a limitation of the Python typing system.
556+
554557
.. warning::
555558

556559
``attrs`` itself doesn't have any features that work on top of type metadata *yet*.

docs/types.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ That means that on modern Python versions, the declaration part of the example f
1010
.. doctest::
1111

1212
>>> import attr
13-
>>> import typing
1413

1514
>>> @attr.s(auto_attribs=True)
1615
... class SomeClass:
1716
... a_number: int = 42
18-
... list_of_numbers: typing.List[int] = attr.Factory(list)
17+
... list_of_numbers: list[int] = attr.Factory(list)
1918

2019
>>> sc = SomeClass(1, [1, 2, 3])
2120
>>> sc
@@ -71,7 +70,7 @@ To mypy, this code is equivalent to the one above:
7170
@attr.s
7271
class SomeClass(object):
7372
a_number = attr.ib(default=42) # type: int
74-
list_of_numbers = attr.ib(factory=list, type=typing.List[int])
73+
list_of_numbers = attr.ib(factory=list, type=list[int])
7574
7675
7776
pyright
@@ -86,7 +85,7 @@ Given the following definition, ``pyright`` will generate static type signatures
8685
@attr.define
8786
class SomeClass:
8887
a_number: int = 42
89-
list_of_numbers: typing.List[int] = attr.field(factory=list)
88+
list_of_numbers: list[int] = attr.field(factory=list)
9089

9190
.. warning::
9291

0 commit comments

Comments
 (0)