deliberately use from dataclasses import every name in all is tested so they all must be present this is a way to catch missing ones just any custom exception we can catch even though this is testing an internal implementation detail it s testing a feature we want to make sure is correctly implemented for the sake of dataclasses itself even though this is testing an internal implementation detail it s testing a feature we want to make sure is correctly implemented for the sake of dataclasses itself nondefaults following defaults a derived class adds a nondefault field after a default one override a base class field and add a default to a field which didn t use to have a default test that declaring this class isn t an error it should use the userprovided hash test that declaring this class isn t an error it should use the generated hash but this one should generate an exception because with unsafehashtrue it s an error to have a hash defined creating this class should not generate an exception because even though hash exists before dataclass is called due to eq being defined since it s none that s okay the generated hash function works as we d expect creating this class should generate an exception because hash exists and is not none which it would be if it had been autogenerated due to eq being defined note that x from c1 replaces x in base but the order remains the same as defined in base make sure the first parameter is not named self but we do use self if no field named self make sure the first parameter is named self gh96151 gh98886 attribute names can shadow builtin names since code generation is used ensure that this is not happening attribute names can shadow builtin names since code generation is used ensure that this is not happening for frozen data classes ensure that orderfalse is the default ensure that orderfalse is the default ensure that orderfalse is the default comparisons fail for subclasses even if no fields are added test combining eq and order if we delete the instance attribute we should then see the class attribute see bpo44674 make sure that the machinery looking for hashability is using the class s hash not the instance s hash this shouldn t make the variable hashable test all 6 cases of hashtruefalsenone comparetruefalse hash contains the field the field is not present in the hash if initfalse and no default value then the field won t be present in the instance thefields is a tuple of 3 items each value is in annotations we only have a class attribute if a default value is specified either directly or via a field with a default for the known types don t allow mutable default values can t use a zerolength value nor a nonzerolength value check subtypes also fail because this is a classvar it can be mutable because this is a classvar it can be mutable if a mutable default isn t in the known list of list dict set then it s okay these 2 instances will share this value of x call with dataclass make sure we can t be compared to a tuple and that we can t compare to another unrelated dataclass test that some of the problems with namedtuple don t happen here make sure we can t unpack make sure another class with the same field names isn t equal some dummy class and instance to use as a default first check annotations even though they re not function annotations verify init check the return type should be none check each parameter this is testing an internal name and probably shouldn t be tested don t test for the default since it s set to missing don t test for the default since it s set to missing now repeat with hash test that missing works the same as a default not being specified test that missing works the same as a default factory not being specified which is really the same as a default not being specified too just make sure it gets called postinit gets called but doesn t raise this is just checking that self is used correctly if there s not an init then postinit won t get called creating the class won t raise make sure that if we re frozen postinit can t set attributes make sure super postinit isn t called by default now call super and it will raise make sure postinit is called even if not defined in our class see bpo46757 which had proposed always adding postinit as raymond hettinger pointed out that would be a breaking change so add a test to make sure that the current behavior doesn t change since a0 has no postinit and one wasn t automatically added because that s the rule it s never added by dataclass it s only the class that can add it then b0 postinit is called verify that now the same thing except a1 defines postinit this time b1 postinit isn t being called this mimics what would happen if a1 postinit had been automatically added instead of manually added as we see here this test isn t really needed but i m including it just to demonstrate the changed behavior when a1 does define postinit make sure classvars are ignored in init repr etc if a classvar has no default value it should not be set on the class it makes no sense for a classvar to have a default factory when would it be called call it yourself since it s classwide if a classvar has a default value it should be set on the class make sure classvars work even if we re frozen we can still modify the classvar it s only instances that are frozen if an initvar has no default value it should not be set on the class it makes no sense for an initvar to have a default factory when would it be called call it yourself since it s classwide if an initvar has a default value it should be set on the class make sure the repr is correct note that this deliberately tests that a dataclass need not have a postinit function if it has an initvar field it could just be used in a derived class as shown here we can instantiate by passing the initvar even though it s not used test a factory that returns a new list test a factory that returns a shared list test various other field flags repr hash init see also testdefaultfactorywithnoinit compare we need a factory with a side effect make sure the default factory is called for each new instance we need a factory that we can test if it s been called make sure that if a field has a default factory function it s not called if a value is specified see bpo32896 test that an intermediate class that defines annotations does not define fields y was not initialized and if we again derive a nondataclass no fields are added it s an error for a classvar to have a factory function see bpo37868 also test for an instance attribute indirect tests for isdataclassinstance check that we can call fields on either a class or instance and get back the same thing check that typeerror is raised if not passed a dataclass or instance basic tests for asdict it should return a new dictionary asdict should raise on a class object now with a dictfactory ordereddict is convenient but since it compares to dicts we also need to have separate assertis tests make sure that the returned dicts are actually ordereddicts ensure that a field that contains a dict which has a namedtuple as a key works with asdict make sure that t has been copied not used directly ensure asdict does not throw exceptions when a defaultdict is a member of a dataclass basic tests for astuple it should return a new tuple astuple should raise on a class object now using a tuplefactory list is convenient here ensure astuple does not throw exceptions when a defaultdict is a member of a dataclass create the class make it a dataclass create the class make it a dataclass class dict instance dict since postinit can t take params use a classmethod alternate constructor this is mostly an example to show how to use this technique in a real example create a new instance and populate x from contents of a file make sure the default metadata is readonly and of zero length make sure only a mapping can be passed as metadata zero length make sure an empty dict works update should work see bpo35960 make sure a nonempty dict works update should work nonexistent key try a custom mapping make sure we re still talking to our custom mapping subscripting the resulting class should work etc check that type aliases work correctly check mro resolution this is still an error make sure we don t pick up the type annotation in the base class same test but with the base class not a dataclass this is still an error make sure we don t pick up the type annotation in the base class because 3 6 and 3 7 differ in how inspect signature work see bpo 32108 for the time being just compare them with whitespace stripped lorem ipsum x int self assertequalc doc lorem ipsum def testdocstringnofieldsself dataclass class c pass self assertdocstrequalc doc c def testdocstringonefieldself dataclass class c x int self assertdocstrequalc doc cx int def testdocstringtwofieldsself dataclass class c x int y int self assertdocstrequalc doc cx int y int def testdocstringthreefieldsself dataclass class c x int y int z str self assertdocstrequalc doc cx int y int z str def testdocstringonefieldwithdefaultself dataclass class c x int 3 self assertdocstrequalc doc cx int3 def testdocstringonefieldwithdefaultnoneself dataclass class c x unionint typenone none self assertdocstrequalc doc cx optionalintnone def testdocstringlistfieldself dataclass class c x listint self assertdocstrequalc doc cx listint def testdocstringlistfieldwithdefaultfactoryself dataclass class c x listint fielddefaultfactorylist self assertdocstrequalc doc cx listintfactory def testdocstringdequefieldself dataclass class c x deque self assertdocstrequalc doc cx collections deque def testdocstringdequefieldwithdefaultfactoryself dataclass class c x deque fielddefaultfactorydeque self assertdocstrequalc doc cx collections dequefactory def testdocstringwithnosignatureself see https github compythoncpythonissues103449 class metatype call dict class basemetaclassmeta pass dataclass class cbase pass self assertdocstrequalc doc c class testinitunittest testcase def testbasehasinitself class b def initself self z 100 make sure that declaring this class doesn t raise an error the issue is that we can t override init in our class but it should be okay to add init to us if our base has an init dataclass class cb x int 0 c c10 self assertequalc x 10 self assertnotin z varsc make sure that if we don t add an init the base init gets called dataclassinitfalse class cb x int 10 c c self assertequalc x 10 self assertequalc z 100 def testnoinitself dataclassinitfalse class c i int 0 self assertequalc i 0 dataclassinitfalse class c i int 2 def initself self i 3 self assertequalc i 3 def testoverwritinginitself if the class has init use it no matter the value of init dataclass class c x int def initself x self x 2 x self assertequalc3 x 6 dataclassinittrue class c x int def initself x self x 2 x self assertequalc4 x 8 dataclassinitfalse class c x int def initself x self x 2 x self assertequalc5 x 10 def testinheritfromprotocolself dataclasses inheriting from protocol should preserve their own init see bpo45081 class pprotocol a int dataclass class cp a int self assertequalc5 a 5 dataclass class dp def initself a self a a 2 self assertequald5 a 10 class testreprunittest testcase def testreprself dataclass class b x int dataclass class cb y int 10 o c4 self assertequalrepro testrepr testrepr locals cx4 y10 dataclass class dc x int 20 self assertequalreprd testrepr testrepr locals dx20 y10 dataclass class c dataclass class d i int dataclass class e pass self assertequalreprc d0 testrepr testrepr locals c di0 self assertequalreprc e testrepr testrepr locals c e def testnoreprself test a class with no repr and reprfalse dataclassreprfalse class c x int self assertinf name testrepr testnorepr locals c object at reprc3 test a class with a repr and reprfalse dataclassreprfalse class c x int def reprself return cclass self assertequalreprc3 cclass def testoverwritingreprself if the class has repr use it no matter the value of repr dataclass class c x int def reprself return x self assertequalreprc0 x dataclassreprtrue class c x int def reprself return x self assertequalreprc0 x dataclassreprfalse class c x int def reprself return x self assertequalreprc0 x class testequnittest testcase def testnoeqself test a class with no eq and eqfalse dataclasseqfalse class c x int self assertnotequalc0 c0 c c3 self assertequalc c test a class with an eq and eqfalse dataclasseqfalse class c x int def eqself other return other 10 self assertequalc3 10 def testoverwritingeqself if the class has eq use it no matter the value of eq dataclass class c x int def eqself other return other 3 self assertequalc1 3 self assertnotequalc1 1 dataclasseqtrue class c x int def eqself other return other 4 self assertequalc1 4 self assertnotequalc1 1 dataclasseqfalse class c x int def eqself other return other 5 self assertequalc1 5 self assertnotequalc1 1 class testorderingunittest testcase def testfunctoolstotalorderingself test that functools totalordering works with this class totalordering dataclass class c x int def ltself other perform the test backward just to make sure this is being called return self x other self assertlessc0 1 self assertlessequalc0 1 self assertgreaterc0 1 self assertgreaterequalc0 1 def testnoorderself test that no ordering functions are added by default dataclassorderfalse class c x int make sure no order methods are added self assertnotin le c dict self assertnotin lt c dict self assertnotin ge c dict self assertnotin gt c dict test that lt is still called dataclassorderfalse class c x int def ltself other return false make sure other methods aren t added self assertnotin le c dict self assertnotin ge c dict self assertnotin gt c dict def testoverwritingorderself with self assertraisesregextypeerror cannot overwrite attribute lt using functools totalordering dataclassordertrue class c x int def ltself pass with self assertraisesregextypeerror cannot overwrite attribute le using functools totalordering dataclassordertrue class c x int def leself pass with self assertraisesregextypeerror cannot overwrite attribute gt using functools totalordering dataclassordertrue class c x int def gtself pass with self assertraisesregextypeerror cannot overwrite attribute ge using functools totalordering dataclassordertrue class c x int def geself pass class testhashunittest testcase def testunsafehashself dataclassunsafehashtrue class c x int y str self assertequalhashc1 foo hash1 foo def testhashrulesself def nonboolvalue map to something else that s true but not a bool if value is none return none if value return 3 return 0 def testcase unsafehash eq frozen withhash result with self subtestcasecase unsafehashunsafehash eqeq frozenfrozen if result exception if withhash dataclassunsafehashunsafehash eqeq frozenfrozen class c def hashself return 0 else dataclassunsafehashunsafehash eqeq frozenfrozen class c pass see if the result matches what s expected if result fn hash contains the function we generated self assertin hash c dict self assertisnotnonec dict hash elif result hash is not present in our class if not withhash self assertnotin hash c dict elif result none hash is set to none self assertin hash c dict self assertisnonec dict hash elif result exception creating the class should cause an exception this only happens with withhashtrue assertwithhash with self assertraisesregextypeerror cannot overwrite attribute hash dataclassunsafehashunsafehash eqeq frozenfrozen class c def hashself return 0 else assert false f unknown result result r there are 8 cases of unsafehashtruefalse eqtruefalse frozentruefalse and for each of these a different result if hash is defined or not for case unsafehash eq frozen resnodefinedhash resdefinedhash in enumerate false false false false false true false true false none false true true fn true false false fn exception true false true fn exception true true false fn exception true true true fn exception 1 testcase unsafehash eq frozen false resnodefinedhash testcase unsafehash eq frozen true resdefinedhash test nonbool truth values too this is just to make sure the datadriven table in the decorator handles nonbool values testcase nonboolunsafehash nonbooleq nonboolfrozen false resnodefinedhash testcase nonboolunsafehash nonbooleq nonboolfrozen true resdefinedhash def testeqonlyself if a class defines eq hash is automatically added and set to none this is normal python behavior not related to dataclasses make sure we don t interfere with that see bpo32546 dataclass class c i int def eqself other return self i other i self assertequalc1 c1 self assertnotequalc1 c4 and make sure things work in this case if we specify unsafehashtrue dataclassunsafehashtrue class c i int def eqself other return self i other i self assertequalc1 c1 0 self assertequalhashc1 hashc1 0 and check that the classes eq is being used despite specifying eqtrue dataclassunsafehashtrue eqtrue class c i int def eqself other return self i 3 and self i other i self assertequalc3 c3 self assertnotequalc1 c1 self assertequalhashc1 hashc1 0 def test0fieldhashself dataclassfrozentrue class c pass self assertequalhashc hash dataclassunsafehashtrue class c pass self assertequalhashc hash def test1fieldhashself dataclassfrozentrue class c x int self assertequalhashc4 hash4 self assertequalhashc42 hash42 dataclassunsafehashtrue class c x int self assertequalhashc4 hash4 self assertequalhashc42 hash42 def testhashnoargsself test dataclasses with no hash argument this exists to make sure that if the dataclass parameter name is changed or the nondefault hashing behavior changes the default hashability keeps working the same way class base def hashself return 301 if frozen or eq is none then use the default value do not specify any value in the decorator for frozen eq base expected in none none object unhashable none none base unhashable none false object object none false base base none true object unhashable none true base unhashable false none object unhashable false none base unhashable false false object object false false base base false true object unhashable false true base unhashable true none object tuple true none base tuple true false object object true false base base true true object tuple true true base tuple with self subtestfrozenfrozen eqeq basebase expectedexpected first create the class if frozen is none and eq is none dataclass class cbase i int elif frozen is none dataclasseqeq class cbase i int elif eq is none dataclassfrozenfrozen class cbase i int else dataclassfrozenfrozen eqeq class cbase i int now make sure it hashes as expected if expected unhashable c c10 with self assertraisesregextypeerror unhashable type hashc elif expected base self assertequalhashc10 301 elif expected object i m not sure what test to use here object s hash isn t based on id so calling hash won t tell us much so just check the function used is object s self assertisc hash object hash elif expected tuple self assertequalhashc42 hash42 else assert false f unknown value for expectedexpected r class testfrozenunittest testcase def testfrozenself dataclassfrozentrue class c i int c c10 self assertequalc i 10 with self assertraisesfrozeninstanceerror c i 5 self assertequalc i 10 def testfrozenemptyself dataclassfrozentrue class c pass c c self assertfalsehasattrc i with self assertraisesfrozeninstanceerror c i 5 self assertfalsehasattrc i with self assertraisesfrozeninstanceerror del c i def testinheritself dataclassfrozentrue class c i int dataclassfrozentrue class dc j int d d0 10 with self assertraisesfrozeninstanceerror d i 5 with self assertraisesfrozeninstanceerror d j 6 self assertequald i 0 self assertequald j 10 def testinheritnonfrozenfromemptyfrozenself dataclassfrozentrue class c pass with self assertraisesregextypeerror cannot inherit nonfrozen dataclass from a frozen one dataclass class dc j int def testinheritfrozenmutlipleinheritanceself dataclass class notfrozen pass dataclassfrozentrue class frozen pass class notdataclass pass for bases in notfrozen frozen frozen notfrozen frozen notdataclass notdataclass frozen with self subtestbasesbases with self assertraisesregex typeerror cannot inherit nonfrozen dataclass from a frozen one dataclass class notfrozenchildbases pass for bases in notfrozen frozen frozen notfrozen notfrozen notdataclass notdataclass notfrozen with self subtestbasesbases with self assertraisesregex typeerror cannot inherit frozen dataclass from a nonfrozen one dataclassfrozentrue class frozenchildbases pass def testinheritfrozenmutlipleinheritanceregularmixinsself dataclassfrozentrue class frozen pass class notdataclass pass class c1frozen notdataclass pass self assertequalc1 mro c1 frozen notdataclass object class c2notdataclass frozen pass self assertequalc2 mro c2 notdataclass frozen object dataclassfrozentrue class c3frozen notdataclass pass self assertequalc3 mro c3 frozen notdataclass object dataclassfrozentrue class c4notdataclass frozen pass self assertequalc4 mro c4 notdataclass frozen object def testmultiplefrozendataclassesinheritanceself dataclassfrozentrue class frozena pass dataclassfrozentrue class frozenb pass class c1frozena frozenb pass self assertequalc1 mro c1 frozena frozenb object class c2frozenb frozena pass self assertequalc2 mro c2 frozenb frozena object dataclassfrozentrue class c3frozena frozenb pass self assertequalc3 mro c3 frozena frozenb object dataclassfrozentrue class c4frozenb frozena pass self assertequalc4 mro c4 frozenb frozena object def testinheritnonfrozenfromemptyself dataclass class c pass dataclass class dc j int d d3 self assertequald j 3 self assertisinstanced c test both ways with an intermediate normal nondataclass class and without an intermediate class def testinheritnonfrozenfromfrozenself for intermediateclass in true false with self subtestintermediateclassintermediateclass dataclassfrozentrue class c i int if intermediateclass class ic pass else i c with self assertraisesregextypeerror cannot inherit nonfrozen dataclass from a frozen one dataclass class di pass def testinheritfrozenfromnonfrozenself for intermediateclass in true false with self subtestintermediateclassintermediateclass dataclass class c i int if intermediateclass class ic pass else i c with self assertraisesregextypeerror cannot inherit frozen dataclass from a nonfrozen one dataclassfrozentrue class di pass def testinheritfromnormalclassself for intermediateclass in true false with self subtestintermediateclassintermediateclass class c pass if intermediateclass class ic pass else i c dataclassfrozentrue class di i int d d10 with self assertraisesfrozeninstanceerror d i 5 def testnonfrozennormalderivedself see bpo32953 dataclassfrozentrue class d x int y int 10 class sd pass s s3 self assertequals x 3 self assertequals y 10 s cached true but can t change the frozen attributes with self assertraisesfrozeninstanceerror s x 5 with self assertraisesfrozeninstanceerror s y 5 self assertequals x 3 self assertequals y 10 self assertequals cached true with self assertraisesfrozeninstanceerror del s x self assertequals x 3 with self assertraisesfrozeninstanceerror del s y self assertequals y 10 del s cached self assertfalsehasattrs cached with self assertraisesattributeerror as cm del s cached self assertnotisinstancecm exception frozeninstanceerror def testnonfrozennormalderivedfromemptyfrozenself dataclassfrozentrue class d pass class sd pass s s self assertfalsehasattrs x s x 5 self assertequals x 5 del s x self assertfalsehasattrs x with self assertraisesattributeerror as cm del s x self assertnotisinstancecm exception frozeninstanceerror def testoverwritingfrozenself frozen uses setattr and delattr with self assertraisesregextypeerror cannot overwrite attribute setattr dataclassfrozentrue class c x int def setattrself pass with self assertraisesregextypeerror cannot overwrite attribute delattr dataclassfrozentrue class c x int def delattrself pass dataclassfrozenfalse class c x int def setattrself name value self dict x value 2 self assertequalc10 x 20 def testfrozenhashself dataclassfrozentrue class c x any if x is immutable we can compute the hash no exception is raised hashc3 if x is mutable computing the hash is an error with self assertraisesregextypeerror unhashable type hashc class testslotsunittest testcase def testsimpleself dataclass class c slots x x any there was a bug where a variable in a slot was assumed to also have a default value of type types memberdescriptortype with self assertraisesregextypeerror rinit missing 1 required positional argument x c we can create an instance and assign to x c c10 self assertequalc x 10 c x 5 self assertequalc x 5 we can t assign to anything else with self assertraisesregexattributeerror c object has no attribute y c y 5 def testderivedaddedfieldself see bpo33100 dataclass class base slots x x any dataclass class derivedbase x int y int d derived1 2 self assertequald x d y 1 2 we can add a new field to the derived instance d z 10 def testgeneratedslotsself dataclassslotstrue class c x int y int c c1 2 self assertequalc x c y 1 2 c x 3 c y 4 self assertequalc x c y 3 4 with self assertraisesregexattributeerror c object has no attribute z c z 5 def testaddslotswhenslotsexistsself with self assertraisesregextypeerror c already specifies slots dataclassslotstrue class c slots x x int def testgeneratedslotsvalueself class root slots x class root2root slots k j class root3root2 slots h class root4root3 slots aa dataclassslotstrue class baseroot4 y int j str h str self assertequalbase slots y dataclassslotstrue class derivedbase aa float x str z int k str h str self assertequalderived slots z dataclass class anotherderivedbase z int self assertnotin slots anotherderived dict def testcantinheritfromiteratorslotsself class root slots iter a class root2root slots b with self assertraisesregex typeerror slots of root cannot be determined dataclassslotstrue class croot2 x int def testreturnsnewclassself class a x int b dataclassa slotstrue self assertisnota b self assertfalsehasattra slots self asserttruehasattrb slots can t be local to testfrozenpickle dataclassfrozentrue slotstrue class frozenslotsclass foo str bar int dataclassfrozentrue class frozenwithoutslotsclass foo str bar int def testfrozenpickleself bpo43999 self assertequalself frozenslotsclass slots foo bar for proto in rangepickle highestprotocol 1 with self subtestprotoproto obj self frozenslotsclassa 1 p pickle loadspickle dumpsobj protocolproto self assertisnotobj p self assertequalobj p obj self frozenwithoutslotsclassa 1 p pickle loadspickle dumpsobj protocolproto self assertisnotobj p self assertequalobj p dataclassfrozentrue slotstrue class frozenslotsgetstateclass foo str bar int getstatecalled bool fielddefaultfalse comparefalse def getstateself object setattrself getstatecalled true return self foo self bar dataclassfrozentrue slotstrue class frozenslotssetstateclass foo str bar int setstatecalled bool fielddefaultfalse comparefalse def setstateself state object setattrself setstatecalled true object setattrself foo state0 object setattrself bar state1 dataclassfrozentrue slotstrue class frozenslotsallstateclass foo str bar int getstatecalled bool fielddefaultfalse comparefalse setstatecalled bool fielddefaultfalse comparefalse def getstateself object setattrself getstatecalled true return self foo self bar def setstateself state object setattrself setstatecalled true object setattrself foo state0 object setattrself bar state1 def testfrozenslotspicklecustomstateself for proto in rangepickle highestprotocol 1 with self subtestprotoproto obj self frozenslotsgetstateclass a 1 dumped pickle dumpsobj protocolproto self asserttrueobj getstatecalled self assertequalobj pickle loadsdumped for proto in rangepickle highestprotocol 1 with self subtestprotoproto obj self frozenslotssetstateclass a 1 obj2 pickle loadspickle dumpsobj protocolproto self asserttrueobj2 setstatecalled self assertequalobj obj2 for proto in rangepickle highestprotocol 1 with self subtestprotoproto obj self frozenslotsallstateclass a 1 dumped pickle dumpsobj protocolproto self asserttrueobj getstatecalled obj2 pickle loadsdumped self asserttrueobj2 setstatecalled self assertequalobj obj2 def testslotswithdefaultnoinitself originally reported in bpo44649 dataclassslotstrue class a a str b str fielddefault b initfalse obj aa self assertequalobj a a self assertequalobj b b def testslotswithdefaultfactorynoinitself originally reported in bpo44649 dataclassslotstrue class a a str b str fielddefaultfactorylambda b initfalse obj aa self assertequalobj a a self assertequalobj b b def testslotsnoweakrefself dataclassslotstrue class a no weakref pass self assertnotinweakref a slots a a with self assertraisesregextypeerror cannot create weak reference weakref refa with self assertraisesattributeerror a weakref def testslotsweakrefself dataclassslotstrue weakrefslottrue class a a int self assertinweakref a slots a a1 aref weakref refa self assertisa weakref aref def testslotsweakrefbasestrself class base slots weakref dataclassslotstrue class abase a int weakref is in the base class not a but an a is still weakrefable self assertinweakref base slots self assertnotinweakref a slots a a1 weakref refa def testslotsweakrefbasetupleself same as testslotsweakrefbase but use a tuple instead of a string in the base class class base slots weakref dataclassslotstrue class abase a int weakref is in the base class not a but an a is still weakrefable self assertinweakref base slots self assertnotinweakref a slots a a1 weakref refa def testweakrefslotwithoutslotself with self assertraisesregextypeerror weakrefslot is true but slots is false dataclassweakrefslottrue class a a int def testweakrefslotmakedataclassself a makedataclass a a int slotstrue weakrefslottrue self assertinweakref a slots a a1 weakref refa and make sure if raises if slotstrue is not given with self assertraisesregextypeerror weakrefslot is true but slots is false b makedataclass b a int weakrefslottrue def testweakrefslotsubclassweakrefslotself dataclassslotstrue weakrefslottrue class base field int a can also specify weakrefslottrue if it wants to gh93521 dataclassslotstrue weakrefslottrue class abase weakref is in the base class not a but an instance of a is still weakrefable self assertinweakref base slots self assertnotinweakref a slots a a1 aref weakref refa self assertisa weakref aref def testweakrefslotsubclassnoweakrefslotself dataclassslotstrue weakrefslottrue class base field int dataclassslotstrue class abase weakref is in the base class not a even though a doesn t specify weakrefslot it should still be weakrefable self assertinweakref base slots self assertnotinweakref a slots a a1 aref weakref refa self assertisa weakref aref def testweakrefslotnormalbaseweakrefslotself class base slots weakref dataclassslotstrue weakrefslottrue class abase field int weakref is in the base class not a but an instance of a is still weakrefable self assertinweakref base slots self assertnotinweakref a slots a a1 aref weakref refa self assertisa weakref aref class testdescriptorsunittest testcase def testsetnameself see bpo33141 create a descriptor class d def setnameself owner name self name name x def getself instance owner if instance is not none return 1 return self this is the case of just normal descriptor behavior no dataclass code is involved in initializing the descriptor dataclass class c c intd self assertequalc c name cx now test with a default value and initfalse which is the only time this is really meaningful if not using initfalse then the descriptor will be overwritten anyway dataclass class c c intfielddefaultd initfalse self assertequalc c name cx self assertequalc c 1 def testnondescriptorself pep 487 says setname should work on nondescriptors create a descriptor class d def setnameself owner name self name name x dataclass class c c intfielddefaultd initfalse self assertequalc c name cx def testlookuponinstanceself see bpo33175 class d pass d d create an attribute on the instance not type d setname mock make sure d setname is not called dataclass class c i intfielddefaultd initfalse self assertequald setname callcount 0 def testlookuponclassself see bpo33175 class d pass d setname mock make sure d setname is called dataclass class c i intfielddefaultd initfalse self assertequald setname callcount 1 def testinitcallssetself class d pass d set mock dataclass class c i d d make sure d set is called d set resetmock c c5 self assertequald set callcount 1 def testgettingfieldcallsgetself class d pass d set mock d get mock dataclass class c i d d c c5 make sure d get is called d get resetmock value c i self assertequald get callcount 1 def testsettingfieldcallssetself class d pass d set mock dataclass class c i d d c c5 make sure d set is called d set resetmock c i 10 self assertequald set callcount 1 def testsettinguninitializeddescriptorfieldself class d pass d set mock dataclass class c i d d set is not called because there s no d instance to call it on d set resetmock c c5 self assertequald set callcount 0 d set still isn t called after setting i to an instance of d because descriptors don t behave like that when stored as instance vars c i d c i 5 self assertequald set callcount 0 def testdefaultvalueself class d def getself instance any owner object int if instance is none return 100 return instance x def setself instance any value int none instance x value dataclass class c i d d c c self assertequalc i 100 c c5 self assertequalc i 5 def testnodefaultvalueself class d def getself instance any owner object int if instance is none raise attributeerror return instance x def setself instance any value int none instance x value dataclass class c i d d with self assertraisesregextypeerror missing 1 required positional argument c c class teststringannotationsunittest testcase def testclassvarself some expressions recognized as classvar really aren t but if you re using string annotations it s not an exact science these tests assume that both import typing and from typing import have been run in this file for typestr in classvarint classvar int classvar int classvar classvar typing classvarint typing classvarstr typing classvarstr typing classvarstr typing classvarstr typing classvar str typing classvar str not syntactically valid but these will be treated as classvars typing classvar int typing classvar with self subtesttypestrtypestr dataclass class c x typestr x is a classvar so c takes no args c and it won t appear in the class s dict because it doesn t have a default self assertnotin x c dict def testisntclassvarself for typestr in cv t classvar t classvarint typing classvarint classvar classvarint typing classvarxint typong classvarint dataclasses classvarint typingxclassvarstr with self subtesttypestrtypestr dataclass class c x typestr x is not a classvar so c takes one arg self assertequalc10 x 10 def testinitvarself these tests assume that both import dataclasses and from dataclasses import have been run in this file for typestr in initvarint initvar int initvar int initvar initvar dataclasses initvarint dataclasses initvarstr dataclasses initvarstr dataclasses initvarstr dataclasses initvarstr dataclasses initvar str dataclasses initvar str not syntactically valid but these will be treated as initvars dataclasses initvar int dataclasses initvar with self subtesttypestrtypestr dataclass class c x typestr x is an initvar so doesn t create a member with self assertraisesregexattributeerror object has no attribute x c1 x def testisntinitvarself for typestr in iv dc initvar xdataclasses xinitvar typing xinitvarint with self subtesttypestrtypestr dataclass class c x typestr x is not an initvar so there will be a member x self assertequalc10 x 10 def testclassvarmodulelevelimportself from test testdataclasses import dataclassmodule1 from test testdataclasses import dataclassmodule1str from test testdataclasses import dataclassmodule2 from test testdataclasses import dataclassmodule2str for m in dataclassmodule1 dataclassmodule1str dataclassmodule2 dataclassmodule2str with self subtestmm there s a difference in how the classvars are interpreted when using string annotations or not see the imported modules for details if m usingstrings c m cv10 else c m cv self assertequalc cv0 20 there s a difference in how the initvars are interpreted when using string annotations or not see the imported modules for details c m iv0 1 2 3 4 for fieldname in iv0 iv1 iv2 iv3 with self subtestfieldnamefieldname with self assertraisesregexattributeerror fobject has no attribute fieldname since fieldname is an initvar it s not an instance field getattrc fieldname if m usingstrings iv4 is interpreted as a normal field self assertin notiv4 c dict self assertequalc notiv4 4 else iv4 is interpreted as an initvar so it won t exist on the instance self assertnotin notiv4 c dict def testtextannotationsself from test testdataclasses import dataclasstextanno self assertequal gettypehintsdataclasstextanno bar foo dataclasstextanno foo self assertequal gettypehintsdataclasstextanno bar init foo dataclasstextanno foo return typenone bymakedataclass makedataclass bymakedataclass x int manualmodulemakedataclass makedataclass manualmodulemakedataclass x int modulename wrongnamemakedataclass makedataclass wrong x int wrongmodulemakedataclass makedataclass wrongmodulemakedataclass x int module custom class testmakedataclassunittest testcase def testsimpleself c makedataclass c x int y int fielddefault5 namespace addone lambda self self x 1 c c10 self assertequalc x c y 10 5 self assertequalc addone 11 def testnomutatenamespaceself make sure a provided namespace isn t mutated ns c makedataclass c x int y int fielddefault5 namespacens self assertequalns def testbaseself class base1 pass class base2 pass c makedataclass c x int basesbase1 base2 c c2 self assertisinstancec c self assertisinstancec base1 self assertisinstancec base2 def testbasedataclassself dataclass class base1 x int class base2 pass c makedataclass c y int basesbase1 base2 with self assertraisesregextypeerror required positional c c2 c c1 2 self assertisinstancec c self assertisinstancec base1 self assertisinstancec base2 self assertequalc x c y 1 2 def testinitvarself def postinitself y self x y c makedataclass c x int y initvarint namespace postinit postinit c c2 3 self assertequalvarsc x 6 self assertequallenfieldsc 1 def testclassvarself c makedataclass c x int y classvarint 10 z classvarint fielddefault20 c c1 self assertequalvarsc x 1 self assertequallenfieldsc 1 self assertequalc y 10 self assertequalc z 20 def testotherparamsself c makedataclass c x int y classvarint 10 z classvarint fielddefault20 initfalse make sure we have a repr but no init self assertnotin init varsc self assertin repr varsc make sure random other params don t work with self assertraisesregextypeerror unexpected keyword argument c makedataclass c xxinitfalse def testnotypesself c makedataclass point x y z c c1 2 3 self assertequalvarsc x 1 y 2 z 3 self assertequalc annotations x typing any y typing any z typing any c makedataclass point x y int z c c1 2 3 self assertequalvarsc x 1 y 2 z 3 self assertequalc annotations x typing any y int z typing any def testmoduleattrself self assertequalbymakedataclass module name self assertequalbymakedataclass1 module name self assertequalwrongmodulemakedataclass module custom nested makedataclass nested self assertequalnested module name self assertequalnested module name def testpicklesupportself for klass in bymakedataclass manualmodulemakedataclass for proto in rangepickle highestprotocol 1 with self subtestprotoproto self assertequal pickle loadspickle dumpsklass proto klass self assertequal pickle loadspickle dumpsklass1 proto klass1 def testcannotbepickledself for klass in wrongnamemakedataclass wrongmodulemakedataclass for proto in rangepickle highestprotocol 1 with self subtestprotoproto with self assertraisespickle pickleerror pickle dumpsklass proto with self assertraisespickle pickleerror pickle dumpsklass1 proto def testinvalidtypespecificationself for badfield in 1 2 3 4 with self subtestbadfieldbadfield with self assertraisesregextypeerror r invalid field makedataclass c a badfield and test for things with no len for badfield in float lambda x x with self subtestbadfieldbadfield with self assertraisesregextypeerror r has no len makedataclass c a badfield def testduplicatefieldnamesself for field in a ab with self subtestfieldfield with self assertraisesregextypeerror field name duplicated makedataclass c field a field def testkeywordfieldnamesself for field in for async await as with self subtestfieldfield with self assertraisesregextypeerror must not be keywords makedataclass c a field with self assertraisesregextypeerror must not be keywords makedataclass c field with self assertraisesregextypeerror must not be keywords makedataclass c field a def testnonidentifierfieldnamesself for field in x y 23 little johnny tables with self subtestfieldfield with self assertraisesregextypeerror must be valid identifiers makedataclass c a field with self assertraisesregextypeerror must be valid identifiers makedataclass c field with self assertraisesregextypeerror must be valid identifiers makedataclass c field a def testunderscorefieldnamesself unlike namedtuple it s okay if dataclass field names have an underscore makedataclass c a aa a def testfunnyclassnamesnamesself no reason to prevent weird class names since types newclass allows them for classname in x y 23 with self subtestclassnameclassname c makedataclassclassname a b self assertequalc name classname class testreplaceunittest testcase def testself dataclassfrozentrue class c x int y int c c1 2 c1 replacec x3 self assertequalc1 x 3 self assertequalc1 y 2 def testfrozenself dataclassfrozentrue class c x int y int z int fieldinitfalse default10 t int fieldinitfalse default100 c c1 2 c1 replacec x3 self assertequalc x c y c z c t 1 2 10 100 self assertequalc1 x c1 y c1 z c1 t 3 2 10 100 with self assertraisesregextypeerror initfalse replacec x3 z20 t50 with self assertraisesregextypeerror initfalse replacec z20 replacec x3 z20 t50 make sure the result is still frozen with self assertraisesregexfrozeninstanceerror cannot assign to field x c1 x 3 make sure we can t replace an attribute that doesn t exist if we re also replacing one that does exist test this here because setting attributes on frozen instances is handled slightly differently from nonfrozen ones with self assertraisesregextypeerror rinit got an unexpected keyword argument a c1 replacec x20 a5 def testinvalidfieldnameself dataclassfrozentrue class c x int y int c c1 2 with self assertraisesregextypeerror rinit got an unexpected keyword argument z c1 replacec z3 def testinvalidobjectself dataclassfrozentrue class c x int y int with self assertraisesregextypeerror dataclass instance replacec x3 with self assertraisesregextypeerror dataclass instance replace0 x3 def testnoinitself dataclass class c x int y int fieldinitfalse default10 c c1 c y 20 make sure y gets the default value c1 replacec x5 self assertequalc1 x c1 y 5 10 trying to replace y is an error with self assertraisesregextypeerror initfalse replacec x2 y30 with self assertraisesregextypeerror initfalse replacec y30 def testclassvarself dataclass class c x int y classvarint 1000 c c1 d c2 self assertisc y d y self assertequalc y 1000 trying to replace y is an error can t replace classvars with self assertraisesregextypeerror rinit got an unexpected keyword argument y replacec y30 replacec x5 def testinitvarisspecifiedself dataclass class c x int y initvarint def postinitself y self x y c c1 10 self assertequalc x 10 with self assertraisesregextypeerror rinitvar y must be rspecified with replace replacec x3 c replacec x3 y5 self assertequalc x 15 def testinitvarwithdefaultvalueself dataclass class c x int y initvarint none z initvarint 42 def postinitself y z if y is not none self x y if z is not none self x z c cx1 y10 z1 self assertequalreplacec cx12 self assertequalreplacec y4 cx12 y4 z42 self assertequalreplacec y4 z1 cx12 y4 z1 def testrecursivereprself dataclass class c f c c cnone c f c self assertequalreprc testreplace testrecursiverepr locals cf def testrecursivereprtwoattrsself dataclass class c f c g c c cnone none c f c c g c self assertequalreprc testreplace testrecursivereprtwoattrs locals cf g def testrecursivereprindirectionself dataclass class c f d dataclass class d f c c cnone d dnone c f d d f c self assertequalreprc testreplace testrecursivereprindirection locals cftestreplace testrecursivereprindirection locals df def testrecursivereprindirectiontwoself dataclass class c f d dataclass class d f e dataclass class e f c c cnone d dnone e enone c f d d f e e f c self assertequalreprc testreplace testrecursivereprindirectiontwo locals cftestreplace testrecursivereprindirectiontwo locals dftestreplace testrecursivereprindirectiontwo locals ef def testrecursivereprmiscattrsself dataclass class c f c g int c cnone 1 c f c self assertequalreprc testreplace testrecursivereprmiscattrs locals cf g1 def testinitvarself dataclass class c x int y initvarint c c1 10 d c2 20 in our case replacing an initvar is a noop self assertequalc replacec y5 replacec x5 class testabstractunittest testcase def testabcimplementationself class orderedabc abc abc abstractmethod def ltself other pass abc abstractmethod def leself other pass dataclassordertrue class dateordered year int month month day int self assertfalseinspect isabstractdate self assertgreaterdate2020 12 25 date2020 8 31 def testmaintainabcself class aabc abc abc abstractmethod def fooself pass dataclass class datea year int month month day int self asserttrueinspect isabstractdate msg class date without an implementation for abstract method foo self assertraisesregextypeerror msg date class testmatchargsunittest testcase def testmatchargsself dataclass class c a int self assertequalc42 matchargs a def testexplicitmatchargsself ma dataclass class c a int matchargs ma self assertisc42 matchargs ma def testbpo43764self dataclassreprfalse eqfalse initfalse class x a int b int c int self assertequalx matchargs a b c def testmatchargsargumentself dataclassmatchargsfalse class x a int self assertnotin matchargs x dict dataclassmatchargsfalse class y a int matchargs b self assertequaly matchargs b dataclassmatchargsfalse class zy z int self assertequalz matchargs b ensure parent dataclass matchargs is seen if child class specifies matchargsfalse dataclass class a a int z int dataclassmatchargsfalse class ba b int self assertequalb matchargs a z def testmakedataclassesself c makedataclass c x int y int self assertequalc matchargs x y c makedataclass c x int y int matchargstrue self assertequalc matchargs x y c makedataclass c x int y int matchargsfalse self assertnotin matchargs c dict c makedataclass c x int y int namespace matchargs z self assertequalc matchargs z class testkeywordargsunittest testcase def testnoclassvarkwargself msg field a is a classvar but specifies kwonly with self assertraisesregextypeerror msg dataclass class a a classvarint fieldkwonlytrue with self assertraisesregextypeerror msg dataclass class a a classvarint fieldkwonlyfalse with self assertraisesregextypeerror msg dataclasskwonlytrue class a a classvarint fieldkwonlyfalse def testfieldmarkedaskwonlyself using dataclasskwonlytrue dataclasskwonlytrue class a a int self asserttruefieldsa0 kwonly dataclasskwonlytrue class a a int fieldkwonlytrue self asserttruefieldsa0 kwonly dataclasskwonlytrue class a a int fieldkwonlyfalse self assertfalsefieldsa0 kwonly using dataclasskwonlyfalse dataclasskwonlyfalse class a a int self assertfalsefieldsa0 kwonly dataclasskwonlyfalse class a a int fieldkwonlytrue self asserttruefieldsa0 kwonly dataclasskwonlyfalse class a a int fieldkwonlyfalse self assertfalsefieldsa0 kwonly not specifying dataclasskwonly dataclass class a a int self assertfalsefieldsa0 kwonly dataclass class a a int fieldkwonlytrue self asserttruefieldsa0 kwonly dataclass class a a int fieldkwonlyfalse self assertfalsefieldsa0 kwonly def testmatchargsself kw fields don t show up in matchargs dataclasskwonlytrue class c a int self assertequalca42 matchargs dataclass class c a int b int fieldkwonlytrue self assertequalc42 b10 matchargs a def testkwonlyself dataclass class a a int kwonly b int c int a3 c5 b4 msg takes 2 positional arguments but 4 were given with self assertraisesregextypeerror msg a3 4 5 dataclasskwonlytrue class b a int kwonly b int c int ba3 b4 c5 msg takes 1 positional argument but 4 were given with self assertraisesregextypeerror msg b3 4 5 explicitly make a field that follows kwonly be nonkeywordonly dataclass class c a int kwonly b int c int fieldkwonlyfalse c c1 2 b3 self assertequalc a 1 self assertequalc b 3 self assertequalc c 2 c c1 b3 c2 self assertequalc a 1 self assertequalc b 3 self assertequalc c 2 c c1 b3 c2 self assertequalc a 1 self assertequalc b 3 self assertequalc c 2 c cc2 b3 a1 self assertequalc a 1 self assertequalc b 3 self assertequalc c 2 def testkwonlyasstringself dataclass class a a int dataclasses kwonly b int c int a3 c5 b4 msg takes 2 positional arguments but 4 were given with self assertraisesregextypeerror msg a3 4 5 def testkwonlytwiceself msg y is kwonly but kwonly has already been specified with self assertraisesregextypeerror msg dataclass class a a int x kwonly y kwonly b int c int with self assertraisesregextypeerror msg dataclass class a a int x kwonly b int y kwonly c int with self assertraisesregextypeerror msg dataclass class a a int x kwonly b int c int y kwonly but this usage is okay since it s not using kwonly dataclass class a a int kwonly b int c int fieldkwonlytrue and if inheriting it s okay dataclass class a a int kwonly b int c int dataclass class ba kwonly d int make sure the error is raised in a derived class with self assertraisesregextypeerror msg dataclass class a a int kwonly b int c int dataclass class ba x kwonly d int y kwonly def testpostinitself dataclass class a a int kwonly b initvarint c int d initvarint def postinitself b d raise customerrorf b d with self assertraisesregexcustomerror b3 d4 a1 c2 b3 d4 dataclass class b a int kwonly b initvarint c int d initvarint def postinitself b d self a b self c d b b1 c2 b3 d4 self assertequalasdictb a 3 c 4 def testdefaultsself for kwargs make sure we can have defaults after nondefaults dataclass class a a int 0 kwonly b int c int 1 d int a ad4 b3 self assertequala a 0 self assertequala b 3 self assertequala c 1 self assertequala d 4 make sure we still check for nonkwarg nondefaults not following defaults errregex nondefault argument z follows default argument a with self assertraisesregextypeerror errregex dataclass class a a int 0 z int kwonly b int c int 1 d int def testmakedataclassself a makedataclassa a kwonlytrue self asserttruefieldsa0 kwonly b makedataclassb a b int fieldkwonlyfalse kwonlytrue self asserttruefieldsb0 kwonly self assertfalsefieldsb1 kwonly if name main unittest main deliberately use from dataclasses import every name in __all__ is tested so they all must be present this is a way to catch missing ones needed for the string typing classvar int to work as an annotation needed for the string dataclasses initvar int to work as an annotation just any custom exception we can catch even though this is testing an internal implementation detail it s testing a feature we want to make sure is correctly implemented for the sake of dataclasses itself even though this is testing an internal implementation detail it s testing a feature we want to make sure is correctly implemented for the sake of dataclasses itself non defaults following defaults a derived class adds a non default field after a default one override a base class field and add a default to a field which didn t use to have a default test that declaring this class isn t an error it should use the user provided __hash__ test that declaring this class isn t an error it should use the generated __hash__ but this one should generate an exception because with unsafe_hash true it s an error to have a __hash__ defined creating this class should not generate an exception because even though __hash__ exists before dataclass is called due to __eq__ being defined since it s none that s okay the generated hash function works as we d expect creating this class should generate an exception because __hash__ exists and is not none which it would be if it had been auto generated due to __eq__ being defined note that x from c1 replaces x in base but the order remains the same as defined in base make sure the first parameter is not named self but we do use self if no field named self make sure the first parameter is named self gh 96151 gh 98886 attribute names can shadow built in names since code generation is used ensure that this is not happening attribute names can shadow built in names since code generation is used ensure that this is not happening for frozen data classes ensure that order false is the default ensure that order false is the default ensure that order false is the default comparisons fail for subclasses even if no fields are added test combining eq and order if we delete the instance attribute we should then see the class attribute see bpo 44674 make sure that the machinery looking for hashability is using the class s __hash__ not the instance s __hash__ this shouldn t make the variable hashable test all 6 cases of hash true false none compare true false __hash__ contains the field the field is not present in the hash if init false and no default value then the field won t be present in the instance the_fields is a tuple of 3 items each value is in __annotations__ we only have a class attribute if a default value is specified either directly or via a field with a default for the known types don t allow mutable default values can t use a zero length value nor a non zero length value check subtypes also fail because this is a classvar it can be mutable because this is a classvar it can be mutable if a mutable default isn t in the known list of list dict set then it s okay these 2 instances will share this value of x call with dataclass make sure we can t be compared to a tuple and that we can t compare to another unrelated dataclass test that some of the problems with namedtuple don t happen here make sure we can t unpack make sure another class with the same field names isn t equal some dummy class and instance to use as a default first check __annotations__ even though they re not function annotations verify __init__ check the return type should be none check each parameter this is testing an internal name and probably shouldn t be tested don t test for the default since it s set to missing don t test for the default since it s set to missing now repeat with __hash__ test that missing works the same as a default not being specified test that missing works the same as a default factory not being specified which is really the same as a default not being specified too just make sure it gets called post init gets called but doesn t raise this is just checking that self is used correctly if there s not an __init__ then post init won t get called creating the class won t raise make sure that if we re frozen post init can t set attributes make sure super post init isn t called by default now call super and it will raise make sure post init is called even if not defined in our class see bpo 46757 which had proposed always adding __post_init__ as raymond hettinger pointed out that would be a breaking change so add a test to make sure that the current behavior doesn t change since a0 has no __post_init__ and one wasn t automatically added because that s the rule it s never added by dataclass it s only the class that can add it then b0 __post_init__ is called verify that now the same thing except a1 defines __post_init__ this time b1 __post_init__ isn t being called this mimics what would happen if a1 __post_init__ had been automatically added instead of manually added as we see here this test isn t really needed but i m including it just to demonstrate the changed behavior when a1 does define __post_init__ make sure classvars are ignored in __init__ __repr__ etc we have 2 fields and 4 classvars if a classvar has no default value it should not be set on the class it makes no sense for a classvar to have a default factory when would it be called call it yourself since it s class wide if a classvar has a default value it should be set on the class make sure classvars work even if we re frozen we have 2 fields and 3 classvars we can still modify the classvar it s only instances that are frozen if an initvar has no default value it should not be set on the class it makes no sense for an initvar to have a default factory when would it be called call it yourself since it s class wide if an initvar has a default value it should be set on the class make sure the repr is correct note that this deliberately tests that a dataclass need not have a __post_init__ function if it has an initvar field it could just be used in a derived class as shown here we can instantiate by passing the initvar even though it s not used test a factory that returns a new list test a factory that returns a shared list test various other field flags repr hash init see also test_default_factory_with_no_init compare we need a factory with a side effect make sure the default factory is called for each new instance we need a factory that we can test if it s been called make sure that if a field has a default factory function it s not called if a value is specified see bpo 32896 test that an intermediate class that defines annotations does not define fields y was not initialized and if we again derive a non dataclass no fields are added it s an error for a classvar to have a factory function see bpo 37868 also test for an instance attribute indirect tests for _is_dataclass_instance check that we can call fields on either a class or instance and get back the same thing check that typeerror is raised if not passed a dataclass or instance basic tests for asdict it should return a new dictionary asdict should raise on a class object now with a dict_factory ordereddict is convenient but since it compares to dicts we also need to have separate assertis tests make sure that the returned dicts are actually ordereddicts ensure that a field that contains a dict which has a namedtuple as a key works with asdict make sure that t has been copied not used directly ensure asdict does not throw exceptions when a defaultdict is a member of a dataclass make sure defaultdict is copied basic tests for astuple it should return a new tuple astuple should raise on a class object now using a tuple_factory list is convenient here ensure astuple does not throw exceptions when a defaultdict is a member of a dataclass make sure defaultdict is copied create the class make it a dataclass create the class make it a dataclass class dict instance dict since __post_init__ can t take params use a classmethod alternate constructor this is mostly an example to show how to use this technique in a real example create a new instance and populate x from contents of a file make sure the default metadata is read only and of zero length make sure only a mapping can be passed as metadata zero length make sure an empty dict works update should work see bpo 35960 make sure a non empty dict works update should work non existent key try a custom mapping make sure we re still talking to our custom mapping subscripting the resulting class should work etc check that type aliases work correctly check mro resolution this is still an error make sure we don t pick up the type annotation in the base class same test but with the base class not a dataclass this is still an error make sure we don t pick up the type annotation in the base class because 3 6 and 3 7 differ in how inspect signature work see bpo 32108 for the time being just compare them with whitespace stripped lorem ipsum see https github com python cpython issues 103449 make sure that declaring this class doesn t raise an error the issue is that we can t override __init__ in our class but it should be okay to add __init__ to us if our base has an __init__ make sure that if we don t add an init the base __init__ gets called if the class has __init__ use it no matter the value of init dataclasses inheriting from protocol should preserve their own __init__ see bpo 45081 test a class with no __repr__ and repr false test a class with a __repr__ and repr false if the class has __repr__ use it no matter the value of repr test a class with no __eq__ and eq false test a class with an __eq__ and eq false if the class has __eq__ use it no matter the value of eq test that functools total_ordering works with this class perform the test backward just to make sure this is being called test that no ordering functions are added by default make sure no order methods are added test that __lt__ is still called make sure other methods aren t added map to something else that s true but not a bool see if the result matches what s expected __hash__ contains the function we generated __hash__ is not present in our class __hash__ is set to none creating the class should cause an exception this only happens with with_hash true there are 8 cases of unsafe_hash true false eq true false frozen true false and for each of these a different result if __hash__ is defined or not test non bool truth values too this is just to make sure the data driven table in the decorator handles non bool values if a class defines __eq__ __hash__ is automatically added and set to none this is normal python behavior not related to dataclasses make sure we don t interfere with that see bpo 32546 and make sure things work in this case if we specify unsafe_hash true and check that the classes __eq__ is being used despite specifying eq true test dataclasses with no hash argument this exists to make sure that if the dataclass parameter name is changed or the non default hashing behavior changes the default hashability keeps working the same way if frozen or eq is none then use the default value do not specify any value in the decorator first create the class now make sure it hashes as expected i m not sure what test to use here object s hash isn t based on id so calling hash won t tell us much so just check the function used is object s test both ways with an intermediate normal non dataclass class and without an intermediate class see bpo 32953 but can t change the frozen attributes frozen uses __setattr__ and __delattr__ if x is immutable we can compute the hash no exception is raised if x is mutable computing the hash is an error there was a bug where a variable in a slot was assumed to also have a default value of type types memberdescriptortype we can create an instance and assign to x we can t assign to anything else see bpo 33100 we can add a new field to the derived instance can t be local to test_frozen_pickle bpo 43999 originally reported in bpo 44649 originally reported in bpo 44649 no weakref __weakref__ is in the base class not a but an a is still weakref able same as test_slots_weakref_base but use a tuple instead of a string in the base class __weakref__ is in the base class not a but an a is still weakref able and make sure if raises if slots true is not given a can also specify weakref_slot true if it wants to gh 93521 __weakref__ is in the base class not a but an instance of a is still weakref able __weakref__ is in the base class not a even though a doesn t specify weakref_slot it should still be weakref able __weakref__ is in the base class not a but an instance of a is still weakref able see bpo 33141 create a descriptor this is the case of just normal descriptor behavior no dataclass code is involved in initializing the descriptor now test with a default value and init false which is the only time this is really meaningful if not using init false then the descriptor will be overwritten anyway pep 487 says __set_name__ should work on non descriptors create a descriptor see bpo 33175 create an attribute on the instance not type make sure d __set_name__ is not called see bpo 33175 make sure d __set_name__ is called make sure d __set__ is called make sure d __get__ is called make sure d __set__ is called d __set__ is not called because there s no d instance to call it on d __set__ still isn t called after setting i to an instance of d because descriptors don t behave like that when stored as instance vars some expressions recognized as classvar really aren t but if you re using string annotations it s not an exact science these tests assume that both import typing and from typing import have been run in this file not syntactically valid but these will be treated as classvars x is a classvar so c takes no args and it won t appear in the class s dict because it doesn t have a default x is not a classvar so c takes one arg these tests assume that both import dataclasses and from dataclasses import have been run in this file not syntactically valid but these will be treated as initvars x is an initvar so doesn t create a member x is not an initvar so there will be a member x there s a difference in how the classvars are interpreted when using string annotations or not see the imported modules for details there s a difference in how the initvars are interpreted when using string annotations or not see the imported modules for details since field_name is an initvar it s not an instance field iv4 is interpreted as a normal field iv4 is interpreted as an initvar so it won t exist on the instance make sure a provided namespace isn t mutated make sure we have a repr but no init make sure random other params don t work and test for things with no len unlike namedtuple it s okay if dataclass field names have an underscore no reason to prevent weird class names since types new_class allows them make sure the result is still frozen make sure we can t replace an attribute that doesn t exist if we re also replacing one that does exist test this here because setting attributes on frozen instances is handled slightly differently from non frozen ones make sure y gets the default value trying to replace y is an error trying to replace y is an error can t replace classvars def test_initvar self dataclass class c x int y initvar int c c 1 10 d c 2 20 in our case replacing an initvar is a no op self assertequal c replace c y 5 replace c x 5 ensure parent dataclass __match_args__ is seen if child class specifies match_args false using dataclass kw_only true using dataclass kw_only false not specifying dataclass kw_only kw fields don t show up in __match_args__ explicitly make a field that follows kw_only be non keyword only but this usage is okay since it s not using kw_only and if inheriting it s okay make sure the error is raised in a derived class for kwargs make sure we can have defaults after non defaults make sure we still check for non kwarg non defaults not following defaults
from dataclasses import *

