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/init.md
+6-11Lines changed: 6 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,7 +90,8 @@ Even if you're not using this feature, it's important to be aware of it because
90
90
... _1: int
91
91
Traceback (most recent call last):
92
92
...
93
-
SyntaxError: invalid syntax
93
+
TypeError: Invalid initialization alias '1' for attribute '_1'. Aliases must be valid Python identifiers.
94
+
94
95
```
95
96
96
97
In this case a valid attribute name `_1` got transformed into an invalid argument name `1`.
@@ -251,10 +252,7 @@ ValueError: 'x' has to be smaller than 'y'!
251
252
... x = field(validator=attrs.validators.instance_of(int))
252
253
>>> C(42)
253
254
C(x=42)
254
-
>>> C("42")
255
-
Traceback (most recent call last):
256
-
...
257
-
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), <type 'int'>, '42')
255
+
TypeError: ("'x' must be <class 'int'> (got '42' that is a <class 'str'>).", Attribute(name='x', ..., alias='x'), <class 'int'>, '42')
258
256
```
259
257
260
258
Of course you can mix and match the two approaches at your convenience.
@@ -270,10 +268,7 @@ If you use both ways to define validators for an attribute, they are both ran:
270
268
... raise ValueError("value out of bounds")
271
269
>>> C(128)
272
270
C(x=128)
273
-
>>> C("128")
274
-
Traceback (most recent call last):
275
-
...
276
-
TypeError: ("'x' must be <class 'int'> (got '128' that is a <class 'str'>).", Attribute(name='x', default=NOTHING, validator=[<instance_of validator for type <class 'int'>>, <function fits_byte at 0x10fd7a0d0>], repr=True, cmp=True, hash=True, init=True, metadata=mappingproxy({}), type=None, converter=None), <class 'int'>, '128')
271
+
TypeError: ("'x' must be <class 'int'> (got '128' that is a <class 'str'>).", Attribute(name='x', ..., alias='x'), <class 'int'>, '128')
277
272
>>> C(256)
278
273
Traceback (most recent call last):
279
274
...
@@ -290,7 +285,7 @@ C(x='128')
290
285
>>> C("128")
291
286
Traceback (most recent call last):
292
287
...
293
-
TypeError: ("'x' must be <class 'int'> (got '128' that is a <class 'str'>).", Attribute(name='x', default=NOTHING, validator=[<instance_of validator for type <class 'int'>>, <function fits_byte at 0x10fd7a0d0>], repr=True, cmp=True, hash=True, init=True, metadata=mappingproxy({}), type=None, converter=None), <class 'int'>, '128')
288
+
TypeError: ("'x' must be <class 'int'> (got '128' that is a <class 'str'>).", Attribute(name='x', ..., alias='x'), <class 'int'>, '128')
294
289
```
295
290
296
291
... or within a context manager:
@@ -302,7 +297,7 @@ C(x='128')
302
297
>>> C("128")
303
298
Traceback (most recent call last):
304
299
...
305
-
TypeError: ("'x' must be <class 'int'> (got '128' that is a <class 'str'>).", Attribute(name='x', default=NOTHING, validator=[<instance_of validator for type <class 'int'>>, <function fits_byte at 0x10fd7a0d0>], repr=True, cmp=True, hash=True, init=True, metadata=mappingproxy({}), type=None, converter=None), <class 'int'>, '128')
300
+
TypeError: ("'x' must be <class 'int'> (got '128' that is a <class 'str'>).", Attribute(name='x', ..., alias='x'), <class 'int'>, '128')
0 commit comments