You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/examples.md
+10-12Lines changed: 10 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -408,18 +408,16 @@ Therefore if you use `@validator`, it is *not* enough to annotate said attribute
408
408
409
409
*attrs* ships with a bunch of validators, make sure to [check them out](api-validators) before writing your own:
410
410
411
-
```{eval-rst}
412
-
.. doctest::
413
-
414
-
>>> @define
415
-
... class C:
416
-
... x: int = field(validator=validators.instance_of(int))
417
-
>>> C(42)
418
-
C(x=42)
419
-
>>> C("42")
420
-
Traceback (most recent call last):
421
-
...
422
-
TypeError: ("'x' must be <type 'int'> (got '42' that is a <type 'str'>).", Attribute(name='x', default=NOTHING, factory=NOTHING, validator=<instance_of validator for type <type 'int'>>, type=None, kw_only=False), <type 'int'>, '42')
411
+
```{doctest}
412
+
>>> @define
413
+
... class C:
414
+
... x: int = field(validator=validators.instance_of(int))
415
+
>>> C(42)
416
+
C(x=42)
417
+
>>> C("42")
418
+
Traceback (most recent call last):
419
+
...
420
+
TypeError: ("'x' must be <type 'int'> (got '42' that is a <type 'str'>).", Attribute(name='x', default=NOTHING, factory=NOTHING, validator=<instance_of validator for type <type 'int'>>, type=None, kw_only=False), <type 'int'>, '42')
423
421
```
424
422
425
423
Please note that if you use {func}`attr.s` (and **not** {func}`attrs.define`) to define your class, validators only run on initialization by default -- not when you set an attribute.
Copy file name to clipboardExpand all lines: docs/how-does-it-work.md
+10-12Lines changed: 10 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,18 +20,16 @@ While creating new classes is more elegant, we've run into several edge cases su
20
20
21
21
To be very clear: if you define a class with a single attribute without a default value, the generated `__init__` will look *exactly* how you'd expect:
22
22
23
-
```{eval-rst}
24
-
.. doctest::
25
-
26
-
>>> import inspect
27
-
>>> from attr import define
28
-
>>> @define
29
-
... class C:
30
-
... x: int
31
-
>>> print(inspect.getsource(C.__init__))
32
-
def __init__(self, x):
33
-
self.x = x
34
-
<BLANKLINE>
23
+
```{doctest}
24
+
>>> import inspect
25
+
>>> from attrs import define
26
+
>>> @define
27
+
... class C:
28
+
... x: int
29
+
>>> print(inspect.getsource(C.__init__))
30
+
def __init__(self, x):
31
+
self.x = x
32
+
<BLANKLINE>
35
33
```
36
34
37
35
No magic, no meta programming, no expensive introspection at runtime.
Copy file name to clipboardExpand all lines: docs/types.md
+9-11Lines changed: 9 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,19 +4,17 @@
4
4
5
5
However they will forever remain *optional*, therefore the example from the README could also be written as:
6
6
7
-
```{eval-rst}
8
-
.. doctest::
7
+
```{doctest}
8
+
>>> from attrs import define, field
9
9
10
-
>>> from attrs import define, field
10
+
>>> @define
11
+
... class SomeClass:
12
+
... a_number = field(default=42)
13
+
... list_of_numbers = field(factory=list)
11
14
12
-
>>> @define
13
-
... class SomeClass:
14
-
... a_number = field(default=42)
15
-
... list_of_numbers = field(factory=list)
16
-
17
-
>>> sc = SomeClass(1, [1, 2, 3])
18
-
>>> sc
19
-
SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
15
+
>>> sc = SomeClass(1, [1, 2, 3])
16
+
>>> sc
17
+
SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
20
18
```
21
19
22
20
You can choose freely between the approaches, but please remember that if you choose to use type annotations, you **must** annotate **all** attributes!
0 commit comments