import abc
import io
import pickle
import inspect
import builtins
import types
import weakref
import traceback
import unittest
from unittest.mock import Mock
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict
from typing import get_type_hints
from collections import deque, OrderedDict, namedtuple, defaultdict
from functools import total_ordering

import typing       
import dataclasses  


class CustomError(Exception): pass

class TestCase(unittest.TestCase):
    def test_no_fields(self):
        @dataclass
        class C:
            pass

        o = C()
        self.assertEqual(len(fields(C)), 0)

    def test_no_fields_but_member_variable(self):
        @dataclass
        class C:
            i = 0

        o = C()
        self.assertEqual(len(fields(C)), 0)

    def test_one_field_no_default(self):
        @dataclass
        class C:
            x: int

        o = C(42)
        self.assertEqual(o.x, 42)

    def test_field_default_default_factory_error(self):
        msg = "cannot specify both default and default_factory"
        with self.assertRaisesRegex(ValueError, msg):
            @dataclass
            class C:
                x: int = field(default=1, default_factory=int)

    def test_field_repr(self):
        int_field = field(default=1, init=True, repr=False)
        int_field.name = "id"
        repr_output = repr(int_field)
        expected_output = "Field(name='id',type=None," \
                           f"default=1,default_factory={MISSING!r}," \
                           "init=True,repr=False,hash=None," \
                           "compare=True,metadata=mappingproxy({})," \
                           f"kw_only={MISSING!r}," \
                           "_field_type=None)"

        self.assertEqual(repr_output, expected_output)

    def test_field_recursive_repr(self):
        rec_field = field()
        rec_field.type = rec_field
        rec_field.name = "id"
        repr_output = repr(rec_field)

        self.assertIn(",type=...,", repr_output)

    def test_recursive_annotation(self):
        class C:
            pass

        @dataclass
        class D:
            C: C = field()

        self.assertIn(",type=...,", repr(D.__dataclass_fields__["C"]))

    def test_dataclass_params_repr(self):
        
        
        
        @dataclass(slots=True, frozen=True)
        class Some: pass

        repr_output = repr(Some.__dataclass_params__)
        expected_output = "_DataclassParams(init=True,repr=True," \
                          "eq=True,order=False,unsafe_hash=False,frozen=True," \
                          "match_args=True,kw_only=False," \
                          "slots=True,weakref_slot=False)"
        self.assertEqual(repr_output, expected_output)

    def test_dataclass_params_signature(self):
        
        
        
        @dataclass
        class Some: pass

        for param in inspect.signature(dataclass).parameters:
            if param == 'cls':
                continue
            self.assertTrue(hasattr(Some.__dataclass_params__, param), msg=param)

    def test_named_init_params(self):
        @dataclass
        class C:
            x: int

        o = C(x=32)
        self.assertEqual(o.x, 32)

    def test_two_fields_one_default(self):
        @dataclass
        class C:
            x: int
            y: int = 0

        o = C(3)
        self.assertEqual((o.x, o.y), (3, 0))

        
        with self.assertRaisesRegex(TypeError,
                                    "non-default argument 'y' follows "
                                    "default argument 'x'"):
            @dataclass
            class C:
                x: int = 0
                y: int

        
        with self.assertRaisesRegex(TypeError,
                                    "non-default argument 'y' follows "
                                    "default argument 'x'"):
            @dataclass
            class B:
                x: int = 0

            @dataclass
            class C(B):
                y: int

        
        
        with self.assertRaisesRegex(TypeError,
                                    "non-default argument 'y' follows "
                                    "default argument 'x'"):
            @dataclass
            class B:
                x: int
                y: int

            @dataclass
            class C(B):
                x: int = 0

    def test_overwrite_hash(self):
        
        
        @dataclass(frozen=True)
        class C:
            x: int
            def __hash__(self):
                return 301
        self.assertEqual(hash(C(100)), 301)

        
        
        @dataclass(frozen=True)
        class C:
            x: int
            def __eq__(self, other):
                return False
        self.assertEqual(hash(C(100)), hash((100,)))

        
        
        with self.assertRaisesRegex(TypeError,
                                    'Cannot overwrite attribute __hash__'):
            @dataclass(unsafe_hash=True)
            class C:
                def __hash__(self):
                    pass

        
        
        
        
        @dataclass(unsafe_hash=True)
        class C:
            x: int
            def __eq__(self):
                pass
        
        self.assertEqual(hash(C(10)), hash((10,)))

        
        
        
        with self.assertRaisesRegex(TypeError,
                                    'Cannot overwrite attribute __hash__'):
            @dataclass(unsafe_hash=True)
            class C:
                x: int
                def __eq__(self):
                    pass
                def __hash__(self):
                    pass

    def test_overwrite_fields_in_derived_class(self):
        
        
        @dataclass
        class Base:
            x: Any = 15.0
            y: int = 0

        @dataclass
        class C1(Base):
            z: int = 10
            x: int = 15

        o = Base()
        self.assertEqual(repr(o), 'TestCase.test_overwrite_fields_in_derived_class.<locals>.Base(x=15.0, y=0)')

        o = C1()
        self.assertEqual(repr(o), 'TestCase.test_overwrite_fields_in_derived_class.<locals>.C1(x=15, y=0, z=10)')

        o = C1(x=5)
        self.assertEqual(repr(o), 'TestCase.test_overwrite_fields_in_derived_class.<locals>.C1(x=5, y=0, z=10)')

    def test_field_named_self(self):
        @dataclass
        class C:
            self: str
        c=C('foo')
        self.assertEqual(c.self, 'foo')

        
        sig = inspect.signature(C.__init__)
        first = next(iter(sig.parameters))
        self.assertNotEqual('self', first)

        
        @dataclass
        class C:
            selfx: str

        
        sig = inspect.signature(C.__init__)
        first = next(iter(sig.parameters))
        self.assertEqual('self', first)

    def test_field_named_object(self):
        @dataclass
        class C:
            object: str
        c = C('foo')
        self.assertEqual(c.object, 'foo')

    def test_field_named_object_frozen(self):
        @dataclass(frozen=True)
        class C:
            object: str
        c = C('foo')
        self.assertEqual(c.object, 'foo')

    def test_field_named_BUILTINS_frozen(self):
        
        @dataclass(frozen=True)
        class C:
            BUILTINS: int
        c = C(5)
        self.assertEqual(c.BUILTINS, 5)

    def test_field_with_special_single_underscore_names(self):
        

        @dataclass
        class X:
            x: int = field(default_factory=lambda: 111)
            _dflt_x: int = field(default_factory=lambda: 222)

        X()

        @dataclass
        class Y:
            y: int = field(default_factory=lambda: 111)
            _HAS_DEFAULT_FACTORY: int = 222

        assert Y(y=222).y == 222

    def test_field_named_like_builtin(self):
        
        
        
        exclusions = {'None', 'True', 'False'}
        builtins_names = sorted(
            b for b in builtins.__dict__.keys()
            if not b.startswith('__') and b not in exclusions
        )
        attributes = [(name, str) for name in builtins_names]
        C = make_dataclass('C', attributes)

        c = C(*[name for name in builtins_names])

        for name in builtins_names:
            self.assertEqual(getattr(c, name), name)

    def test_field_named_like_builtin_frozen(self):
        
        
        
        
        exclusions = {'None', 'True', 'False'}
        builtins_names = sorted(
            b for b in builtins.__dict__.keys()
            if not b.startswith('__') and b not in exclusions
        )
        attributes = [(name, str) for name in builtins_names]
        C = make_dataclass('C', attributes, frozen=True)

        c = C(*[name for name in builtins_names])

        for name in builtins_names:
            self.assertEqual(getattr(c, name), name)

    def test_0_field_compare(self):
        
        @dataclass
        class C0:
            pass

        @dataclass(order=False)
        class C1:
            pass

        for cls in [C0, C1]:
            with self.subTest(cls=cls):
                self.assertEqual(cls(), cls())
                for idx, fn in enumerate([lambda a, b: a < b,
                                          lambda a, b: a <= b,
                                          lambda a, b: a > b,
                                          lambda a, b: a >= b]):
                    with self.subTest(idx=idx):
                        with self.assertRaisesRegex(TypeError,
                                                    f"not supported between instances of '{cls.__name__}' and '{cls.__name__}'"):
                            fn(cls(), cls())

        @dataclass(order=True)
        class C:
            pass
        self.assertLessEqual(C(), C())
        self.assertGreaterEqual(C(), C())

    def test_1_field_compare(self):
        
        @dataclass
        class C0:
            x: int

        @dataclass(order=False)
        class C1:
            x: int

        for cls in [C0, C1]:
            with self.subTest(cls=cls):
                self.assertEqual(cls(1), cls(1))
                self.assertNotEqual(cls(0), cls(1))
                for idx, fn in enumerate([lambda a, b: a < b,
                                          lambda a, b: a <= b,
                                          lambda a, b: a > b,
                                          lambda a, b: a >= b]):
                    with self.subTest(idx=idx):
                        with self.assertRaisesRegex(TypeError,
                                                    f"not supported between instances of '{cls.__name__}' and '{cls.__name__}'"):
                            fn(cls(0), cls(0))

        @dataclass(order=True)
        class C:
            x: int
        self.assertLess(C(0), C(1))
        self.assertLessEqual(C(0), C(1))
        self.assertLessEqual(C(1), C(1))
        self.assertGreater(C(1), C(0))
        self.assertGreaterEqual(C(1), C(0))
        self.assertGreaterEqual(C(1), C(1))

    def test_simple_compare(self):
        
        @dataclass
        class C0:
            x: int
            y: int

        @dataclass(order=False)
        class C1:
            x: int
            y: int

        for cls in [C0, C1]:
            with self.subTest(cls=cls):
                self.assertEqual(cls(0, 0), cls(0, 0))
                self.assertEqual(cls(1, 2), cls(1, 2))
                self.assertNotEqual(cls(1, 0), cls(0, 0))
                self.assertNotEqual(cls(1, 0), cls(1, 1))
                for idx, fn in enumerate([lambda a, b: a < b,
                                          lambda a, b: a <= b,
                                          lambda a, b: a > b,
                                          lambda a, b: a >= b]):
                    with self.subTest(idx=idx):
                        with self.assertRaisesRegex(TypeError,
                                                    f"not supported between instances of '{cls.__name__}' and '{cls.__name__}'"):
                            fn(cls(0, 0), cls(0, 0))

        @dataclass(order=True)
        class C:
            x: int
            y: int

        for idx, fn in enumerate([lambda a, b: a == b,
                                  lambda a, b: a <= b,
                                  lambda a, b: a >= b]):
            with self.subTest(idx=idx):
                self.assertTrue(fn(C(0, 0), C(0, 0)))

        for idx, fn in enumerate([lambda a, b: a < b,
                                  lambda a, b: a <= b,
                                  lambda a, b: a != b]):
            with self.subTest(idx=idx):
                self.assertTrue(fn(C(0, 0), C(0, 1)))
                self.assertTrue(fn(C(0, 1), C(1, 0)))
                self.assertTrue(fn(C(1, 0), C(1, 1)))

        for idx, fn in enumerate([lambda a, b: a > b,
                                  lambda a, b: a >= b,
                                  lambda a, b: a != b]):
            with self.subTest(idx=idx):
                self.assertTrue(fn(C(0, 1), C(0, 0)))
                self.assertTrue(fn(C(1, 0), C(0, 1)))
                self.assertTrue(fn(C(1, 1), C(1, 0)))

    def test_compare_subclasses(self):
        
        
        @dataclass
        class B:
            i: int

        @dataclass
        class C(B):
            pass

        for idx, (fn, expected) in enumerate([(lambda a, b: a == b, False),
                                              (lambda a, b: a != b, True)]):
            with self.subTest(idx=idx):
                self.assertEqual(fn(B(0), C(0)), expected)

        for idx, fn in enumerate([lambda a, b: a < b,
                                  lambda a, b: a <= b,
                                  lambda a, b: a > b,
                                  lambda a, b: a >= b]):
            with self.subTest(idx=idx):
                with self.assertRaisesRegex(TypeError,
                                            "not supported between instances of 'B' and 'C'"):
                    fn(B(0), C(0))

    def test_eq_order(self):
        
        for (eq,    order, result   ) in [
            (False, False, 'neither'),
            (False, True,  'exception'),
            (True,  False, 'eq_only'),
            (True,  True,  'both'),
        ]:
            with self.subTest(eq=eq, order=order):
                if result == 'exception':
                    with self.assertRaisesRegex(ValueError, 'eq must be true if order is true'):
                        @dataclass(eq=eq, order=order)
                        class C:
                            pass
                else:
                    @dataclass(eq=eq, order=order)
                    class C:
                        pass

                    if result == 'neither':
                        self.assertNotIn('__eq__', C.__dict__)
                        self.assertNotIn('__lt__', C.__dict__)
                        self.assertNotIn('__le__', C.__dict__)
                        self.assertNotIn('__gt__', C.__dict__)
                        self.assertNotIn('__ge__', C.__dict__)
                    elif result == 'both':
                        self.assertIn('__eq__', C.__dict__)
                        self.assertIn('__lt__', C.__dict__)
                        self.assertIn('__le__', C.__dict__)
                        self.assertIn('__gt__', C.__dict__)
                        self.assertIn('__ge__', C.__dict__)
                    elif result == 'eq_only':
                        self.assertIn('__eq__', C.__dict__)
                        self.assertNotIn('__lt__', C.__dict__)
                        self.assertNotIn('__le__', C.__dict__)
                        self.assertNotIn('__gt__', C.__dict__)
                        self.assertNotIn('__ge__', C.__dict__)
                    else:
                        assert False, f'unknown result {result!r}'

    def test_field_no_default(self):
        @dataclass
        class C:
            x: int = field()

        self.assertEqual(C(5).x, 5)

        with self.assertRaisesRegex(TypeError,
                                    r"__init__\(\) missing 1 required "
                                    "positional argument: 'x'"):
            C()

    def test_field_default(self):
        default = object()
        @dataclass
        class C:
            x: object = field(default=default)

        self.assertIs(C.x, default)
        c = C(10)
        self.assertEqual(c.x, 10)

        
        
        del c.x
        self.assertIs(c.x, default)

        self.assertIs(C().x, default)

    def test_not_in_repr(self):
        @dataclass
        class C:
            x: int = field(repr=False)
        with self.assertRaises(TypeError):
            C()
        c = C(10)
        self.assertEqual(repr(c), 'TestCase.test_not_in_repr.<locals>.C()')

        @dataclass
        class C:
            x: int = field(repr=False)
            y: int
        c = C(10, 20)
        self.assertEqual(repr(c), 'TestCase.test_not_in_repr.<locals>.C(y=20)')

    def test_not_in_compare(self):
        @dataclass
        class C:
            x: int = 0
            y: int = field(compare=False, default=4)

        self.assertEqual(C(), C(0, 20))
        self.assertEqual(C(1, 10), C(1, 20))
        self.assertNotEqual(C(3), C(4, 10))
        self.assertNotEqual(C(3, 10), C(4, 10))

    def test_no_unhashable_default(self):
        
        class Unhashable:
            __hash__ = None

        unhashable_re = 'mutable default .* for field a is not allowed'
        with self.assertRaisesRegex(ValueError, unhashable_re):
            @dataclass
            class A:
                a: dict = {}

        with self.assertRaisesRegex(ValueError, unhashable_re):
            @dataclass
            class A:
                a: Any = Unhashable()

        
        
        with self.assertRaisesRegex(ValueError, unhashable_re):
            unhashable = Unhashable()
            
            unhashable.__hash__ = lambda: 0
            @dataclass
            class A:
                a: Any = unhashable

    def test_hash_field_rules(self):
        
        
        
        for (hash_,    compare, result  ) in [
            (True,     False,   'field' ),
            (True,     True,    'field' ),
            (False,    False,   'absent'),
            (False,    True,    'absent'),
            (None,     False,   'absent'),
            (None,     True,    'field' ),
            ]:
            with self.subTest(hash=hash_, compare=compare):
                @dataclass(unsafe_hash=True)
                class C:
                    x: int = field(compare=compare, hash=hash_, default=5)

                if result == 'field':
                    
                    self.assertEqual(hash(C(5)), hash((5,)))
                elif result == 'absent':
                    
                    self.assertEqual(hash(C(5)), hash(()))
                else:
                    assert False, f'unknown result {result!r}'

    def test_init_false_no_default(self):
        
        
        @dataclass
        class C:
            x: int = field(init=False)

        self.assertNotIn('x', C().__dict__)

        @dataclass
        class C:
            x: int
            y: int = 0
            z: int = field(init=False)
            t: int = 10

        self.assertNotIn('z', C(0).__dict__)
        self.assertEqual(vars(C(5)), {'t': 10, 'x': 5, 'y': 0})

    def test_class_marker(self):
        @dataclass
        class C:
            x: int
            y: str = field(init=False, default=None)
            z: str = field(repr=False)

        the_fields = fields(C)
        
        
        self.assertIsInstance(the_fields, tuple)
        for f in the_fields:
            self.assertIs(type(f), Field)
            self.assertIn(f.name, C.__annotations__)

        self.assertEqual(len(the_fields), 3)

        self.assertEqual(the_fields[0].name, 'x')
        self.assertEqual(the_fields[0].type, int)
        self.assertFalse(hasattr(C, 'x'))
        self.assertTrue (the_fields[0].init)
        self.assertTrue (the_fields[0].repr)
        self.assertEqual(the_fields[1].name, 'y')
        self.assertEqual(the_fields[1].type, str)
        self.assertIsNone(getattr(C, 'y'))
        self.assertFalse(the_fields[1].init)
        self.assertTrue (the_fields[1].repr)
        self.assertEqual(the_fields[2].name, 'z')
        self.assertEqual(the_fields[2].type, str)
        self.assertFalse(hasattr(C, 'z'))
        self.assertTrue (the_fields[2].init)
        self.assertFalse(the_fields[2].repr)

    def test_field_order(self):
        @dataclass
        class B:
            a: str = 'B:a'
            b: str = 'B:b'
            c: str = 'B:c'

        @dataclass
        class C(B):
            b: str = 'C:b'

        self.assertEqual([(f.name, f.default) for f in fields(C)],
                         [('a', 'B:a'),
                          ('b', 'C:b'),
                          ('c', 'B:c')])

        @dataclass
        class D(B):
            c: str = 'D:c'

        self.assertEqual([(f.name, f.default) for f in fields(D)],
                         [('a', 'B:a'),
                          ('b', 'B:b'),
                          ('c', 'D:c')])

        @dataclass
        class E(D):
            a: str = 'E:a'
            d: str = 'E:d'

        self.assertEqual([(f.name, f.default) for f in fields(E)],
                         [('a', 'E:a'),
                          ('b', 'B:b'),
                          ('c', 'D:c'),
                          ('d', 'E:d')])

    def test_class_attrs(self):
        
        
        default = object()
        @dataclass
        class C:
            x: int
            y: int = field(repr=False)
            z: object = default
            t: int = field(default=100)

        self.assertFalse(hasattr(C, 'x'))
        self.assertFalse(hasattr(C, 'y'))
        self.assertIs   (C.z, default)
        self.assertEqual(C.t, 100)

    def test_disallowed_mutable_defaults(self):
        
        for typ, empty, non_empty in [(list, [], [1]),
                                      (dict, {}, {0:1}),
                                      (set, set(), set([1])),
                                      ]:
            with self.subTest(typ=typ):
                
                with self.assertRaisesRegex(ValueError,
                                            f'mutable default {typ} for field '
                                            'x is not allowed'):
                    @dataclass
                    class Point:
                        x: typ = empty


                
                with self.assertRaisesRegex(ValueError,
                                            f'mutable default {typ} for field '
                                            'y is not allowed'):
                    @dataclass
                    class Point:
                        y: typ = non_empty

                
                class Subclass(typ): pass

                with self.assertRaisesRegex(ValueError,
                                            "mutable default .*Subclass'>"
                                            " for field z is not allowed"
                                            ):
                    @dataclass
                    class Point:
                        z: typ = Subclass()

                
                @dataclass
                class C:
                    z: ClassVar[typ] = typ()

                
                @dataclass
                class C:
                    x: ClassVar[typ] = Subclass()

    def test_deliberately_mutable_defaults(self):
        
        
        class Mutable:
            def __init__(self):
                self.l = []

        @dataclass
        class C:
            x: Mutable

        
        lst = Mutable()
        o1 = C(lst)
        o2 = C(lst)
        self.assertEqual(o1, o2)
        o1.x.l.extend([1, 2])
        self.assertEqual(o1, o2)
        self.assertEqual(o1.x.l, [1, 2])
        self.assertIs(o1.x, o2.x)

    def test_no_options(self):
        
        @dataclass()
        class C:
            x: int

        self.assertEqual(C(42).x, 42)

    def test_not_tuple(self):
        
        @dataclass
        class Point:
            x: int
            y: int
        self.assertNotEqual(Point(1, 2), (1, 2))

        
        @dataclass
        class C:
            x: int
            y: int
        self.assertNotEqual(Point(1, 3), C(1, 3))

    def test_not_other_dataclass(self):
        
        
        @dataclass
        class Point3D:
            x: int
            y: int
            z: int

        @dataclass
        class Date:
            year: int
            month: int
            day: int

        self.assertNotEqual(Point3D(2017, 6, 3), Date(2017, 6, 3))
        self.assertNotEqual(Point3D(1, 2, 3), (1, 2, 3))

        
        with self.assertRaisesRegex(TypeError, 'unpack'):
            x, y, z = Point3D(4, 5, 6)

        
        
        @dataclass
        class Point3Dv1:
            x: int = 0
            y: int = 0
            z: int = 0
        self.assertNotEqual(Point3D(0, 0, 0), Point3Dv1())

    def test_function_annotations(self):
        
        class F:
            pass
        f = F()

        def validate_class(cls):
            
            
            self.assertEqual(cls.__annotations__['i'], int)
            self.assertEqual(cls.__annotations__['j'], str)
            self.assertEqual(cls.__annotations__['k'], F)
            self.assertEqual(cls.__annotations__['l'], float)
            self.assertEqual(cls.__annotations__['z'], complex)

            

            signature = inspect.signature(cls.__init__)
            
            self.assertIs(signature.return_annotation, None)

            
            params = iter(signature.parameters.values())
            param = next(params)
            
            self.assertEqual(param.name, 'self')
            param = next(params)
            self.assertEqual(param.name, 'i')
            self.assertIs   (param.annotation, int)
            self.assertEqual(param.default, inspect.Parameter.empty)
            self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_OR_KEYWORD)
            param = next(params)
            self.assertEqual(param.name, 'j')
            self.assertIs   (param.annotation, str)
            self.assertEqual(param.default, inspect.Parameter.empty)
            self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_OR_KEYWORD)
            param = next(params)
            self.assertEqual(param.name, 'k')
            self.assertIs   (param.annotation, F)
            
            self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_OR_KEYWORD)
            param = next(params)
            self.assertEqual(param.name, 'l')
            self.assertIs   (param.annotation, float)
            
            self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_OR_KEYWORD)
            self.assertRaises(StopIteration, next, params)


        @dataclass
        class C:
            i: int
            j: str
            k: F = f
            l: float=field(default=None)
            z: complex=field(default=3+4j, init=False)

        validate_class(C)

        
        @dataclass(frozen=True, unsafe_hash=True)
        class C:
            i: int
            j: str
            k: F = f
            l: float=field(default=None)
            z: complex=field(default=3+4j, init=False)

        validate_class(C)

    def test_missing_default(self):
        
        
        @dataclass
        class C:
            x: int=field(default=MISSING)
        with self.assertRaisesRegex(TypeError,
                                    r'__init__\(\) missing 1 required '
                                    'positional argument'):
            C()
        self.assertNotIn('x', C.__dict__)

        @dataclass
        class D:
            x: int
        with self.assertRaisesRegex(TypeError,
                                    r'__init__\(\) missing 1 required '
                                    'positional argument'):
            D()
        self.assertNotIn('x', D.__dict__)

    def test_missing_default_factory(self):
        
        
        
        @dataclass
        class C:
            x: int=field(default_factory=MISSING)
        with self.assertRaisesRegex(TypeError,
                                    r'__init__\(\) missing 1 required '
                                    'positional argument'):
            C()
        self.assertNotIn('x', C.__dict__)

        @dataclass
        class D:
            x: int=field(default=MISSING, default_factory=MISSING)
        with self.assertRaisesRegex(TypeError,
                                    r'__init__\(\) missing 1 required '
                                    'positional argument'):
            D()
        self.assertNotIn('x', D.__dict__)

    def test_missing_repr(self):
        self.assertIn('MISSING_TYPE object', repr(MISSING))

    def test_dont_include_other_annotations(self):
        @dataclass
        class C:
            i: int
            def foo(self) -> int:
                return 4
            @property
            def bar(self) -> int:
                return 5
        self.assertEqual(list(C.__annotations__), ['i'])
        self.assertEqual(C(10).foo(), 4)
        self.assertEqual(C(10).bar, 5)
        self.assertEqual(C(10).i, 10)

    def test_post_init(self):
        
        @dataclass
        class C:
            def __post_init__(self):
                raise CustomError()
        with self.assertRaises(CustomError):
            C()

        @dataclass
        class C:
            i: int = 10
            def __post_init__(self):
                if self.i == 10:
                    raise CustomError()
        with self.assertRaises(CustomError):
            C()
        
        
        C(5)

        
        @dataclass(init=False)
        class C:
            def __post_init__(self):
                raise CustomError()
        
        C()

        @dataclass
        class C:
            x: int = 0
            def __post_init__(self):
                self.x *= 2
        self.assertEqual(C().x, 0)
        self.assertEqual(C(2).x, 4)

        
        
        @dataclass(frozen=True)
        class C:
            x: int = 0
            def __post_init__(self):
                self.x *= 2
        with self.assertRaises(FrozenInstanceError):
            C()

    def test_post_init_super(self):
        
        class B:
            def __post_init__(self):
                raise CustomError()

        @dataclass
        class C(B):
            def __post_init__(self):
                self.x = 5

        self.assertEqual(C().x, 5)

        
        @dataclass
        class C(B):
            def __post_init__(self):
                super().__post_init__()

        with self.assertRaises(CustomError):
            C()

        
        
        @dataclass
        class C(B):
            pass

        with self.assertRaises(CustomError):
            C()

    def test_post_init_staticmethod(self):
        flag = False
        @dataclass
        class C:
            x: int
            y: int
            @staticmethod
            def __post_init__():
                nonlocal flag
                flag = True

        self.assertFalse(flag)
        c = C(3, 4)
        self.assertEqual((c.x, c.y), (3, 4))
        self.assertTrue(flag)

    def test_post_init_classmethod(self):
        @dataclass
        class C:
            flag = False
            x: int
            y: int
            @classmethod
            def __post_init__(cls):
                cls.flag = True

        self.assertFalse(C.flag)
        c = C(3, 4)
        self.assertEqual((c.x, c.y), (3, 4))
        self.assertTrue(C.flag)

    def test_post_init_not_auto_added(self):
        
        
        

        @dataclass
        class A0:
            pass

        @dataclass
        class B0:
            b_called: bool = False
            def __post_init__(self):
                self.b_called = True

        @dataclass
        class C0(A0, B0):
            c_called: bool = False
            def __post_init__(self):
                super().__post_init__()
                self.c_called = True

        
        
        
        
        c = C0()
        self.assertTrue(c.b_called)
        self.assertTrue(c.c_called)

        
        
        @dataclass
        class A1:
            def __post_init__(self):
                pass

        @dataclass
        class B1:
            b_called: bool = False
            def __post_init__(self):
                self.b_called = True

        @dataclass
        class C1(A1, B1):
            c_called: bool = False
            def __post_init__(self):
                super().__post_init__()
                self.c_called = True

        
        
        
        
        
        c = C1()
        self.assertFalse(c.b_called)
        self.assertTrue(c.c_called)

    def test_class_var(self):
        
        @dataclass
        class C:
            x: int
            y: int = 10
            z: ClassVar[int] = 1000
            w: ClassVar[int] = 2000
            t: ClassVar[int] = 3000
            s: ClassVar      = 4000

        c = C(5)
        self.assertEqual(repr(c), 'TestCase.test_class_var.<locals>.C(x=5, y=10)')
        self.assertEqual(len(fields(C)), 2)                 
        self.assertEqual(len(C.__annotations__), 6)         
        self.assertEqual(c.z, 1000)
        self.assertEqual(c.w, 2000)
        self.assertEqual(c.t, 3000)
        self.assertEqual(c.s, 4000)
        C.z += 1
        self.assertEqual(c.z, 1001)
        c = C(20)
        self.assertEqual((c.x, c.y), (20, 10))
        self.assertEqual(c.z, 1001)
        self.assertEqual(c.w, 2000)
        self.assertEqual(c.t, 3000)
        self.assertEqual(c.s, 4000)

    def test_class_var_no_default(self):
        
        @dataclass
        class C:
            x: ClassVar[int]

        self.assertNotIn('x', C.__dict__)

    def test_class_var_default_factory(self):
        
        
        with self.assertRaisesRegex(TypeError,
                                    'cannot have a default factory'):
            @dataclass
            class C:
                x: ClassVar[int] = field(default_factory=int)

            self.assertNotIn('x', C.__dict__)

    def test_class_var_with_default(self):
        
        @dataclass
        class C:
            x: ClassVar[int] = 10
        self.assertEqual(C.x, 10)

        @dataclass
        class C:
            x: ClassVar[int] = field(default=10)
        self.assertEqual(C.x, 10)

    def test_class_var_frozen(self):
        
        @dataclass(frozen=True)
        class C:
            x: int
            y: int = 10
            z: ClassVar[int] = 1000
            w: ClassVar[int] = 2000
            t: ClassVar[int] = 3000

        c = C(5)
        self.assertEqual(repr(C(5)), 'TestCase.test_class_var_frozen.<locals>.C(x=5, y=10)')
        self.assertEqual(len(fields(C)), 2)                 
        self.assertEqual(len(C.__annotations__), 5)         
        self.assertEqual(c.z, 1000)
        self.assertEqual(c.w, 2000)
        self.assertEqual(c.t, 3000)
        
        
        C.z += 1
        self.assertEqual(c.z, 1001)
        c = C(20)
        self.assertEqual((c.x, c.y), (20, 10))
        self.assertEqual(c.z, 1001)
        self.assertEqual(c.w, 2000)
        self.assertEqual(c.t, 3000)

    def test_init_var_no_default(self):
        
        @dataclass
        class C:
            x: InitVar[int]

        self.assertNotIn('x', C.__dict__)

    def test_init_var_default_factory(self):
        
        
        with self.assertRaisesRegex(TypeError,
                                    'cannot have a default factory'):
            @dataclass
            class C:
                x: InitVar[int] = field(default_factory=int)

            self.assertNotIn('x', C.__dict__)

    def test_init_var_with_default(self):
        
        @dataclass
        class C:
            x: InitVar[int] = 10
        self.assertEqual(C.x, 10)

        @dataclass
        class C:
            x: InitVar[int] = field(default=10)
        self.assertEqual(C.x, 10)

    def test_init_var(self):
        @dataclass
        class C:
            x: int = None
            init_param: InitVar[int] = None

            def __post_init__(self, init_param):
                if self.x is None:
                    self.x = init_param*2

        c = C(init_param=10)
        self.assertEqual(c.x, 20)

    def test_init_var_preserve_type(self):
        self.assertEqual(InitVar[int].type, int)

        
        self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]')
        self.assertEqual(repr(InitVar[List[int]]),
                         'dataclasses.InitVar[typing.List[int]]')
        self.assertEqual(repr(InitVar[list[int]]),
                         'dataclasses.InitVar[list[int]]')
        self.assertEqual(repr(InitVar[int|str]),
                         'dataclasses.InitVar[int | str]')

    def test_init_var_inheritance(self):
        
        
        
        @dataclass
        class Base:
            x: int
            init_base: InitVar[int]

        
        
        b = Base(0, 10)
        self.assertEqual(vars(b), {'x': 0})

        @dataclass
        class C(Base):
            y: int
            init_derived: InitVar[int]

            def __post_init__(self, init_base, init_derived):
                self.x = self.x + init_base
                self.y = self.y + init_derived

        c = C(10, 11, 50, 51)
        self.assertEqual(vars(c), {'x': 21, 'y': 101})

    def test_default_factory(self):
        
        @dataclass
        class C:
            x: int
            y: list = field(default_factory=list)

        c0 = C(3)
        c1 = C(3)
        self.assertEqual(c0.x, 3)
        self.assertEqual(c0.y, [])
        self.assertEqual(c0, c1)
        self.assertIsNot(c0.y, c1.y)
        self.assertEqual(astuple(C(5, [1])), (5, [1]))

        
        l = []
        @dataclass
        class C:
            x: int
            y: list = field(default_factory=lambda: l)

        c0 = C(3)
        c1 = C(3)
        self.assertEqual(c0.x, 3)
        self.assertEqual(c0.y, [])
        self.assertEqual(c0, c1)
        self.assertIs(c0.y, c1.y)
        self.assertEqual(astuple(C(5, [1])), (5, [1]))

        
        
        @dataclass
        class C:
            x: list = field(default_factory=list, repr=False)
        self.assertEqual(repr(C()), 'TestCase.test_default_factory.<locals>.C()')
        self.assertEqual(C().x, [])

        
        @dataclass(unsafe_hash=True)
        class C:
            x: list = field(default_factory=list, hash=False)
        self.assertEqual(astuple(C()), ([],))
        self.assertEqual(hash(C()), hash(()))

        
        @dataclass
        class C:
            x: list = field(default_factory=list, init=False)
        self.assertEqual(astuple(C()), ([],))

        
        @dataclass
        class C:
            x: list = field(default_factory=list, compare=False)
        self.assertEqual(C(), C([1]))

    def test_default_factory_with_no_init(self):
        
        factory = Mock()

        @dataclass
        class C:
            x: list = field(default_factory=factory, init=False)

        
        C().x
        self.assertEqual(factory.call_count, 1)
        C().x
        self.assertEqual(factory.call_count, 2)

    def test_default_factory_not_called_if_value_given(self):
        
        factory = Mock()

        @dataclass
        class C:
            x: int = field(default_factory=factory)

        
        
        C().x
        self.assertEqual(factory.call_count, 1)
        self.assertEqual(C(10).x, 10)
        self.assertEqual(factory.call_count, 1)
        C().x
        self.assertEqual(factory.call_count, 2)

    def test_default_factory_derived(self):
        
        @dataclass
        class Foo:
            x: dict = field(default_factory=dict)

        @dataclass
        class Bar(Foo):
            y: int = 1

        self.assertEqual(Foo().x, {})
        self.assertEqual(Bar().x, {})
        self.assertEqual(Bar().y, 1)

        @dataclass
        class Baz(Foo):
            pass
        self.assertEqual(Baz().x, {})

    def test_intermediate_non_dataclass(self):
        
        

        @dataclass
        class A:
            x: int

        class B(A):
            y: int

        @dataclass
        class C(B):
            z: int

        c = C(1, 3)
        self.assertEqual((c.x, c.z), (1, 3))

        
        with self.assertRaisesRegex(AttributeError,
                                    'object has no attribute'):
            c.y

        
        class D(C):
            t: int
        d = D(4, 5)
        self.assertEqual((d.x, d.z), (4, 5))

    def test_classvar_default_factory(self):
        
        with self.assertRaisesRegex(TypeError,
                                    'cannot have a default factory'):
            @dataclass
            class C:
                x: ClassVar[int] = field(default_factory=int)

    def test_is_dataclass(self):
        class NotDataClass:
            pass

        self.assertFalse(is_dataclass(0))
        self.assertFalse(is_dataclass(int))
        self.assertFalse(is_dataclass(NotDataClass))
        self.assertFalse(is_dataclass(NotDataClass()))

        @dataclass
        class C:
            x: int

        @dataclass
        class D:
            d: C
            e: int

        c = C(10)
        d = D(c, 4)

        self.assertTrue(is_dataclass(C))
        self.assertTrue(is_dataclass(c))
        self.assertFalse(is_dataclass(c.x))
        self.assertTrue(is_dataclass(d.d))
        self.assertFalse(is_dataclass(d.e))

    def test_is_dataclass_when_getattr_always_returns(self):
        
        class A:
            def __getattr__(self, key):
                return 0
        self.assertFalse(is_dataclass(A))
        a = A()

        
        class B:
            pass
        b = B()
        b.__dataclass_fields__ = []

        for obj in a, b:
            with self.subTest(obj=obj):
                self.assertFalse(is_dataclass(obj))

                
                with self.assertRaisesRegex(TypeError, 'should be called on dataclass instances'):
                    asdict(obj)
                with self.assertRaisesRegex(TypeError, 'should be called on dataclass instances'):
                    astuple(obj)
                with self.assertRaisesRegex(TypeError, 'should be called on dataclass instances'):
                    replace(obj, x=0)

    def test_is_dataclass_genericalias(self):
        @dataclass
        class A(types.GenericAlias):
            origin: type
            args: type
        self.assertTrue(is_dataclass(A))
        a = A(list, int)
        self.assertTrue(is_dataclass(type(a)))
        self.assertTrue(is_dataclass(a))


    def test_helper_fields_with_class_instance(self):
        
        
        @dataclass
        class C:
            x: int
            y: float

        self.assertEqual(fields(C), fields(C(0, 0.0)))

    def test_helper_fields_exception(self):
        
        
        with self.assertRaisesRegex(TypeError, 'dataclass type or instance'):
            fields(0)

        class C: pass
        with self.assertRaisesRegex(TypeError, 'dataclass type or instance'):
            fields(C)
        with self.assertRaisesRegex(TypeError, 'dataclass type or instance'):
            fields(C())

    def test_clean_traceback_from_fields_exception(self):
        stdout = io.StringIO()
        try:
            fields(object)
        except TypeError as exc:
            traceback.print_exception(exc, file=stdout)
        printed_traceback = stdout.getvalue()
        self.assertNotIn("AttributeError", printed_traceback)
        self.assertNotIn("__dataclass_fields__", printed_traceback)

    def test_helper_asdict(self):
        
        @dataclass
        class C:
            x: int
            y: int
        c = C(1, 2)

        self.assertEqual(asdict(c), {'x': 1, 'y': 2})
        self.assertEqual(asdict(c), asdict(c))
        self.assertIsNot(asdict(c), asdict(c))
        c.x = 42
        self.assertEqual(asdict(c), {'x': 42, 'y': 2})
        self.assertIs(type(asdict(c)), dict)

    def test_helper_asdict_raises_on_classes(self):
        
        @dataclass
        class C:
            x: int
            y: int
        with self.assertRaisesRegex(TypeError, 'dataclass instance'):
            asdict(C)
        with self.assertRaisesRegex(TypeError, 'dataclass instance'):
            asdict(int)

    def test_helper_asdict_copy_values(self):
        @dataclass
        class C:
            x: int
            y: List[int] = field(default_factory=list)
        initial = []
        c = C(1, initial)
        d = asdict(c)
        self.assertEqual(d['y'], initial)
        self.assertIsNot(d['y'], initial)
        c = C(1)
        d = asdict(c)
        d['y'].append(1)
        self.assertEqual(c.y, [])

    def test_helper_asdict_nested(self):
        @dataclass
        class UserId:
            token: int
            group: int
        @dataclass
        class User:
            name: str
            id: UserId
        u = User('Joe', UserId(123, 1))
        d = asdict(u)
        self.assertEqual(d, {'name': 'Joe', 'id': {'token': 123, 'group': 1}})
        self.assertIsNot(asdict(u), asdict(u))
        u.id.group = 2
        self.assertEqual(asdict(u), {'name': 'Joe',
                                     'id': {'token': 123, 'group': 2}})

    def test_helper_asdict_builtin_containers(self):
        @dataclass
        class User:
            name: str
            id: int
        @dataclass
        class GroupList:
            id: int
            users: List[User]
        @dataclass
        class GroupTuple:
            id: int
            users: Tuple[User, ...]
        @dataclass
        class GroupDict:
            id: int
            users: Dict[str, User]
        a = User('Alice', 1)
        b = User('Bob', 2)
        gl = GroupList(0, [a, b])
        gt = GroupTuple(0, (a, b))
        gd = GroupDict(0, {'first': a, 'second': b})
        self.assertEqual(asdict(gl), {'id': 0, 'users': [{'name': 'Alice', 'id': 1},
                                                         {'name': 'Bob', 'id': 2}]})
        self.assertEqual(asdict(gt), {'id': 0, 'users': ({'name': 'Alice', 'id': 1},
                                                         {'name': 'Bob', 'id': 2})})
        self.assertEqual(asdict(gd), {'id': 0, 'users': {'first': {'name': 'Alice', 'id': 1},
                                                         'second': {'name': 'Bob', 'id': 2}}})

    def test_helper_asdict_builtin_object_containers(self):
        @dataclass
        class Child:
            d: object

        @dataclass
        class Parent:
            child: Child

        self.assertEqual(asdict(Parent(Child([1]))), {'child': {'d': [1]}})
        self.assertEqual(asdict(Parent(Child({1: 2}))), {'child': {'d': {1: 2}}})

    def test_helper_asdict_factory(self):
        @dataclass
        class C:
            x: int
            y: int
        c = C(1, 2)
        d = asdict(c, dict_factory=OrderedDict)
        self.assertEqual(d, OrderedDict([('x', 1), ('y', 2)]))
        self.assertIsNot(d, asdict(c, dict_factory=OrderedDict))
        c.x = 42
        d = asdict(c, dict_factory=OrderedDict)
        self.assertEqual(d, OrderedDict([('x', 42), ('y', 2)]))
        self.assertIs(type(d), OrderedDict)

    def test_helper_asdict_namedtuple(self):
        T = namedtuple('T', 'a b c')
        @dataclass
        class C:
            x: str
            y: T
        c = C('outer', T(1, C('inner', T(11, 12, 13)), 2))

        d = asdict(c)
        self.assertEqual(d, {'x': 'outer',
                             'y': T(1,
                                    {'x': 'inner',
                                     'y': T(11, 12, 13)},
                                    2),
                             }
                         )

        
        
        
        d = asdict(c, dict_factory=OrderedDict)
        self.assertEqual(d, {'x': 'outer',
                             'y': T(1,
                                    {'x': 'inner',
                                     'y': T(11, 12, 13)},
                                    2),
                             }
                         )

        
        self.assertIs(type(d), OrderedDict)
        self.assertIs(type(d['y'][1]), OrderedDict)

    def test_helper_asdict_namedtuple_key(self):
        
        

        @dataclass
        class C:
            f: dict
        T = namedtuple('T', 'a')

        c = C({T('an a'): 0})

        self.assertEqual(asdict(c), {'f': {T(a='an a'): 0}})

    def test_helper_asdict_namedtuple_derived(self):
        class T(namedtuple('Tbase', 'a')):
            def my_a(self):
                return self.a

        @dataclass
        class C:
            f: T

        t = T(6)
        c = C(t)

        d = asdict(c)
        self.assertEqual(d, {'f': T(a=6)})
        
        self.assertIsNot(d['f'], t)
        self.assertEqual(d['f'].my_a(), 6)

    def test_helper_asdict_defaultdict(self):
        
        
        @dataclass
        class C:
            mp: DefaultDict[str, List]

        dd = defaultdict(list)
        dd["x"].append(12)
        c = C(mp=dd)
        d = asdict(c)

        self.assertEqual(d, {"mp": {"x": [12]}})
        self.assertTrue(d["mp"] is not c.mp)  

    def test_helper_astuple(self):
        
        @dataclass
        class C:
            x: int
            y: int = 0
        c = C(1)

        self.assertEqual(astuple(c), (1, 0))
        self.assertEqual(astuple(c), astuple(c))
        self.assertIsNot(astuple(c), astuple(c))
        c.y = 42
        self.assertEqual(astuple(c), (1, 42))
        self.assertIs(type(astuple(c)), tuple)

    def test_helper_astuple_raises_on_classes(self):
        
        @dataclass
        class C:
            x: int
            y: int
        with self.assertRaisesRegex(TypeError, 'dataclass instance'):
            astuple(C)
        with self.assertRaisesRegex(TypeError, 'dataclass instance'):
            astuple(int)

    def test_helper_astuple_copy_values(self):
        @dataclass
        class C:
            x: int
            y: List[int] = field(default_factory=list)
        initial = []
        c = C(1, initial)
        t = astuple(c)
        self.assertEqual(t[1], initial)
        self.assertIsNot(t[1], initial)
        c = C(1)
        t = astuple(c)
        t[1].append(1)
        self.assertEqual(c.y, [])

    def test_helper_astuple_nested(self):
        @dataclass
        class UserId:
            token: int
            group: int
        @dataclass
        class User:
            name: str
            id: UserId
        u = User('Joe', UserId(123, 1))
        t = astuple(u)
        self.assertEqual(t, ('Joe', (123, 1)))
        self.assertIsNot(astuple(u), astuple(u))
        u.id.group = 2
        self.assertEqual(astuple(u), ('Joe', (123, 2)))

    def test_helper_astuple_builtin_containers(self):
        @dataclass
        class User:
            name: str
            id: int
        @dataclass
        class GroupList:
            id: int
            users: List[User]
        @dataclass
        class GroupTuple:
            id: int
            users: Tuple[User, ...]
        @dataclass
        class GroupDict:
            id: int
            users: Dict[str, User]
        a = User('Alice', 1)
        b = User('Bob', 2)
        gl = GroupList(0, [a, b])
        gt = GroupTuple(0, (a, b))
        gd = GroupDict(0, {'first': a, 'second': b})
        self.assertEqual(astuple(gl), (0, [('Alice', 1), ('Bob', 2)]))
        self.assertEqual(astuple(gt), (0, (('Alice', 1), ('Bob', 2))))
        self.assertEqual(astuple(gd), (0, {'first': ('Alice', 1), 'second': ('Bob', 2)}))

    def test_helper_astuple_builtin_object_containers(self):
        @dataclass
        class Child:
            d: object

        @dataclass
        class Parent:
            child: Child

        self.assertEqual(astuple(Parent(Child([1]))), (([1],),))
        self.assertEqual(astuple(Parent(Child({1: 2}))), (({1: 2},),))

    def test_helper_astuple_factory(self):
        @dataclass
        class C:
            x: int
            y: int
        NT = namedtuple('NT', 'x y')
        def nt(lst):
            return NT(*lst)
        c = C(1, 2)
        t = astuple(c, tuple_factory=nt)
        self.assertEqual(t, NT(1, 2))
        self.assertIsNot(t, astuple(c, tuple_factory=nt))
        c.x = 42
        t = astuple(c, tuple_factory=nt)
        self.assertEqual(t, NT(42, 2))
        self.assertIs(type(t), NT)

    def test_helper_astuple_namedtuple(self):
        T = namedtuple('T', 'a b c')
        @dataclass
        class C:
            x: str
            y: T
        c = C('outer', T(1, C('inner', T(11, 12, 13)), 2))

        t = astuple(c)
        self.assertEqual(t, ('outer', T(1, ('inner', (11, 12, 13)), 2)))

        
        t = astuple(c, tuple_factory=list)
        self.assertEqual(t, ['outer', T(1, ['inner', T(11, 12, 13)], 2)])

    def test_helper_astuple_defaultdict(self):
        
        
        @dataclass
        class C:
            mp: DefaultDict[str, List]

        dd = defaultdict(list)
        dd["x"].append(12)
        c = C(mp=dd)
        t = astuple(c)

        self.assertEqual(t, ({"x": [12]},))
        self.assertTrue(t[0] is not dd) 

    def test_dynamic_class_creation(self):
        cls_dict = {'__annotations__': {'x': int, 'y': int},
                    }

        
        cls = type('C', (), cls_dict)

        
        cls1 = dataclass(cls)

        self.assertEqual(cls1, cls)
        self.assertEqual(asdict(cls(1, 2)), {'x': 1, 'y': 2})

    def test_dynamic_class_creation_using_field(self):
        cls_dict = {'__annotations__': {'x': int, 'y': int},
                    'y': field(default=5),
                    }

        
        cls = type('C', (), cls_dict)

        
        cls1 = dataclass(cls)

        self.assertEqual(cls1, cls)
        self.assertEqual(asdict(cls1(1)), {'x': 1, 'y': 5})

    def test_init_in_order(self):
        @dataclass
        class C:
            a: int
            b: int = field()
            c: list = field(default_factory=list, init=False)
            d: list = field(default_factory=list)
            e: int = field(default=4, init=False)
            f: int = 4

        calls = []
        def setattr(self, name, value):
            calls.append((name, value))

        C.__setattr__ = setattr
        c = C(0, 1)
        self.assertEqual(('a', 0), calls[0])
        self.assertEqual(('b', 1), calls[1])
        self.assertEqual(('c', []), calls[2])
        self.assertEqual(('d', []), calls[3])
        self.assertNotIn(('e', 4), calls)
        self.assertEqual(('f', 4), calls[4])

    def test_items_in_dicts(self):
        @dataclass
        class C:
            a: int
            b: list = field(default_factory=list, init=False)
            c: list = field(default_factory=list)
            d: int = field(default=4, init=False)
            e: int = 0

        c = C(0)
        
        self.assertNotIn('a', C.__dict__)
        self.assertNotIn('b', C.__dict__)
        self.assertNotIn('c', C.__dict__)
        self.assertIn('d', C.__dict__)
        self.assertEqual(C.d, 4)
        self.assertIn('e', C.__dict__)
        self.assertEqual(C.e, 0)
        
        self.assertIn('a', c.__dict__)
        self.assertEqual(c.a, 0)
        self.assertIn('b', c.__dict__)
        self.assertEqual(c.b, [])
        self.assertIn('c', c.__dict__)
        self.assertEqual(c.c, [])
        self.assertNotIn('d', c.__dict__)
        self.assertIn('e', c.__dict__)
        self.assertEqual(c.e, 0)

    def test_alternate_classmethod_constructor(self):
        
        
        
        @dataclass
        class C:
            x: int
            @classmethod
            def from_file(cls, filename):
                
                
                value_in_file = 20
                return cls(value_in_file)

        self.assertEqual(C.from_file('filename').x, 20)

    def test_field_metadata_default(self):
        
        
        @dataclass
        class C:
            i: int

        self.assertFalse(fields(C)[0].metadata)
        self.assertEqual(len(fields(C)[0].metadata), 0)
        with self.assertRaisesRegex(TypeError,
                                    'does not support item assignment'):
            fields(C)[0].metadata['test'] = 3

    def test_field_metadata_mapping(self):
        
        
        with self.assertRaises(TypeError):
            @dataclass
            class C:
                i: int = field(metadata=0)

        
        d = {}
        @dataclass
        class C:
            i: int = field(metadata=d)
        self.assertFalse(fields(C)[0].metadata)
        self.assertEqual(len(fields(C)[0].metadata), 0)
        
        d['foo'] = 1
        self.assertEqual(len(fields(C)[0].metadata), 1)
        self.assertEqual(fields(C)[0].metadata['foo'], 1)
        with self.assertRaisesRegex(TypeError,
                                    'does not support item assignment'):
            fields(C)[0].metadata['test'] = 3

        
        d = {'test': 10, 'bar': '42', 3: 'three'}
        @dataclass
        class C:
            i: int = field(metadata=d)
        self.assertEqual(len(fields(C)[0].metadata), 3)
        self.assertEqual(fields(C)[0].metadata['test'], 10)
        self.assertEqual(fields(C)[0].metadata['bar'], '42')
        self.assertEqual(fields(C)[0].metadata[3], 'three')
        
        d['foo'] = 1
        self.assertEqual(len(fields(C)[0].metadata), 4)
        self.assertEqual(fields(C)[0].metadata['foo'], 1)
        with self.assertRaises(KeyError):
            
            fields(C)[0].metadata['baz']
        with self.assertRaisesRegex(TypeError,
                                    'does not support item assignment'):
            fields(C)[0].metadata['test'] = 3

    def test_field_metadata_custom_mapping(self):
        
        class SimpleNameSpace:
            def __init__(self, **kw):
                self.__dict__.update(kw)

            def __getitem__(self, item):
                if item == 'xyzzy':
                    return 'plugh'
                return getattr(self, item)

            def __len__(self):
                return self.__dict__.__len__()

        @dataclass
        class C:
            i: int = field(metadata=SimpleNameSpace(a=10))

        self.assertEqual(len(fields(C)[0].metadata), 1)
        self.assertEqual(fields(C)[0].metadata['a'], 10)
        with self.assertRaises(AttributeError):
            fields(C)[0].metadata['b']
        
        self.assertEqual(fields(C)[0].metadata['xyzzy'], 'plugh')

    def test_generic_dataclasses(self):
        T = TypeVar('T')

        @dataclass
        class LabeledBox(Generic[T]):
            content: T
            label: str = '<unknown>'

        box = LabeledBox(42)
        self.assertEqual(box.content, 42)
        self.assertEqual(box.label, '<unknown>')

        
        Alias = List[LabeledBox[int]]

    def test_generic_extending(self):
        S = TypeVar('S')
        T = TypeVar('T')

        @dataclass
        class Base(Generic[T, S]):
            x: T
            y: S

        @dataclass
        class DataDerived(Base[int, T]):
            new_field: str
        Alias = DataDerived[str]
        c = Alias(0, 'test1', 'test2')
        self.assertEqual(astuple(c), (0, 'test1', 'test2'))

        class NonDataDerived(Base[int, T]):
            def new_method(self):
                return self.y
        Alias = NonDataDerived[float]
        c = Alias(10, 1.0)
        self.assertEqual(c.new_method(), 1.0)

    def test_generic_dynamic(self):
        T = TypeVar('T')

        @dataclass
        class Parent(Generic[T]):
            x: T
        Child = make_dataclass('Child', [('y', T), ('z', Optional[T], None)],
                               bases=(Parent[int], Generic[T]), namespace={'other': 42})
        self.assertIs(Child[int](1, 2).z, None)
        self.assertEqual(Child[int](1, 2, 3).z, 3)
        self.assertEqual(Child[int](1, 2, 3).other, 42)
        
        Alias = Child[T]
        self.assertEqual(Alias[int](1, 2).x, 1)
        
        self.assertEqual(Child.__mro__, (Child, Parent, Generic, object))

    def test_dataclasses_pickleable(self):
        global P, Q, R
        @dataclass
        class P:
            x: int
            y: int = 0
        @dataclass
        class Q:
            x: int
            y: int = field(default=0, init=False)
        @dataclass
        class R:
            x: int
            y: List[int] = field(default_factory=list)
        q = Q(1)
        q.y = 2
        samples = [P(1), P(1, 2), Q(1), q, R(1), R(1, [2, 3, 4])]
        for sample in samples:
            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                with self.subTest(sample=sample, proto=proto):
                    new_sample = pickle.loads(pickle.dumps(sample, proto))
                    self.assertEqual(sample.x, new_sample.x)
                    self.assertEqual(sample.y, new_sample.y)
                    self.assertIsNot(sample, new_sample)
                    new_sample.x = 42
                    another_new_sample = pickle.loads(pickle.dumps(new_sample, proto))
                    self.assertEqual(new_sample.x, another_new_sample.x)
                    self.assertEqual(sample.y, another_new_sample.y)

    def test_dataclasses_qualnames(self):
        @dataclass(order=True, unsafe_hash=True, frozen=True)
        class A:
            x: int
            y: int

        self.assertEqual(A.__init__.__name__, "__init__")
        for function in (
            '__eq__',
            '__lt__',
            '__le__',
            '__gt__',
            '__ge__',
            '__hash__',
            '__init__',
            '__repr__',
            '__setattr__',
            '__delattr__',
        ):
            self.assertEqual(getattr(A, function).__qualname__, f"TestCase.test_dataclasses_qualnames.<locals>.A.{function}")

        with self.assertRaisesRegex(TypeError, r"A\.__init__\(\) missing"):
            A()


