Skip to content

Commit 1090044

Browse files
committed
updated
1 parent 9dbc137 commit 1090044

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

docs/init.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Even if you're not using this feature, it's important to be aware of it because
9090
... _1: int
9191
Traceback (most recent call last):
9292
...
93-
SyntaxError: invalid syntax
93+
TypeError: Invalid initialization alias '1' for attribute '_1'. Aliases must be valid Python identifiers.
9494
```
9595

9696
In this case a valid attribute name `_1` got transformed into an invalid argument name `1`.
@@ -254,7 +254,7 @@ C(x=42)
254254
>>> C("42")
255255
Traceback (most recent call last):
256256
...
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')
257+
TypeError: ("'x' must be <class 'int'> (got '42' that is a <class 'str'>).", Attribute(name='x', ..., alias='x'), <class 'int'>, '42')
258258
```
259259

260260
Of course you can mix and match the two approaches at your convenience.
@@ -273,7 +273,7 @@ C(x=128)
273273
>>> C("128")
274274
Traceback (most recent call last):
275275
...
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')
276+
TypeError: ("'x' must be <class 'int'> (got '128' that is a <class 'str'>).", Attribute(name='x', ..., alias='x'), <class 'int'>, '128')
277277
>>> C(256)
278278
Traceback (most recent call last):
279279
...

tests/test_alias_validation.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ def test_valid_unicode_aliases(self):
8686
"""
8787
Valid Unicode identifiers that don't collide are allowed.
8888
"""
89-
90-
@attr.s
91-
class C:
92-
π = attr.ib()
93-
α = attr.ib(alias="beta")
94-
95-
inst = C(π=3.14, beta=1)
96-
assert inst.π == 3.14
97-
assert inst.α == 1
89+
# We use make_class to avoid non-ASCII characters in the source code,
90+
# which satisfies linters.
91+
pi = "\u03c0"
92+
alpha = "\u03b1"
93+
C = attr.make_class("C", {pi: attr.ib(), alpha: attr.ib(alias="beta")})
94+
95+
inst = C(3.14, beta=1)
96+
assert getattr(inst, pi) == 3.14
97+
assert getattr(inst, alpha) == 1
9898

9999
def test_init_false_skipped(self):
100100
"""

0 commit comments

Comments
 (0)