-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathtest_alias_validation.py
More file actions
110 lines (89 loc) · 2.81 KB
/
Copy pathtest_alias_validation.py
File metadata and controls
110 lines (89 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import unicodedata
import pytest
import attr
class TestAliasValidation:
def test_invalid_identifier(self):
"""
Invalid identifiers are rejected.
"""
with pytest.raises(
TypeError, match="Invalid initialization alias '1x'"
):
@attr.s
class C:
x = attr.ib(alias="1x")
def test_keyword_alias(self):
"""
Keywords are rejected.
"""
with pytest.raises(
TypeError, match="Invalid initialization alias 'class'"
):
@attr.s
class C:
x = attr.ib(alias="class")
def test_self_shadowing(self):
"""
'self' shadowing is rejected.
"""
with pytest.raises(TypeError, match="shadows the 'self' parameter"):
@attr.s
class C:
x = attr.ib(alias="self")
def test_unicode_normalization_collision(self):
"""
Aliases that collide after NFKC normalization are rejected.
"""
omega = "\u03a9"
ohm = "\u2126"
assert omega != ohm
assert unicodedata.normalize("NFKC", omega) == unicodedata.normalize(
"NFKC", ohm
)
with pytest.raises(
TypeError, match="collides with another attribute's alias"
):
@attr.s
class C:
x = attr.ib(alias=omega)
y = attr.ib(alias=ohm)
def test_make_class_normalization_collision(self):
"""
make_class also respects alias normalization collision checks.
"""
omega = "\u03a9"
ohm = "\u2126"
with pytest.raises(
TypeError, match="collides with another attribute's alias"
):
attr.make_class("C", {omega: attr.ib(), ohm: attr.ib()})
def test_non_string_alias(self):
"""
Non-string aliases are rejected.
"""
with pytest.raises(TypeError, match="Invalid initialization alias 1"):
@attr.s
class C:
x = attr.ib(alias=1)
def test_valid_unicode_aliases(self):
"""
Valid Unicode identifiers that don't collide are allowed.
"""
# We use make_class to avoid non-ASCII characters in the source code,
# which satisfies linters.
pi = "\u03c0"
alpha = "\u03b1"
C = attr.make_class("C", {pi: attr.ib(), alpha: attr.ib(alias="beta")})
inst = C(3.14, beta=1)
assert getattr(inst, pi) == 3.14
assert getattr(inst, alpha) == 1
def test_init_false_skipped(self):
"""
Validation is skipped if init=False.
"""
@attr.s
class C:
x = attr.ib(init=False, alias="not an identifier!")
inst = C()
inst.x = 42
assert inst.x == 42