class TestFieldNoAnnotation(unittest.TestCase):
    def test_field_without_annotation(self):
        with self.assertRaisesRegex(TypeError,
                                    "'f' is a field but has no type annotation"):
            @dataclass
            class C:
                f = field()

    def test_field_without_annotation_but_annotation_in_base(self):
        @dataclass
        class B:
            f: int

        with self.assertRaisesRegex(TypeError,
                                    "'f' is a field but has no type annotation"):
            
            
            @dataclass
            class C(B):
                f = field()

    def test_field_without_annotation_but_annotation_in_base_not_dataclass(self):
        
        class B:
            f: int

        with self.assertRaisesRegex(TypeError,
                                    "'f' is a field but has no type annotation"):
            
            
            @dataclass
            class C(B):
                f = field()


class TestDocString(unittest.TestCase):
    def assertDocStrEqual(self, a, b):
        
        
        
        self.assertEqual(a.replace(' ', ''), b.replace(' ', ''))

    def test_existing_docstring_not_overridden(self):
        @dataclass
        class C:
            
            x: int

        self.assertEqual(C.__doc__, "Lorem ipsum")

    def test_docstring_no_fields(self):
        @dataclass
        class C:
            pass

        self.assertDocStrEqual(C.__doc__, "C()")

    def test_docstring_one_field(self):
        @dataclass
        class C:
            x: int

        self.assertDocStrEqual(C.__doc__, "C(x:int)")

    def test_docstring_two_fields(self):
        @dataclass
        class C:
            x: int
            y: int

        self.assertDocStrEqual(C.__doc__, "C(x:int, y:int)")

    def test_docstring_three_fields(self):
        @dataclass
        class C:
            x: int
            y: int
            z: str

        self.assertDocStrEqual(C.__doc__, "C(x:int, y:int, z:str)")

    def test_docstring_one_field_with_default(self):
        @dataclass
        class C:
            x: int = 3

        self.assertDocStrEqual(C.__doc__, "C(x:int=3)")

    def test_docstring_one_field_with_default_none(self):
        @dataclass
        class C:
            x: Union[int, type(None)] = None

        self.assertDocStrEqual(C.__doc__, "C(x:Optional[int]=None)")

    def test_docstring_list_field(self):
        @dataclass
        class C:
            x: List[int]

        self.assertDocStrEqual(C.__doc__, "C(x:List[int])")

    def test_docstring_list_field_with_default_factory(self):
        @dataclass
        class C:
            x: List[int] = field(default_factory=list)

        self.assertDocStrEqual(C.__doc__, "C(x:List[int]=<factory>)")

    def test_docstring_deque_field(self):
        @dataclass
        class C:
            x: deque

        self.assertDocStrEqual(C.__doc__, "C(x:collections.deque)")

    def test_docstring_deque_field_with_default_factory(self):
        @dataclass
        class C:
            x: deque = field(default_factory=deque)

        self.assertDocStrEqual(C.__doc__, "C(x:collections.deque=<factory>)")

    def test_docstring_with_no_signature(self):
        
        class Meta(type):
            __call__ = dict
        class Base(metaclass=Meta):
            pass

        @dataclass
        class C(Base):
            pass

        self.assertDocStrEqual(C.__doc__, "C")


