File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ ` attrs.make_class() ` now allows for Unicode class names.
Original file line number Diff line number Diff line change 13
13
import sys
14
14
import types
15
15
import typing
16
+ import unicodedata
16
17
17
18
from operator import itemgetter
18
19
@@ -2907,7 +2908,11 @@ def make_class(
2907
2908
.. versionadded:: 17.1.0 *bases*
2908
2909
.. versionchanged:: 18.1.0 If *attrs* is ordered, the order is retained.
2909
2910
.. versionchanged:: 23.2.0 *class_body*
2911
+ .. versionchanged:: 25.2.0 Class names can now be unicode.
2910
2912
"""
2913
+ # Class identifiers are converted into the normal form NFKC while parsing
2914
+ name = unicodedata .normalize ("NFKC" , name )
2915
+
2911
2916
if isinstance (attrs , dict ):
2912
2917
cls_dict = attrs
2913
2918
elif isinstance (attrs , (list , tuple )):
Original file line number Diff line number Diff line change 10
10
import inspect
11
11
import itertools
12
12
import sys
13
+ import unicodedata
13
14
14
15
from operator import attrgetter
15
16
from typing import Generic , TypeVar
@@ -1100,6 +1101,48 @@ def test_attr_args(self):
1100
1101
1101
1102
assert repr (C (1 )).startswith ("<tests.test_make.C object at 0x" )
1102
1103
1104
+ def test_normalized_unicode_attr_args (self ):
1105
+ """
1106
+ Unicode identifiers are valid in Python.
1107
+ """
1108
+ clsname = "ü"
1109
+
1110
+ assert clsname == unicodedata .normalize ("NFKC" , clsname )
1111
+
1112
+ attrname = "ß"
1113
+
1114
+ assert attrname == unicodedata .normalize ("NFKC" , attrname )
1115
+
1116
+ C = make_class (clsname , [attrname ], repr = False )
1117
+
1118
+ assert repr (C (1 )).startswith ("<tests.test_make.ü object at 0x" )
1119
+
1120
+ kwargs = {"ß" : 1 }
1121
+ c = C (** kwargs )
1122
+
1123
+ assert 1 == c .ß
1124
+
1125
+ def test_unnormalized_unicode_attr_args (self ):
1126
+ """
1127
+ Unicode identifiers are normalized to NFKC form in Python.
1128
+ """
1129
+
1130
+ clsname = "Ŀ"
1131
+
1132
+ assert clsname != unicodedata .normalize ("NFKC" , clsname )
1133
+
1134
+ attrname = "ㅁ"
1135
+
1136
+ assert attrname != unicodedata .normalize ("NFKC" , attrname )
1137
+
1138
+ C = make_class (clsname , [attrname ], repr = False )
1139
+ assert repr (C (1 )).startswith ("<tests.test_make.L· object at 0x" )
1140
+
1141
+ kwargs = {unicodedata .normalize ("NFKC" , attrname ): 1 }
1142
+ c = C (** kwargs )
1143
+
1144
+ assert 1 == c .ㅁ
1145
+
1103
1146
def test_catches_wrong_attrs_type (self ):
1104
1147
"""
1105
1148
Raise `TypeError` if an invalid type for attrs is passed.
You can’t perform that action at this time.
0 commit comments