@@ -133,8 +133,8 @@ def __eq__(self):
133
133
self .assertEqual (hash (C (10 )), hash ((10 ,)))
134
134
135
135
# Creating this class should generate an exception, because
136
- # __hash__ exists and is not None, which it would be if it had
137
- # been auto-generated do due __eq__ being defined.
136
+ # __hash__ exists and is not None, which it would be if it
137
+ # had been auto-generated due to __eq__ being defined.
138
138
with self .assertRaisesRegex (TypeError ,
139
139
'Cannot overwrite attribute __hash__' ):
140
140
@dataclass (unsafe_hash = True )
@@ -145,7 +145,6 @@ def __eq__(self):
145
145
def __hash__ (self ):
146
146
pass
147
147
148
-
149
148
def test_overwrite_fields_in_derived_class (self ):
150
149
# Note that x from C1 replaces x in Base, but the order remains
151
150
# the same as defined in Base.
@@ -624,7 +623,7 @@ class C:
624
623
self .assertIs (o1 .x , o2 .x )
625
624
626
625
def test_no_options (self ):
627
- # call with dataclass()
626
+ # Call with dataclass().
628
627
@dataclass ()
629
628
class C :
630
629
x : int
@@ -639,7 +638,7 @@ class Point:
639
638
y : int
640
639
self .assertNotEqual (Point (1 , 2 ), (1 , 2 ))
641
640
642
- # And that we can't compare to another unrelated dataclass
641
+ # And that we can't compare to another unrelated dataclass.
643
642
@dataclass
644
643
class C :
645
644
x : int
@@ -664,7 +663,7 @@ class Date:
664
663
self .assertNotEqual (Point3D (2017 , 6 , 3 ), Date (2017 , 6 , 3 ))
665
664
self .assertNotEqual (Point3D (1 , 2 , 3 ), (1 , 2 , 3 ))
666
665
667
- # Make sure we can't unpack
666
+ # Make sure we can't unpack.
668
667
with self .assertRaisesRegex (TypeError , 'unpack' ):
669
668
x , y , z = Point3D (4 , 5 , 6 )
670
669
@@ -695,7 +694,7 @@ def validate_class(cls):
695
694
# Verify __init__.
696
695
697
696
signature = inspect .signature (cls .__init__ )
698
- # Check the return type, should be None
697
+ # Check the return type, should be None.
699
698
self .assertIs (signature .return_annotation , None )
700
699
701
700
# Check each parameter.
@@ -716,12 +715,12 @@ def validate_class(cls):
716
715
param = next (params )
717
716
self .assertEqual (param .name , 'k' )
718
717
self .assertIs (param .annotation , F )
719
- # Don't test for the default, since it's set to MISSING
718
+ # Don't test for the default, since it's set to MISSING.
720
719
self .assertEqual (param .kind , inspect .Parameter .POSITIONAL_OR_KEYWORD )
721
720
param = next (params )
722
721
self .assertEqual (param .name , 'l' )
723
722
self .assertIs (param .annotation , float )
724
- # Don't test for the default, since it's set to MISSING
723
+ # Don't test for the default, since it's set to MISSING.
725
724
self .assertEqual (param .kind , inspect .Parameter .POSITIONAL_OR_KEYWORD )
726
725
self .assertRaises (StopIteration , next , params )
727
726
@@ -867,7 +866,7 @@ def __post_init__(self):
867
866
868
867
self .assertEqual (C ().x , 5 )
869
868
870
- # Now call super(), and it will raise
869
+ # Now call super(), and it will raise.
871
870
@dataclass
872
871
class C (B ):
873
872
def __post_init__ (self ):
@@ -928,8 +927,8 @@ class C:
928
927
929
928
c = C (5 )
930
929
self .assertEqual (repr (c ), 'TestCase.test_class_var.<locals>.C(x=5, y=10)' )
931
- self .assertEqual (len (fields (C )), 2 ) # We have 2 fields
932
- self .assertEqual (len (C .__annotations__ ), 5 ) # And 3 ClassVars
930
+ self .assertEqual (len (fields (C )), 2 ) # We have 2 fields.
931
+ self .assertEqual (len (C .__annotations__ ), 5 ) # And 3 ClassVars.
933
932
self .assertEqual (c .z , 1000 )
934
933
self .assertEqual (c .w , 2000 )
935
934
self .assertEqual (c .t , 3000 )
@@ -1205,14 +1204,13 @@ class D(C):
1205
1204
d = D (4 , 5 )
1206
1205
self .assertEqual ((d .x , d .z ), (4 , 5 ))
1207
1206
1208
-
1209
- def x_test_classvar_default_factory (self ):
1210
- # XXX: it's an error for a ClassVar to have a factory function
1211
- @dataclass
1212
- class C :
1213
- x : ClassVar [int ] = field (default_factory = int )
1214
-
1215
- self .assertIs (C ().x , int )
1207
+ def test_classvar_default_factory (self ):
1208
+ # It's an error for a ClassVar to have a factory function.
1209
+ with self .assertRaisesRegex (TypeError ,
1210
+ 'cannot have a default factory' ):
1211
+ @dataclass
1212
+ class C :
1213
+ x : ClassVar [int ] = field (default_factory = int )
1216
1214
1217
1215
def test_is_dataclass (self ):
1218
1216
class NotDataClass :
@@ -1264,7 +1262,7 @@ class C: pass
1264
1262
fields (C ())
1265
1263
1266
1264
def test_helper_asdict (self ):
1267
- # Basic tests for asdict(), it should return a new dictionary
1265
+ # Basic tests for asdict(), it should return a new dictionary.
1268
1266
@dataclass
1269
1267
class C :
1270
1268
x : int
@@ -1279,7 +1277,7 @@ class C:
1279
1277
self .assertIs (type (asdict (c )), dict )
1280
1278
1281
1279
def test_helper_asdict_raises_on_classes (self ):
1282
- # asdict() should raise on a class object
1280
+ # asdict() should raise on a class object.
1283
1281
@dataclass
1284
1282
class C :
1285
1283
x : int
@@ -1377,7 +1375,7 @@ class C:
1377
1375
self .assertIs (type (d ), OrderedDict )
1378
1376
1379
1377
def test_helper_astuple (self ):
1380
- # Basic tests for astuple(), it should return a new tuple
1378
+ # Basic tests for astuple(), it should return a new tuple.
1381
1379
@dataclass
1382
1380
class C :
1383
1381
x : int
@@ -1392,7 +1390,7 @@ class C:
1392
1390
self .assertIs (type (astuple (c )), tuple )
1393
1391
1394
1392
def test_helper_astuple_raises_on_classes (self ):
1395
- # astuple() should raise on a class object
1393
+ # astuple() should raise on a class object.
1396
1394
@dataclass
1397
1395
class C :
1398
1396
x : int
@@ -1489,7 +1487,7 @@ def nt(lst):
1489
1487
self .assertIs (type (t ), NT )
1490
1488
1491
1489
def test_dynamic_class_creation (self ):
1492
- cls_dict = {'__annotations__' : {'x' :int , 'y' :int },
1490
+ cls_dict = {'__annotations__' : {'x' : int , 'y' : int },
1493
1491
}
1494
1492
1495
1493
# Create the class.
@@ -1502,7 +1500,7 @@ def test_dynamic_class_creation(self):
1502
1500
self .assertEqual (asdict (cls (1 , 2 )), {'x' : 1 , 'y' : 2 })
1503
1501
1504
1502
def test_dynamic_class_creation_using_field (self ):
1505
- cls_dict = {'__annotations__' : {'x' :int , 'y' :int },
1503
+ cls_dict = {'__annotations__' : {'x' : int , 'y' : int },
1506
1504
'y' : field (default = 5 ),
1507
1505
}
1508
1506
@@ -1569,8 +1567,8 @@ class C:
1569
1567
1570
1568
def test_alternate_classmethod_constructor (self ):
1571
1569
# Since __post_init__ can't take params, use a classmethod
1572
- # alternate constructor. This is mostly an example to show how
1573
- # to use this technique.
1570
+ # alternate constructor. This is mostly an example to show
1571
+ # how to use this technique.
1574
1572
@dataclass
1575
1573
class C :
1576
1574
x : int
@@ -1604,7 +1602,7 @@ def test_field_metadata_mapping(self):
1604
1602
class C :
1605
1603
i : int = field (metadata = 0 )
1606
1604
1607
- # Make sure an empty dict works
1605
+ # Make sure an empty dict works.
1608
1606
@dataclass
1609
1607
class C :
1610
1608
i : int = field (metadata = {})
@@ -1666,7 +1664,7 @@ class LabeledBox(Generic[T]):
1666
1664
self .assertEqual (box .content , 42 )
1667
1665
self .assertEqual (box .label , '<unknown>' )
1668
1666
1669
- # subscripting the resulting class should work, etc.
1667
+ # Subscripting the resulting class should work, etc.
1670
1668
Alias = List [LabeledBox [int ]]
1671
1669
1672
1670
def test_generic_extending (self ):
@@ -1931,7 +1929,7 @@ class B:
1931
1929
with self .assertRaisesRegex (TypeError ,
1932
1930
"'f' is a field but has no type annotation" ):
1933
1931
# This is still an error: make sure we don't pick up the
1934
- # type annotation in the base class.
1932
+ # type annotation in the base class.
1935
1933
@dataclass
1936
1934
class C (B ):
1937
1935
f = field ()
@@ -1944,7 +1942,7 @@ class B:
1944
1942
with self .assertRaisesRegex (TypeError ,
1945
1943
"'f' is a field but has no type annotation" ):
1946
1944
# This is still an error: make sure we don't pick up the
1947
- # type annotation in the base class.
1945
+ # type annotation in the base class.
1948
1946
@dataclass
1949
1947
class C (B ):
1950
1948
f = field ()
@@ -2178,7 +2176,7 @@ def __repr__(self):
2178
2176
2179
2177
class TestFrozen (unittest .TestCase ):
2180
2178
def test_overwriting_frozen (self ):
2181
- # frozen uses __setattr__ and __delattr__
2179
+ # frozen uses __setattr__ and __delattr__.
2182
2180
with self .assertRaisesRegex (TypeError ,
2183
2181
'Cannot overwrite attribute __setattr__' ):
2184
2182
@dataclass (frozen = True )
@@ -2473,16 +2471,16 @@ class C:
2473
2471
2474
2472
def test_hash_no_args (self ):
2475
2473
# Test dataclasses with no hash= argument. This exists to
2476
- # make sure that if the @dataclass parameter name is changed
2477
- # or the non-default hashing behavior changes, the default
2478
- # hashability keeps working the same way.
2474
+ # make sure that if the @dataclass parameter name is changed
2475
+ # or the non-default hashing behavior changes, the default
2476
+ # hashability keeps working the same way.
2479
2477
2480
2478
class Base :
2481
2479
def __hash__ (self ):
2482
2480
return 301
2483
2481
2484
2482
# If frozen or eq is None, then use the default value (do not
2485
- # specify any value in the decorator).
2483
+ # specify any value in the decorator).
2486
2484
for frozen , eq , base , expected in [
2487
2485
(None , None , object , 'unhashable' ),
2488
2486
(None , None , Base , 'unhashable' ),
@@ -2534,9 +2532,9 @@ class C(base):
2534
2532
2535
2533
elif expected == 'object' :
2536
2534
# I'm not sure what test to use here. object's
2537
- # hash isn't based on id(), so calling hash()
2538
- # won't tell us much. So, just check the function
2539
- # used is object's.
2535
+ # hash isn't based on id(), so calling hash()
2536
+ # won't tell us much. So, just check the
2537
+ # function used is object's.
2540
2538
self .assertIs (C .__hash__ , object .__hash__ )
2541
2539
2542
2540
elif expected == 'tuple' :
@@ -2665,8 +2663,9 @@ class C:
2665
2663
__slots__ = ('x' ,)
2666
2664
x : Any
2667
2665
2668
- # There was a bug where a variable in a slot was assumed
2669
- # to also have a default value (of type types.MemberDescriptorType).
2666
+ # There was a bug where a variable in a slot was assumed to
2667
+ # also have a default value (of type
2668
+ # types.MemberDescriptorType).
2670
2669
with self .assertRaisesRegex (TypeError ,
2671
2670
r"__init__\(\) missing 1 required positional argument: 'x'" ):
2672
2671
C ()
0 commit comments