class TestInit(unittest.TestCase):
    def test_base_has_init(self):
        class B:
            def __init__(self):
                self.z = 100

        
        
        
        
        @dataclass
        class C(B):
            x: int = 0
        c = C(10)
        self.assertEqual(c.x, 10)
        self.assertNotIn('z', vars(c))

        
        
        @dataclass(init=False)
        class C(B):
            x: int = 10
        c = C()
        self.assertEqual(c.x, 10)
        self.assertEqual(c.z, 100)

    def test_no_init(self):
        @dataclass(init=False)
        class C:
            i: int = 0
        self.assertEqual(C().i, 0)

        @dataclass(init=False)
        class C:
            i: int = 2
            def __init__(self):
                self.i = 3
        self.assertEqual(C().i, 3)

    def test_overwriting_init(self):
        
        

        @dataclass
        class C:
            x: int
            def __init__(self, x):
                self.x = 2 * x
        self.assertEqual(C(3).x, 6)

        @dataclass(init=True)
        class C:
            x: int
            def __init__(self, x):
                self.x = 2 * x
        self.assertEqual(C(4).x, 8)

        @dataclass(init=False)
        class C:
            x: int
            def __init__(self, x):
                self.x = 2 * x
        self.assertEqual(C(5).x, 10)

    def test_inherit_from_protocol(self):
        
        

        class P(Protocol):
            a: int

        @dataclass
        class C(P):
            a: int

        self.assertEqual(C(5).a, 5)

        @dataclass
        class D(P):
            def __init__(self, a):
                self.a = a * 2

        self.assertEqual(D(5).a, 10)


class TestRepr(unittest.TestCase):
    def test_repr(self):
        @dataclass
        class B:
            x: int

        @dataclass
        class C(B):
            y: int = 10

        o = C(4)
        self.assertEqual(repr(o), 'TestRepr.test_repr.<locals>.C(x=4, y=10)')

        @dataclass
        class D(C):
            x: int = 20
        self.assertEqual(repr(D()), 'TestRepr.test_repr.<locals>.D(x=20, y=10)')

        @dataclass
        class C:
            @dataclass
            class D:
                i: int
            @dataclass
            class E:
                pass
        self.assertEqual(repr(C.D(0)), 'TestRepr.test_repr.<locals>.C.D(i=0)')
        self.assertEqual(repr(C.E()), 'TestRepr.test_repr.<locals>.C.E()')

    def test_no_repr(self):
        
        @dataclass(repr=False)
        class C:
            x: int
        self.assertIn(f'{__name__}.TestRepr.test_no_repr.<locals>.C object at',
                      repr(C(3)))

        
        @dataclass(repr=False)
        class C:
            x: int
            def __repr__(self):
                return 'C-class'
        self.assertEqual(repr(C(3)), 'C-class')

    def test_overwriting_repr(self):
        
        

        @dataclass
        class C:
            x: int
            def __repr__(self):
                return 'x'
        self.assertEqual(repr(C(0)), 'x')

        @dataclass(repr=True)
        class C:
            x: int
            def __repr__(self):
                return 'x'
        self.assertEqual(repr(C(0)), 'x')

        @dataclass(repr=False)
        class C:
            x: int
            def __repr__(self):
                return 'x'
        self.assertEqual(repr(C(0)), 'x')


class TestEq(unittest.TestCase):
    def test_no_eq(self):
        
        @dataclass(eq=False)
        class C:
            x: int
        self.assertNotEqual(C(0), C(0))
        c = C(3)
        self.assertEqual(c, c)

        
        @dataclass(eq=False)
        class C:
            x: int
            def __eq__(self, other):
                return other == 10
        self.assertEqual(C(3), 10)

    def test_overwriting_eq(self):
        
        

        @dataclass
        class C:
            x: int
            def __eq__(self, other):
                return other == 3
        self.assertEqual(C(1), 3)
        self.assertNotEqual(C(1), 1)

        @dataclass(eq=True)
        class C:
            x: int
            def __eq__(self, other):
                return other == 4
        self.assertEqual(C(1), 4)
        self.assertNotEqual(C(1), 1)

        @dataclass(eq=False)
        class C:
            x: int
            def __eq__(self, other):
                return other == 5
        self.assertEqual(C(1), 5)
        self.assertNotEqual(C(1), 1)


class TestOrdering(unittest.TestCase):
    def test_functools_total_ordering(self):
        
        @total_ordering
        @dataclass
        class C:
            x: int
            def __lt__(self, other):
                
                
                return self.x >= other

        self.assertLess(C(0), -1)
        self.assertLessEqual(C(0), -1)
        self.assertGreater(C(0), 1)
        self.assertGreaterEqual(C(0), 1)

    def test_no_order(self):
        
        @dataclass(order=False)
        class C:
            x: int
        
        self.assertNotIn('__le__', C.__dict__)
        self.assertNotIn('__lt__', C.__dict__)
        self.assertNotIn('__ge__', C.__dict__)
        self.assertNotIn('__gt__', C.__dict__)

        
        @dataclass(order=False)
        class C:
            x: int
            def __lt__(self, other):
                return False
        
        self.assertNotIn('__le__', C.__dict__)
        self.assertNotIn('__ge__', C.__dict__)
        self.assertNotIn('__gt__', C.__dict__)

    def test_overwriting_order(self):
        with self.assertRaisesRegex(TypeError,
                                    'Cannot overwrite attribute __lt__'
                                    '.*using functools.total_ordering'):
            @dataclass(order=True)
            class C:
                x: int
                def __lt__(self):
                    pass

        with self.assertRaisesRegex(TypeError,
                                    'Cannot overwrite attribute __le__'
                                    '.*using functools.total_ordering'):
            @dataclass(order=True)
            class C:
                x: int
                def __le__(self):
                    pass

        with self.assertRaisesRegex(TypeError,
                                    'Cannot overwrite attribute __gt__'
                                    '.*using functools.total_ordering'):
            @dataclass(order=True)
            class C:
                x: int
                def __gt__(self):
                    pass

        with self.assertRaisesRegex(TypeError,
                                    'Cannot overwrite attribute __ge__'
                                    '.*using functools.total_ordering'):
            @dataclass(order=True)
            class C:
                x: int
                def __ge__(self):
                    pass

class TestHash(unittest.TestCase):
    def test_unsafe_hash(self):
        @dataclass(unsafe_hash=True)
        class C:
            x: int
            y: str
        self.assertEqual(hash(C(1, 'foo')), hash((1, 'foo')))

    def test_hash_rules(self):
        def non_bool(value):
            
            if value is None:
                return None
            if value:
                return (3,)
            return 0

        def test(case, unsafe_hash, eq, frozen, with_hash, result):
            with self.subTest(case=case, unsafe_hash=unsafe_hash, eq=eq,
                              frozen=frozen):
                if result != 'exception':
                    if with_hash:
                        @dataclass(unsafe_hash=unsafe_hash, eq=eq, frozen=frozen)
                        class C:
                            def __hash__(self):
                                return 0
                    else:
                        @dataclass(unsafe_hash=unsafe_hash, eq=eq, frozen=frozen)
                        class C:
                            pass

                
                if result == 'fn':
                    
                    self.assertIn('__hash__', C.__dict__)
                    self.assertIsNotNone(C.__dict__['__hash__'])

                elif result == '':
                    
                    if not with_hash:
                        self.assertNotIn('__hash__', C.__dict__)

                elif result == 'none':
                    
                    self.assertIn('__hash__', C.__dict__)
                    self.assertIsNone(C.__dict__['__hash__'])

                elif result == 'exception':
                    
                    
                    assert(with_hash)
                    with self.assertRaisesRegex(TypeError, 'Cannot overwrite attribute __hash__'):
                        @dataclass(unsafe_hash=unsafe_hash, eq=eq, frozen=frozen)
                        class C:
                            def __hash__(self):
                                return 0

                else:
                    assert False, f'unknown result {result!r}'

        
        
        
        
        
        
        for case, (unsafe_hash,  eq,    frozen, res_no_defined_hash, res_defined_hash) in enumerate([
                  (False,        False, False,  '',                  ''),
                  (False,        False, True,   '',                  ''),
                  (False,        True,  False,  'none',              ''),
                  (False,        True,  True,   'fn',                ''),
                  (True,         False, False,  'fn',                'exception'),
                  (True,         False, True,   'fn',                'exception'),
                  (True,         True,  False,  'fn',                'exception'),
                  (True,         True,  True,   'fn',                'exception'),
                  ], 1):
            test(case, unsafe_hash, eq, frozen, False, res_no_defined_hash)
            test(case, unsafe_hash, eq, frozen, True,  res_defined_hash)

            
            
            
            test(case, non_bool(unsafe_hash), non_bool(eq), non_bool(frozen), False, res_no_defined_hash)
            test(case, non_bool(unsafe_hash), non_bool(eq), non_bool(frozen), True,  res_defined_hash)


    def test_eq_only(self):
        
        
        
        

        @dataclass
        class C:
            i: int
            def __eq__(self, other):
                return self.i == other.i
        self.assertEqual(C(1), C(1))
        self.assertNotEqual(C(1), C(4))

        
        
        @dataclass(unsafe_hash=True)
        class C:
            i: int
            def __eq__(self, other):
                return self.i == other.i
        self.assertEqual(C(1), C(1.0))
        self.assertEqual(hash(C(1)), hash(C(1.0)))

        
        
        @dataclass(unsafe_hash=True, eq=True)
        class C:
            i: int
            def __eq__(self, other):
                return self.i == 3 and self.i == other.i
        self.assertEqual(C(3), C(3))
        self.assertNotEqual(C(1), C(1))
        self.assertEqual(hash(C(1)), hash(C(1.0)))

    def test_0_field_hash(self):
        @dataclass(frozen=True)
        class C:
            pass
        self.assertEqual(hash(C()), hash(()))

        @dataclass(unsafe_hash=True)
        class C:
            pass
        self.assertEqual(hash(C()), hash(()))

    def test_1_field_hash(self):
        @dataclass(frozen=True)
        class C:
            x: int
        self.assertEqual(hash(C(4)), hash((4,)))
        self.assertEqual(hash(C(42)), hash((42,)))

        @dataclass(unsafe_hash=True)
        class C:
            x: int
        self.assertEqual(hash(C(4)), hash((4,)))
        self.assertEqual(hash(C(42)), hash((42,)))

    def test_hash_no_args(self):
        
        
        
        

        class Base:
            def __hash__(self):
                return 301

        
        
        for frozen, eq,    base,   expected       in [
            (None,  None,  object, 'unhashable'),
            (None,  None,  Base,   'unhashable'),
            (None,  False, object, 'object'),
            (None,  False, Base,   'base'),
            (None,  True,  object, 'unhashable'),
            (None,  True,  Base,   'unhashable'),
            (False, None,  object, 'unhashable'),
            (False, None,  Base,   'unhashable'),
            (False, False, object, 'object'),
            (False, False, Base,   'base'),
            (False, True,  object, 'unhashable'),
            (False, True,  Base,   'unhashable'),
            (True,  None,  object, 'tuple'),
            (True,  None,  Base,   'tuple'),
            (True,  False, object, 'object'),
            (True,  False, Base,   'base'),
            (True,  True,  object, 'tuple'),
            (True,  True,  Base,   'tuple'),
            ]:

            with self.subTest(frozen=frozen, eq=eq, base=base, expected=expected):
                
                if frozen is None and eq is None:
                    @dataclass
                    class C(base):
                        i: int
                elif frozen is None:
                    @dataclass(eq=eq)
                    class C(base):
                        i: int
                elif eq is None:
                    @dataclass(frozen=frozen)
                    class C(base):
                        i: int
                else:
                    @dataclass(frozen=frozen, eq=eq)
                    class C(base):
                        i: int

                
                if expected == 'unhashable':
                    c = C(10)
                    with self.assertRaisesRegex(TypeError, 'unhashable type'):
                        hash(c)

                elif expected == 'base':
                    self.assertEqual(hash(C(10)), 301)

                elif expected == 'object':
                    
                    
                    
                    
                    self.assertIs(C.__hash__, object.__hash__)

                elif expected == 'tuple':
                    self.assertEqual(hash(C(42)), hash((42,)))

                else:
                    assert False, f'unknown value for expected={expected!r}'


class TestFrozen(unittest.TestCase):
    def test_frozen(self):
        @dataclass(frozen=True)
        class C:
            i: int

        c = C(10)
        self.assertEqual(c.i, 10)
        with self.assertRaises(FrozenInstanceError):
            c.i = 5
        self.assertEqual(c.i, 10)

    def test_frozen_empty(self):
        @dataclass(frozen=True)
        class C:
            pass

        c = C()
        self.assertFalse(hasattr(c, 'i'))
        with self.assertRaises(FrozenInstanceError):
            c.i = 5
        self.assertFalse(hasattr(c, 'i'))
        with self.assertRaises(FrozenInstanceError):
            del c.i

    def test_inherit(self):
        @dataclass(frozen=True)
        class C:
            i: int

        @dataclass(frozen=True)
        class D(C):
            j: int

        d = D(0, 10)
        with self.assertRaises(FrozenInstanceError):
            d.i = 5
        with self.assertRaises(FrozenInstanceError):
            d.j = 6
        self.assertEqual(d.i, 0)
        self.assertEqual(d.j, 10)

    def test_inherit_nonfrozen_from_empty_frozen(self):
        @dataclass(frozen=True)
        class C:
            pass

        with self.assertRaisesRegex(TypeError,
                                    'cannot inherit non-frozen dataclass from a frozen one'):
            @dataclass
            class D(C):
                j: int

    def test_inherit_frozen_mutliple_inheritance(self):
        @dataclass
        class NotFrozen:
            pass

        @dataclass(frozen=True)
        class Frozen:
            pass

        class NotDataclass:
            pass

        for bases in (
            (NotFrozen, Frozen),
            (Frozen, NotFrozen),
            (Frozen, NotDataclass),
            (NotDataclass, Frozen),
        ):
            with self.subTest(bases=bases):
                with self.assertRaisesRegex(
                    TypeError,
                    'cannot inherit non-frozen dataclass from a frozen one',
                ):
                    @dataclass
                    class NotFrozenChild(*bases):
                        pass

        for bases in (
            (NotFrozen, Frozen),
            (Frozen, NotFrozen),
            (NotFrozen, NotDataclass),
            (NotDataclass, NotFrozen),
        ):
            with self.subTest(bases=bases):
                with self.assertRaisesRegex(
                    TypeError,
                    'cannot inherit frozen dataclass from a non-frozen one',
                ):
                    @dataclass(frozen=True)
                    class FrozenChild(*bases):
                        pass

    def test_inherit_frozen_mutliple_inheritance_regular_mixins(self):
        @dataclass(frozen=True)
        class Frozen:
            pass

        class NotDataclass:
            pass

        class C1(Frozen, NotDataclass):
            pass
        self.assertEqual(C1.__mro__, (C1, Frozen, NotDataclass, object))

        class C2(NotDataclass, Frozen):
            pass
        self.assertEqual(C2.__mro__, (C2, NotDataclass, Frozen, object))

        @dataclass(frozen=True)
        class C3(Frozen, NotDataclass):
            pass
        self.assertEqual(C3.__mro__, (C3, Frozen, NotDataclass, object))

        @dataclass(frozen=True)
        class C4(NotDataclass, Frozen):
            pass
        self.assertEqual(C4.__mro__, (C4, NotDataclass, Frozen, object))

    def test_multiple_frozen_dataclasses_inheritance(self):
        @dataclass(frozen=True)
        class FrozenA:
            pass

        @dataclass(frozen=True)
        class FrozenB:
            pass

        class C1(FrozenA, FrozenB):
            pass
        self.assertEqual(C1.__mro__, (C1, FrozenA, FrozenB, object))

        class C2(FrozenB, FrozenA):
            pass
        self.assertEqual(C2.__mro__, (C2, FrozenB, FrozenA, object))

        @dataclass(frozen=True)
        class C3(FrozenA, FrozenB):
            pass
        self.assertEqual(C3.__mro__, (C3, FrozenA, FrozenB, object))

        @dataclass(frozen=True)
        class C4(FrozenB, FrozenA):
            pass
        self.assertEqual(C4.__mro__, (C4, FrozenB, FrozenA, object))

    def test_inherit_nonfrozen_from_empty(self):
        @dataclass
        class C:
            pass

        @dataclass
        class D(C):
            j: int

        d = D(3)
        self.assertEqual(d.j, 3)
        self.assertIsInstance(d, C)

    
    
    def test_inherit_nonfrozen_from_frozen(self):
        for intermediate_class in [True, False]:
            with self.subTest(intermediate_class=intermediate_class):
                @dataclass(frozen=True)
                class C:
                    i: int

                if intermediate_class:
                    class I(C): pass
                else:
                    I = C

                with self.assertRaisesRegex(TypeError,
                                            'cannot inherit non-frozen dataclass from a frozen one'):
                    @dataclass
                    class D(I):
                        pass

    def test_inherit_frozen_from_nonfrozen(self):
        for intermediate_class in [True, False]:
            with self.subTest(intermediate_class=intermediate_class):
                @dataclass
                class C:
                    i: int

                if intermediate_class:
                    class I(C): pass
                else:
                    I = C

                with self.assertRaisesRegex(TypeError,
                                            'cannot inherit frozen dataclass from a non-frozen one'):
                    @dataclass(frozen=True)
                    class D(I):
                        pass

    def test_inherit_from_normal_class(self):
        for intermediate_class in [True, False]:
            with self.subTest(intermediate_class=intermediate_class):
                class C:
                    pass

                if intermediate_class:
                    class I(C): pass
                else:
                    I = C

                @dataclass(frozen=True)
                class D(I):
                    i: int

            d = D(10)
            with self.assertRaises(FrozenInstanceError):
                d.i = 5

    def test_non_frozen_normal_derived(self):
        

        @dataclass(frozen=True)
        class D:
            x: int
            y: int = 10

        class S(D):
            pass

        s = S(3)
        self.assertEqual(s.x, 3)
        self.assertEqual(s.y, 10)
        s.cached = True

        
        with self.assertRaises(FrozenInstanceError):
            s.x = 5
        with self.assertRaises(FrozenInstanceError):
            s.y = 5
        self.assertEqual(s.x, 3)
        self.assertEqual(s.y, 10)
        self.assertEqual(s.cached, True)

        with self.assertRaises(FrozenInstanceError):
            del s.x
        self.assertEqual(s.x, 3)
        with self.assertRaises(FrozenInstanceError):
            del s.y
        self.assertEqual(s.y, 10)
        del s.cached
        self.assertFalse(hasattr(s, 'cached'))
        with self.assertRaises(AttributeError) as cm:
            del s.cached
        self.assertNotIsInstance(cm.exception, FrozenInstanceError)

    def test_non_frozen_normal_derived_from_empty_frozen(self):
        @dataclass(frozen=True)
        class D:
            pass

        class S(D):
            pass

        s = S()
        self.assertFalse(hasattr(s, 'x'))
        s.x = 5
        self.assertEqual(s.x, 5)

        del s.x
        self.assertFalse(hasattr(s, 'x'))
        with self.assertRaises(AttributeError) as cm:
            del s.x
        self.assertNotIsInstance(cm.exception, FrozenInstanceError)

    def test_overwriting_frozen(self):
        
        with self.assertRaisesRegex(TypeError,
                                    'Cannot overwrite attribute __setattr__'):
            @dataclass(frozen=True)
            class C:
                x: int
                def __setattr__(self):
                    pass

        with self.assertRaisesRegex(TypeError,
                                    'Cannot overwrite attribute __delattr__'):
            @dataclass(frozen=True)
            class C:
                x: int
                def __delattr__(self):
                    pass

        @dataclass(frozen=False)
        class C:
            x: int
            def __setattr__(self, name, value):
                self.__dict__['x'] = value * 2
        self.assertEqual(C(10).x, 20)

    def test_frozen_hash(self):
        @dataclass(frozen=True)
        class C:
            x: Any

        
        
        hash(C(3))

        
        with self.assertRaisesRegex(TypeError, 'unhashable type'):
            hash(C({}))


class TestSlots(unittest.TestCase):
    def test_simple(self):
        @dataclass
        class C:
            __slots__ = ('x',)
            x: Any

        
        
        
        with self.assertRaisesRegex(TypeError,
                                    r"__init__\(\) missing 1 required positional argument: 'x'"):
            C()

        
        c = C(10)
        self.assertEqual(c.x, 10)
        c.x = 5
        self.assertEqual(c.x, 5)

        
        with self.assertRaisesRegex(AttributeError, "'C' object has no attribute 'y'"):
            c.y = 5

    def test_derived_added_field(self):
        
        @dataclass
        class Base:
            __slots__ = ('x',)
            x: Any

        @dataclass
        class Derived(Base):
            x: int
            y: int

        d = Derived(1, 2)
        self.assertEqual((d.x, d.y), (1, 2))

        
        d.z = 10

    def test_generated_slots(self):
        @dataclass(slots=True)
        class C:
            x: int
            y: int

        c = C(1, 2)
        self.assertEqual((c.x, c.y), (1, 2))

        c.x = 3
        c.y = 4
        self.assertEqual((c.x, c.y), (3, 4))

        with self.assertRaisesRegex(AttributeError, "'C' object has no attribute 'z'"):
            c.z = 5

    def test_add_slots_when_slots_exists(self):
        with self.assertRaisesRegex(TypeError, '^C already specifies __slots__$'):
            @dataclass(slots=True)
            class C:
                __slots__ = ('x',)
                x: int

    def test_generated_slots_value(self):

        class Root:
            __slots__ = {'x'}

        class Root2(Root):
            __slots__ = {'k': '...', 'j': ''}

        class Root3(Root2):
            __slots__ = ['h']

        class Root4(Root3):
            __slots__ = 'aa'

        @dataclass(slots=True)
        class Base(Root4):
            y: int
            j: str
            h: str

        self.assertEqual(Base.__slots__, ('y', ))

        @dataclass(slots=True)
        class Derived(Base):
            aa: float
            x: str
            z: int
            k: str
            h: str

        self.assertEqual(Derived.__slots__, ('z', ))

        @dataclass
        class AnotherDerived(Base):
            z: int

        self.assertNotIn('__slots__', AnotherDerived.__dict__)

    def test_cant_inherit_from_iterator_slots(self):

        class Root:
            __slots__ = iter(['a'])

        class Root2(Root):
            __slots__ = ('b', )

        with self.assertRaisesRegex(
           TypeError,
            "^Slots of 'Root' cannot be determined"
        ):
            @dataclass(slots=True)
            class C(Root2):
                x: int

    def test_returns_new_class(self):
        class A:
            x: int

        B = dataclass(A, slots=True)
        self.assertIsNot(A, B)

        self.assertFalse(hasattr(A, "__slots__"))
        self.assertTrue(hasattr(B, "__slots__"))

    
    @dataclass(frozen=True, slots=True)
    class FrozenSlotsClass:
        foo: str
        bar: int

    @dataclass(frozen=True)
    class FrozenWithoutSlotsClass:
        foo: str
        bar: int

    def test_frozen_pickle(self):
        

        self.assertEqual(self.FrozenSlotsClass.__slots__, ("foo", "bar"))
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.subTest(proto=proto):
                obj = self.FrozenSlotsClass("a", 1)
                p = pickle.loads(pickle.dumps(obj, protocol=proto))
                self.assertIsNot(obj, p)
                self.assertEqual(obj, p)

                obj = self.FrozenWithoutSlotsClass("a", 1)
                p = pickle.loads(pickle.dumps(obj, protocol=proto))
                self.assertIsNot(obj, p)
                self.assertEqual(obj, p)

    @dataclass(frozen=True, slots=True)
    class FrozenSlotsGetStateClass:
        foo: str
        bar: int

        getstate_called: bool = field(default=False, compare=False)

        def __getstate__(self):
            object.__setattr__(self, 'getstate_called', True)
            return [self.foo, self.bar]

    @dataclass(frozen=True, slots=True)
    class FrozenSlotsSetStateClass:
        foo: str
        bar: int

        setstate_called: bool = field(default=False, compare=False)

        def __setstate__(self, state):
            object.__setattr__(self, 'setstate_called', True)
            object.__setattr__(self, 'foo', state[0])
            object.__setattr__(self, 'bar', state[1])

    @dataclass(frozen=True, slots=True)
    class FrozenSlotsAllStateClass:
        foo: str
        bar: int

        getstate_called: bool = field(default=False, compare=False)
        setstate_called: bool = field(default=False, compare=False)

        def __getstate__(self):
            object.__setattr__(self, 'getstate_called', True)
            return [self.foo, self.bar]

        def __setstate__(self, state):
            object.__setattr__(self, 'setstate_called', True)
            object.__setattr__(self, 'foo', state[0])
            object.__setattr__(self, 'bar', state[1])

    def test_frozen_slots_pickle_custom_state(self):
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.subTest(proto=proto):
                obj = self.FrozenSlotsGetStateClass('a', 1)
                dumped = pickle.dumps(obj, protocol=proto)

                self.assertTrue(obj.getstate_called)
                self.assertEqual(obj, pickle.loads(dumped))

        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.subTest(proto=proto):
                obj = self.FrozenSlotsSetStateClass('a', 1)
                obj2 = pickle.loads(pickle.dumps(obj, protocol=proto))

                self.assertTrue(obj2.setstate_called)
                self.assertEqual(obj, obj2)

        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.subTest(proto=proto):
                obj = self.FrozenSlotsAllStateClass('a', 1)
                dumped = pickle.dumps(obj, protocol=proto)

                self.assertTrue(obj.getstate_called)

                obj2 = pickle.loads(dumped)
                self.assertTrue(obj2.setstate_called)
                self.assertEqual(obj, obj2)

    def test_slots_with_default_no_init(self):
        
        @dataclass(slots=True)
        class A:
            a: str
            b: str = field(default='b', init=False)

        obj = A("a")
        self.assertEqual(obj.a, 'a')
        self.assertEqual(obj.b, 'b')

    def test_slots_with_default_factory_no_init(self):
        
        @dataclass(slots=True)
        class A:
            a: str
            b: str = field(default_factory=lambda:'b', init=False)

        obj = A("a")
        self.assertEqual(obj.a, 'a')
        self.assertEqual(obj.b, 'b')

    def test_slots_no_weakref(self):
        @dataclass(slots=True)
        class A:
            
            pass

        self.assertNotIn("__weakref__", A.__slots__)
        a = A()
        with self.assertRaisesRegex(TypeError,
                                    "cannot create weak reference"):
            weakref.ref(a)
        with self.assertRaises(AttributeError):
            a.__weakref__

    def test_slots_weakref(self):
        @dataclass(slots=True, weakref_slot=True)
        class A:
            a: int

        self.assertIn("__weakref__", A.__slots__)
        a = A(1)
        a_ref = weakref.ref(a)

        self.assertIs(a.__weakref__, a_ref)

    def test_slots_weakref_base_str(self):
        class Base:
            __slots__ = '__weakref__'

        @dataclass(slots=True)
        class A(Base):
            a: int

        
        self.assertIn("__weakref__", Base.__slots__)
        self.assertNotIn("__weakref__", A.__slots__)
        a = A(1)
        weakref.ref(a)

    def test_slots_weakref_base_tuple(self):
        
        
        class Base:
            __slots__ = ('__weakref__',)

        @dataclass(slots=True)
        class A(Base):
            a: int

        
        
        self.assertIn("__weakref__", Base.__slots__)
        self.assertNotIn("__weakref__", A.__slots__)
        a = A(1)
        weakref.ref(a)

    def test_weakref_slot_without_slot(self):
        with self.assertRaisesRegex(TypeError,
                                    "weakref_slot is True but slots is False"):
            @dataclass(weakref_slot=True)
            class A:
                a: int

    def test_weakref_slot_make_dataclass(self):
        A = make_dataclass('A', [('a', int),], slots=True, weakref_slot=True)
        self.assertIn("__weakref__", A.__slots__)
        a = A(1)
        weakref.ref(a)

        
        with self.assertRaisesRegex(TypeError,
                                    "weakref_slot is True but slots is False"):
            B = make_dataclass('B', [('a', int),], weakref_slot=True)

    def test_weakref_slot_subclass_weakref_slot(self):
        @dataclass(slots=True, weakref_slot=True)
        class Base:
            field: int

        
        @dataclass(slots=True, weakref_slot=True)
        class A(Base):
            ...

        
        
        self.assertIn("__weakref__", Base.__slots__)
        self.assertNotIn("__weakref__", A.__slots__)
        a = A(1)
        a_ref = weakref.ref(a)
        self.assertIs(a.__weakref__, a_ref)

    def test_weakref_slot_subclass_no_weakref_slot(self):
        @dataclass(slots=True, weakref_slot=True)
        class Base:
            field: int

        @dataclass(slots=True)
        class A(Base):
            ...

        
        
        self.assertIn("__weakref__", Base.__slots__)
        self.assertNotIn("__weakref__", A.__slots__)
        a = A(1)
        a_ref = weakref.ref(a)
        self.assertIs(a.__weakref__, a_ref)

    def test_weakref_slot_normal_base_weakref_slot(self):
        class Base:
            __slots__ = ('__weakref__',)

        @dataclass(slots=True, weakref_slot=True)
        class A(Base):
            field: int

        
        
        self.assertIn("__weakref__", Base.__slots__)
        self.assertNotIn("__weakref__", A.__slots__)
        a = A(1)
        a_ref = weakref.ref(a)
        self.assertIs(a.__weakref__, a_ref)


class TestDescriptors(unittest.TestCase):
    def test_set_name(self):
        

        
        class D:
            def __set_name__(self, owner, name):
                self.name = name + 'x'
            def __get__(self, instance, owner):
                if instance is not None:
                    return 1
                return self

        
        
        @dataclass
        class C:
            c: int=D()
        self.assertEqual(C.c.name, 'cx')

        
        
        
        @dataclass
        class C:
            c: int=field(default=D(), init=False)
        self.assertEqual(C.c.name, 'cx')
        self.assertEqual(C().c, 1)

    def test_non_descriptor(self):
        
        

        class D:
            def __set_name__(self, owner, name):
                self.name = name + 'x'

        @dataclass
        class C:
            c: int=field(default=D(), init=False)
        self.assertEqual(C.c.name, 'cx')

    def test_lookup_on_instance(self):
        
        class D:
            pass

        d = D()
        
        d.__set_name__ = Mock()

        
        @dataclass
        class C:
            i: int=field(default=d, init=False)

        self.assertEqual(d.__set_name__.call_count, 0)

    def test_lookup_on_class(self):
        
        class D:
            pass
        D.__set_name__ = Mock()

        
        @dataclass
        class C:
            i: int=field(default=D(), init=False)

        self.assertEqual(D.__set_name__.call_count, 1)

    def test_init_calls_set(self):
        class D:
            pass

        D.__set__ = Mock()

        @dataclass
        class C:
            i: D = D()

        
        D.__set__.reset_mock()
        c = C(5)
        self.assertEqual(D.__set__.call_count, 1)

    def test_getting_field_calls_get(self):
        class D:
            pass

        D.__set__ = Mock()
        D.__get__ = Mock()

        @dataclass
        class C:
            i: D = D()

        c = C(5)

        
        D.__get__.reset_mock()
        value = c.i
        self.assertEqual(D.__get__.call_count, 1)

    def test_setting_field_calls_set(self):
        class D:
            pass

        D.__set__ = Mock()

        @dataclass
        class C:
            i: D = D()

        c = C(5)

        
        D.__set__.reset_mock()
        c.i = 10
        self.assertEqual(D.__set__.call_count, 1)

    def test_setting_uninitialized_descriptor_field(self):
        class D:
            pass

        D.__set__ = Mock()

        @dataclass
        class C:
            i: D

        
        D.__set__.reset_mock()
        c = C(5)
        self.assertEqual(D.__set__.call_count, 0)

        
        
        c.i = D()
        c.i = 5
        self.assertEqual(D.__set__.call_count, 0)

    def test_default_value(self):
        class D:
            def __get__(self, instance: Any, owner: object) -> int:
                if instance is None:
                    return 100

                return instance._x

            def __set__(self, instance: Any, value: int) -> None:
                instance._x = value

        @dataclass
        class C:
            i: D = D()

        c = C()
        self.assertEqual(c.i, 100)

        c = C(5)
        self.assertEqual(c.i, 5)

    def test_no_default_value(self):
        class D:
            def __get__(self, instance: Any, owner: object) -> int:
                if instance is None:
                    raise AttributeError()

                return instance._x

            def __set__(self, instance: Any, value: int) -> None:
                instance._x = value

        @dataclass
        class C:
            i: D = D()

        with self.assertRaisesRegex(TypeError, 'missing 1 required positional argument'):
            c = C()

class TestStringAnnotations(unittest.TestCase):
    def test_classvar(self):
        
        
        
        
        
        for typestr in ('ClassVar[int]',
                        'ClassVar [int]',
                        ' ClassVar [int]',
                        'ClassVar',
                        ' ClassVar ',
                        'typing.ClassVar[int]',
                        'typing.ClassVar[str]',
                        ' typing.ClassVar[str]',
                        'typing .ClassVar[str]',
                        'typing. ClassVar[str]',
                        'typing.ClassVar [str]',
                        'typing.ClassVar [ str]',

                        
                        
                        'typing.ClassVar.[int]',
                        'typing.ClassVar+',
                        ):
            with self.subTest(typestr=typestr):
                @dataclass
                class C:
                    x: typestr

                
                C()

                
                
                self.assertNotIn('x', C.__dict__)

    def test_isnt_classvar(self):
        for typestr in ('CV',
                        't.ClassVar',
                        't.ClassVar[int]',
                        'typing..ClassVar[int]',
                        'Classvar',
                        'Classvar[int]',
                        'typing.ClassVarx[int]',
                        'typong.ClassVar[int]',
                        'dataclasses.ClassVar[int]',
                        'typingxClassVar[str]',
                        ):
            with self.subTest(typestr=typestr):
                @dataclass
                class C:
                    x: typestr

                
                self.assertEqual(C(10).x, 10)

    def test_initvar(self):
        
        
        for typestr in ('InitVar[int]',
                        'InitVar [int]'
                        ' InitVar [int]',
                        'InitVar',
                        ' InitVar ',
                        'dataclasses.InitVar[int]',
                        'dataclasses.InitVar[str]',
                        ' dataclasses.InitVar[str]',
                        'dataclasses .InitVar[str]',
                        'dataclasses. InitVar[str]',
                        'dataclasses.InitVar [str]',
                        'dataclasses.InitVar [ str]',

                        
                        
                        'dataclasses.InitVar.[int]',
                        'dataclasses.InitVar+',
                        ):
            with self.subTest(typestr=typestr):
                @dataclass
                class C:
                    x: typestr

                
                with self.assertRaisesRegex(AttributeError,
                                            "object has no attribute 'x'"):
                    C(1).x

    def test_isnt_initvar(self):
        for typestr in ('IV',
                        'dc.InitVar',
                        'xdataclasses.xInitVar',
                        'typing.xInitVar[int]',
                        ):
            with self.subTest(typestr=typestr):
                @dataclass
                class C:
                    x: typestr

                
                self.assertEqual(C(10).x, 10)

    def test_classvar_module_level_import(self):
        from test.test_dataclasses import dataclass_module_1
        from test.test_dataclasses import dataclass_module_1_str
        from test.test_dataclasses import dataclass_module_2
        from test.test_dataclasses import dataclass_module_2_str

        for m in (dataclass_module_1, dataclass_module_1_str,
                  dataclass_module_2, dataclass_module_2_str,
                  ):
            with self.subTest(m=m):
                
                
                
                if m.USING_STRINGS:
                    c = m.CV(10)
                else:
                    c = m.CV()
                self.assertEqual(c.cv0, 20)


                
                
                
                c = m.IV(0, 1, 2, 3, 4)

                for field_name in ('iv0', 'iv1', 'iv2', 'iv3'):
                    with self.subTest(field_name=field_name):
                        with self.assertRaisesRegex(AttributeError, f"object has no attribute '{field_name}'"):
                            
                            
                            getattr(c, field_name)

                if m.USING_STRINGS:
                    
                    self.assertIn('not_iv4', c.__dict__)
                    self.assertEqual(c.not_iv4, 4)
                else:
                    
                    
                    self.assertNotIn('not_iv4', c.__dict__)

    def test_text_annotations(self):
        from test.test_dataclasses import dataclass_textanno

        self.assertEqual(
            get_type_hints(dataclass_textanno.Bar),
            {'foo': dataclass_textanno.Foo})
        self.assertEqual(
            get_type_hints(dataclass_textanno.Bar.__init__),
            {'foo': dataclass_textanno.Foo,
             'return': type(None)})


ByMakeDataClass = make_dataclass('ByMakeDataClass', [('x', int)])
ManualModuleMakeDataClass = make_dataclass('ManualModuleMakeDataClass',
                                           [('x', int)],
                                           module=__name__)
WrongNameMakeDataclass = make_dataclass('Wrong', [('x', int)])
WrongModuleMakeDataclass = make_dataclass('WrongModuleMakeDataclass',
                                          [('x', int)],
                                          module='custom')

class TestMakeDataclass(unittest.TestCase):
    def test_simple(self):
        C = make_dataclass('C',
                           [('x', int),
                            ('y', int, field(default=5))],
                           namespace={'add_one': lambda self: self.x + 1})
        c = C(10)
        self.assertEqual((c.x, c.y), (10, 5))
        self.assertEqual(c.add_one(), 11)


    def test_no_mutate_namespace(self):
        
        ns = {}
        C = make_dataclass('C',
                           [('x', int),
                            ('y', int, field(default=5))],
                           namespace=ns)
        self.assertEqual(ns, {})

    def test_base(self):
        class Base1:
            pass
        class Base2:
            pass
        C = make_dataclass('C',
                           [('x', int)],
                           bases=(Base1, Base2))
        c = C(2)
        self.assertIsInstance(c, C)
        self.assertIsInstance(c, Base1)
        self.assertIsInstance(c, Base2)

    def test_base_dataclass(self):
        @dataclass
        class Base1:
            x: int
        class Base2:
            pass
        C = make_dataclass('C',
                           [('y', int)],
                           bases=(Base1, Base2))
        with self.assertRaisesRegex(TypeError, 'required positional'):
            c = C(2)
        c = C(1, 2)
        self.assertIsInstance(c, C)
        self.assertIsInstance(c, Base1)
        self.assertIsInstance(c, Base2)

        self.assertEqual((c.x, c.y), (1, 2))

    def test_init_var(self):
        def post_init(self, y):
            self.x *= y

        C = make_dataclass('C',
                           [('x', int),
                            ('y', InitVar[int]),
                            ],
                           namespace={'__post_init__': post_init},
                           )
        c = C(2, 3)
        self.assertEqual(vars(c), {'x': 6})
        self.assertEqual(len(fields(c)), 1)

    def test_class_var(self):
        C = make_dataclass('C',
                           [('x', int),
                            ('y', ClassVar[int], 10),
                            ('z', ClassVar[int], field(default=20)),
                            ])
        c = C(1)
        self.assertEqual(vars(c), {'x': 1})
        self.assertEqual(len(fields(c)), 1)
        self.assertEqual(C.y, 10)
        self.assertEqual(C.z, 20)

    def test_other_params(self):
        C = make_dataclass('C',
                           [('x', int),
                            ('y', ClassVar[int], 10),
                            ('z', ClassVar[int], field(default=20)),
                            ],
                           init=False)
        
        self.assertNotIn('__init__', vars(C))
        self.assertIn('__repr__', vars(C))

        
        with self.assertRaisesRegex(TypeError, 'unexpected keyword argument'):
            C = make_dataclass('C',
                               [],
                               xxinit=False)

    def test_no_types(self):
        C = make_dataclass('Point', ['x', 'y', 'z'])
        c = C(1, 2, 3)
        self.assertEqual(vars(c), {'x': 1, 'y': 2, 'z': 3})
        self.assertEqual(C.__annotations__, {'x': 'typing.Any',
                                             'y': 'typing.Any',
                                             'z': 'typing.Any'})

        C = make_dataclass('Point', ['x', ('y', int), 'z'])
        c = C(1, 2, 3)
        self.assertEqual(vars(c), {'x': 1, 'y': 2, 'z': 3})
        self.assertEqual(C.__annotations__, {'x': 'typing.Any',
                                             'y': int,
                                             'z': 'typing.Any'})

    def test_module_attr(self):
        self.assertEqual(ByMakeDataClass.__module__, __name__)
        self.assertEqual(ByMakeDataClass(1).__module__, __name__)
        self.assertEqual(WrongModuleMakeDataclass.__module__, "custom")
        Nested = make_dataclass('Nested', [])
        self.assertEqual(Nested.__module__, __name__)
        self.assertEqual(Nested().__module__, __name__)

    def test_pickle_support(self):
        for klass in [ByMakeDataClass, ManualModuleMakeDataClass]:
            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                with self.subTest(proto=proto):
                    self.assertEqual(
                        pickle.loads(pickle.dumps(klass, proto)),
                        klass,
                    )
                    self.assertEqual(
                        pickle.loads(pickle.dumps(klass(1), proto)),
                        klass(1),
                    )

    def test_cannot_be_pickled(self):
        for klass in [WrongNameMakeDataclass, WrongModuleMakeDataclass]:
            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                with self.subTest(proto=proto):
                    with self.assertRaises(pickle.PickleError):
                        pickle.dumps(klass, proto)
                    with self.assertRaises(pickle.PickleError):
                        pickle.dumps(klass(1), proto)

    def test_invalid_type_specification(self):
        for bad_field in [(),
                          (1, 2, 3, 4),
                          ]:
            with self.subTest(bad_field=bad_field):
                with self.assertRaisesRegex(TypeError, r'Invalid field: '):
                    make_dataclass('C', ['a', bad_field])

        
        for bad_field in [float,
                          lambda x:x,
                          ]:
            with self.subTest(bad_field=bad_field):
                with self.assertRaisesRegex(TypeError, r'has no len\(\)'):
                    make_dataclass('C', ['a', bad_field])

    def test_duplicate_field_names(self):
        for field in ['a', 'ab']:
            with self.subTest(field=field):
                with self.assertRaisesRegex(TypeError, 'Field name duplicated'):
                    make_dataclass('C', [field, 'a', field])

    def test_keyword_field_names(self):
        for field in ['for', 'async', 'await', 'as']:
            with self.subTest(field=field):
                with self.assertRaisesRegex(TypeError, 'must not be keywords'):
                    make_dataclass('C', ['a', field])
                with self.assertRaisesRegex(TypeError, 'must not be keywords'):
                    make_dataclass('C', [field])
                with self.assertRaisesRegex(TypeError, 'must not be keywords'):
                    make_dataclass('C', [field, 'a'])

    def test_non_identifier_field_names(self):
        for field in ['()', 'x,y', '*', '2@3', '', 'little johnny tables']:
            with self.subTest(field=field):
                with self.assertRaisesRegex(TypeError, 'must be valid identifiers'):
                    make_dataclass('C', ['a', field])
                with self.assertRaisesRegex(TypeError, 'must be valid identifiers'):
                    make_dataclass('C', [field])
                with self.assertRaisesRegex(TypeError, 'must be valid identifiers'):
                    make_dataclass('C', [field, 'a'])

    def test_underscore_field_names(self):
        
        
        make_dataclass('C', ['_', '_a', 'a_a', 'a_'])

    def test_funny_class_names_names(self):
        
        
        for classname in ['()', 'x,y', '*', '2@3', '']:
            with self.subTest(classname=classname):
                C = make_dataclass(classname, ['a', 'b'])
                self.assertEqual(C.__name__, classname)

class TestReplace(unittest.TestCase):
    def test(self):
        @dataclass(frozen=True)
        class C:
            x: int
            y: int

        c = C(1, 2)
        c1 = replace(c, x=3)
        self.assertEqual(c1.x, 3)
        self.assertEqual(c1.y, 2)

    def test_frozen(self):
        @dataclass(frozen=True)
        class C:
            x: int
            y: int
            z: int = field(init=False, default=10)
            t: int = field(init=False, default=100)

        c = C(1, 2)
        c1 = replace(c, x=3)
        self.assertEqual((c.x, c.y, c.z, c.t), (1, 2, 10, 100))
        self.assertEqual((c1.x, c1.y, c1.z, c1.t), (3, 2, 10, 100))


        with self.assertRaisesRegex(TypeError, 'init=False'):
            replace(c, x=3, z=20, t=50)
        with self.assertRaisesRegex(TypeError, 'init=False'):
            replace(c, z=20)
            replace(c, x=3, z=20, t=50)

        
        with self.assertRaisesRegex(FrozenInstanceError, "cannot assign to field 'x'"):
            c1.x = 3

        
        
        
        
        with self.assertRaisesRegex(TypeError, r"__init__\(\) got an unexpected "
                                             "keyword argument 'a'"):
            c1 = replace(c, x=20, a=5)

    def test_invalid_field_name(self):
        @dataclass(frozen=True)
        class C:
            x: int
            y: int

        c = C(1, 2)
        with self.assertRaisesRegex(TypeError, r"__init__\(\) got an unexpected "
                                    "keyword argument 'z'"):
            c1 = replace(c, z=3)

    def test_invalid_object(self):
        @dataclass(frozen=True)
        class C:
            x: int
            y: int

        with self.assertRaisesRegex(TypeError, 'dataclass instance'):
            replace(C, x=3)

        with self.assertRaisesRegex(TypeError, 'dataclass instance'):
            replace(0, x=3)

    def test_no_init(self):
        @dataclass
        class C:
            x: int
            y: int = field(init=False, default=10)

        c = C(1)
        c.y = 20

        
        c1 = replace(c, x=5)
        self.assertEqual((c1.x, c1.y), (5, 10))

        
        with self.assertRaisesRegex(TypeError, 'init=False'):
            replace(c, x=2, y=30)

        with self.assertRaisesRegex(TypeError, 'init=False'):
            replace(c, y=30)

    def test_classvar(self):
        @dataclass
        class C:
            x: int
            y: ClassVar[int] = 1000

        c = C(1)
        d = C(2)

        self.assertIs(c.y, d.y)
        self.assertEqual(c.y, 1000)

        
        with self.assertRaisesRegex(TypeError, r"__init__\(\) got an "
                                    "unexpected keyword argument 'y'"):
            replace(c, y=30)

        replace(c, x=5)

    def test_initvar_is_specified(self):
        @dataclass
        class C:
            x: int
            y: InitVar[int]

            def __post_init__(self, y):
                self.x *= y

        c = C(1, 10)
        self.assertEqual(c.x, 10)
        with self.assertRaisesRegex(TypeError, r"InitVar 'y' must be "
                                    r"specified with replace\(\)"):
            replace(c, x=3)
        c = replace(c, x=3, y=5)
        self.assertEqual(c.x, 15)

    def test_initvar_with_default_value(self):
        @dataclass
        class C:
            x: int
            y: InitVar[int] = None
            z: InitVar[int] = 42

            def __post_init__(self, y, z):
                if y is not None:
                    self.x += y
                if z is not None:
                    self.x += z

        c = C(x=1, y=10, z=1)
        self.assertEqual(replace(c), C(x=12))
        self.assertEqual(replace(c, y=4), C(x=12, y=4, z=42))
        self.assertEqual(replace(c, y=4, z=1), C(x=12, y=4, z=1))

    def test_recursive_repr(self):
        @dataclass
        class C:
            f: "C"

        c = C(None)
        c.f = c
        self.assertEqual(repr(c), "TestReplace.test_recursive_repr.<locals>.C(f=...)")

    def test_recursive_repr_two_attrs(self):
        @dataclass
        class C:
            f: "C"
            g: "C"

        c = C(None, None)
        c.f = c
        c.g = c
        self.assertEqual(repr(c), "TestReplace.test_recursive_repr_two_attrs"
                                  ".<locals>.C(f=..., g=...)")

    def test_recursive_repr_indirection(self):
        @dataclass
        class C:
            f: "D"

        @dataclass
        class D:
            f: "C"

        c = C(None)
        d = D(None)
        c.f = d
        d.f = c
        self.assertEqual(repr(c), "TestReplace.test_recursive_repr_indirection"
                                  ".<locals>.C(f=TestReplace.test_recursive_repr_indirection"
                                  ".<locals>.D(f=...))")

    def test_recursive_repr_indirection_two(self):
        @dataclass
        class C:
            f: "D"

        @dataclass
        class D:
            f: "E"

        @dataclass
        class E:
            f: "C"

        c = C(None)
        d = D(None)
        e = E(None)
        c.f = d
        d.f = e
        e.f = c
        self.assertEqual(repr(c), "TestReplace.test_recursive_repr_indirection_two"
                                  ".<locals>.C(f=TestReplace.test_recursive_repr_indirection_two"
                                  ".<locals>.D(f=TestReplace.test_recursive_repr_indirection_two"
                                  ".<locals>.E(f=...)))")

    def test_recursive_repr_misc_attrs(self):
        @dataclass
        class C:
            f: "C"
            g: int

        c = C(None, 1)
        c.f = c
        self.assertEqual(repr(c), "TestReplace.test_recursive_repr_misc_attrs"
                                  ".<locals>.C(f=..., g=1)")

    
    
    
    
    

    
    

    
    

    

class TestAbstract(unittest.TestCase):
    def test_abc_implementation(self):
        class Ordered(abc.ABC):
            @abc.abstractmethod
            def __lt__(self, other):
                pass

            @abc.abstractmethod
            def __le__(self, other):
                pass

        @dataclass(order=True)
        class Date(Ordered):
            year: int
            month: 'Month'
            day: 'int'

        self.assertFalse(inspect.isabstract(Date))
        self.assertGreater(Date(2020,12,25), Date(2020,8,31))

    def test_maintain_abc(self):
        class A(abc.ABC):
            @abc.abstractmethod
            def foo(self):
                pass

        @dataclass
        class Date(A):
            year: int
            month: 'Month'
            day: 'int'

        self.assertTrue(inspect.isabstract(Date))
        msg = "class Date without an implementation for abstract method 'foo'"
        self.assertRaisesRegex(TypeError, msg, Date)


class TestMatchArgs(unittest.TestCase):
    def test_match_args(self):
        @dataclass
        class C:
            a: int
        self.assertEqual(C(42).__match_args__, ('a',))

    def test_explicit_match_args(self):
        ma = ()
        @dataclass
        class C:
            a: int
            __match_args__ = ma
        self.assertIs(C(42).__match_args__, ma)

    def test_bpo_43764(self):
        @dataclass(repr=False, eq=False, init=False)
        class X:
            a: int
            b: int
            c: int
        self.assertEqual(X.__match_args__, ("a", "b", "c"))

    def test_match_args_argument(self):
        @dataclass(match_args=False)
        class X:
            a: int
        self.assertNotIn('__match_args__', X.__dict__)

        @dataclass(match_args=False)
        class Y:
            a: int
            __match_args__ = ('b',)
        self.assertEqual(Y.__match_args__, ('b',))

        @dataclass(match_args=False)
        class Z(Y):
            z: int
        self.assertEqual(Z.__match_args__, ('b',))

        
        
        @dataclass
        class A:
            a: int
            z: int
        @dataclass(match_args=False)
        class B(A):
            b: int
        self.assertEqual(B.__match_args__, ('a', 'z'))

    def test_make_dataclasses(self):
        C = make_dataclass('C', [('x', int), ('y', int)])
        self.assertEqual(C.__match_args__, ('x', 'y'))

        C = make_dataclass('C', [('x', int), ('y', int)], match_args=True)
        self.assertEqual(C.__match_args__, ('x', 'y'))

        C = make_dataclass('C', [('x', int), ('y', int)], match_args=False)
        self.assertNotIn('__match__args__', C.__dict__)

        C = make_dataclass('C', [('x', int), ('y', int)], namespace={'__match_args__': ('z',)})
        self.assertEqual(C.__match_args__, ('z',))


class TestKeywordArgs(unittest.TestCase):
    def test_no_classvar_kwarg(self):
        msg = 'field a is a ClassVar but specifies kw_only'
        with self.assertRaisesRegex(TypeError, msg):
            @dataclass
            class A:
                a: ClassVar[int] = field(kw_only=True)

        with self.assertRaisesRegex(TypeError, msg):
            @dataclass
            class A:
                a: ClassVar[int] = field(kw_only=False)

        with self.assertRaisesRegex(TypeError, msg):
            @dataclass(kw_only=True)
            class A:
                a: ClassVar[int] = field(kw_only=False)

    def test_field_marked_as_kwonly(self):
        
        
        @dataclass(kw_only=True)
        class A:
            a: int
        self.assertTrue(fields(A)[0].kw_only)

        @dataclass(kw_only=True)
        class A:
            a: int = field(kw_only=True)
        self.assertTrue(fields(A)[0].kw_only)

        @dataclass(kw_only=True)
        class A:
            a: int = field(kw_only=False)
        self.assertFalse(fields(A)[0].kw_only)

        
        
        @dataclass(kw_only=False)
        class A:
            a: int
        self.assertFalse(fields(A)[0].kw_only)

        @dataclass(kw_only=False)
        class A:
            a: int = field(kw_only=True)
        self.assertTrue(fields(A)[0].kw_only)

        @dataclass(kw_only=False)
        class A:
            a: int = field(kw_only=False)
        self.assertFalse(fields(A)[0].kw_only)

        
        
        @dataclass
        class A:
            a: int
        self.assertFalse(fields(A)[0].kw_only)

        @dataclass
        class A:
            a: int = field(kw_only=True)
        self.assertTrue(fields(A)[0].kw_only)

        @dataclass
        class A:
            a: int = field(kw_only=False)
        self.assertFalse(fields(A)[0].kw_only)

    def test_match_args(self):
        
        @dataclass(kw_only=True)
        class C:
            a: int
        self.assertEqual(C(a=42).__match_args__, ())

        @dataclass
        class C:
            a: int
            b: int = field(kw_only=True)
        self.assertEqual(C(42, b=10).__match_args__, ('a',))

    def test_KW_ONLY(self):
        @dataclass
        class A:
            a: int
            _: KW_ONLY
            b: int
            c: int
        A(3, c=5, b=4)
        msg = "takes 2 positional arguments but 4 were given"
        with self.assertRaisesRegex(TypeError, msg):
            A(3, 4, 5)


        @dataclass(kw_only=True)
        class B:
            a: int
            _: KW_ONLY
            b: int
            c: int
        B(a=3, b=4, c=5)
        msg = "takes 1 positional argument but 4 were given"
        with self.assertRaisesRegex(TypeError, msg):
            B(3, 4, 5)

        
        @dataclass
        class C:
            a: int
            _: KW_ONLY
            b: int
            c: int = field(kw_only=False)
        c = C(1, 2, b=3)
        self.assertEqual(c.a, 1)
        self.assertEqual(c.b, 3)
        self.assertEqual(c.c, 2)
        c = C(1, b=3, c=2)
        self.assertEqual(c.a, 1)
        self.assertEqual(c.b, 3)
        self.assertEqual(c.c, 2)
        c = C(1, b=3, c=2)
        self.assertEqual(c.a, 1)
        self.assertEqual(c.b, 3)
        self.assertEqual(c.c, 2)
        c = C(c=2, b=3, a=1)
        self.assertEqual(c.a, 1)
        self.assertEqual(c.b, 3)
        self.assertEqual(c.c, 2)

    def test_KW_ONLY_as_string(self):
        @dataclass
        class A:
            a: int
            _: 'dataclasses.KW_ONLY'
            b: int
            c: int
        A(3, c=5, b=4)
        msg = "takes 2 positional arguments but 4 were given"
        with self.assertRaisesRegex(TypeError, msg):
            A(3, 4, 5)

    def test_KW_ONLY_twice(self):
        msg = "'Y' is KW_ONLY, but KW_ONLY has already been specified"

        with self.assertRaisesRegex(TypeError, msg):
            @dataclass
            class A:
                a: int
                X: KW_ONLY
                Y: KW_ONLY
                b: int
                c: int

        with self.assertRaisesRegex(TypeError, msg):
            @dataclass
            class A:
                a: int
                X: KW_ONLY
                b: int
                Y: KW_ONLY
                c: int

        with self.assertRaisesRegex(TypeError, msg):
            @dataclass
            class A:
                a: int
                X: KW_ONLY
                b: int
                c: int
                Y: KW_ONLY

        
        @dataclass
        class A:
            a: int
            _: KW_ONLY
            b: int
            c: int = field(kw_only=True)

        
        @dataclass
        class A:
            a: int
            _: KW_ONLY
            b: int
            c: int
        @dataclass
        class B(A):
            _: KW_ONLY
            d: int

        
        with self.assertRaisesRegex(TypeError, msg):
            @dataclass
            class A:
                a: int
                _: KW_ONLY
                b: int
                c: int
            @dataclass
            class B(A):
                X: KW_ONLY
                d: int
                Y: KW_ONLY


    def test_post_init(self):
        @dataclass
        class A:
            a: int
            _: KW_ONLY
            b: InitVar[int]
            c: int
            d: InitVar[int]
            def __post_init__(self, b, d):
                raise CustomError(f'{b=} {d=}')
        with self.assertRaisesRegex(CustomError, 'b=3 d=4'):
            A(1, c=2, b=3, d=4)

        @dataclass
        class B:
            a: int
            _: KW_ONLY
            b: InitVar[int]
            c: int
            d: InitVar[int]
            def __post_init__(self, b, d):
                self.a = b
                self.c = d
        b = B(1, c=2, b=3, d=4)
        self.assertEqual(asdict(b), {'a': 3, 'c': 4})

    def test_defaults(self):
        
        @dataclass
        class A:
            a: int = 0
            _: KW_ONLY
            b: int
            c: int = 1
            d: int

        a = A(d=4, b=3)
        self.assertEqual(a.a, 0)
        self.assertEqual(a.b, 3)
        self.assertEqual(a.c, 1)
        self.assertEqual(a.d, 4)

        
        
        err_regex = "non-default argument 'z' follows default argument 'a'"
        with self.assertRaisesRegex(TypeError, err_regex):
            @dataclass
            class A:
                a: int = 0
                z: int
                _: KW_ONLY
                b: int
                c: int = 1
                d: int

    def test_make_dataclass(self):
        A = make_dataclass("A", ['a'], kw_only=True)
        self.assertTrue(fields(A)[0].kw_only)

        B = make_dataclass("B",
                           ['a', ('b', int, field(kw_only=False))],
                           kw_only=True)
        self.assertTrue(fields(B)[0].kw_only)
        self.assertFalse(fields(B)[1].kw_only)


if __name__ == '__main__':
    unittest.main()