U kg5@sddlZddlZddlZddlZddlZddlZddlZddlZddlZdddddddd d d d d g Z Gddde Z GdddZ e Z GdddZeZeiZGdddZedZedZedZdZdZdZedZGdddeZGdddedZGd ddZGd!d"d"Zeed#d#dd#dd$d%dZ d&d'Z!d(d)Z"dded*d+d,Z#d-d.Z$d/d0Z%d1d2Z&d3d4Z'd5d6Z(d7d8Z)d9d:Z*d;d<Z+d=d>Z,d?d@Z-dAdBZ.dCdDZ/dEdFZ0dGdHZ1dIdJZ2dKdLZ3dddde1de2de2e3e2e3e2e3e2e3dMZ4dNdOZ5ddd#d#d#dPdPdPdQdRdZ6dSdZ7dTdUZ8dVd Z9e:dWdXd Z;dYdZZd]d^Z?d_dd#d#d#dPdPdPd`dad Z@dbd ZAdceA_BdS)eN dataclassfieldFieldFrozenInstanceErrorInitVarMISSINGfieldsasdictastuplemake_dataclassreplace is_dataclassc@s eZdZdS)rN__name__ __module__ __qualname__rr!/usr/lib/python3.8/dataclasses.pyrsc@seZdZddZdS)_HAS_DEFAULT_FACTORY_CLASScCsdS)Nz rselfrrr__repr__sz#_HAS_DEFAULT_FACTORY_CLASS.__repr__N)rrrrrrrrrsrc@s eZdZdS) _MISSING_TYPENrrrrrrsrc@seZdZddZddZdS) _FIELD_BASEcCs ||_dSNnamerrrrr__init__sz_FIELD_BASE.__init__cCs|jSrrrrrrrsz_FIELD_BASE.__repr__N)rrrrrrrrrrsr_FIELD_FIELD_CLASSVAR_FIELD_INITVARZ__dataclass_fields__Z__dataclass_params__Z __post_init__z^(?:\s*(\w+)\s*\.)?\s*(\w+)c@seZdZddZdS) _InitVarMetacCst|Sr)r)rZparamsrrr __getitem__sz_InitVarMeta.__getitem__N)rrrr#rrrrr"sr"c@s eZdZdZddZddZdS)rtypecCs ||_dSrr$)rr%rrrrszInitVar.__init__cCs,t|jtr|jj}n t|j}d|dS)Nzdataclasses.InitVar[]) isinstancer%rrepr)rZ type_namerrrrs   zInitVar.__repr__Nrrr __slots__rrrrrrrs) metaclassc@s(eZdZdZddZddZddZdS) r) rr%defaultdefault_factoryr(hashinitcomparemetadata _field_typecCsRd|_d|_||_||_||_||_||_||_|dkrr?r@rArrrr*s z_DataclassParams.__init__c Cs6d|jd|jd|jd|jd|jd|jd S)Nz_DataclassParams(init=r6z,eq=z,order=z ,unsafe_hash=z,frozen=r7r=rrrrr2sz_DataclassParams.__repr__Nr)rrrrr<!sr<Tr,r-r/r(r.r0r1cCs,|tk r|tk rtdt|||||||S)aReturn an object to identify dataclass fields. default is the default value of the field. default_factory is a 0-argument function called to initialize a field's value. If init is True, the field will be a parameter to the class's __init__() function. If repr is True, the field will be included in the object's repr(). If hash is True, the field will be included in the object's hash(). If compare is True, the field will be used in comparison functions. metadata, if specified, must be a mapping which is stored but not otherwise examined by dataclass. It is an error to specify both default and default_factory. z/cannot specify both default and default_factory)r ValueErrorrrBrrrr@s cs(|sdSddfdd|DdS)N()(,csg|]}d|jqS).r.0fobj_namerr _sz_tuple_str..,))join)rLrrrKr _tuple_strVsrPcs"ttfdd}|S)Nc sDt|tf}|krdS|z |}W5|X|S)Nz...)id_thread get_identadddiscard)rkeyresult repr_running user_functionrrwrapperis   z _recursive_repr..wrapper)set functoolswraps)rZr[rrXr_recursive_reprds r_)globalslocals return_typec Cs|dkr i}d|krt|d<d}|tk r4||d<d}d|}ddd|D}d |d |d |d |}d |}d|d|d|}i} t||| | df|S)NZBUILTINSZ _return_typez->_return_typerF css|]}d|VqdS)z Nr)rIbrrr sz_create_fn..z def rEr7z: , zdef __create_fn__(z): z return Z __create_fn__)builtinsrrOkeysexec) rargsZbodyr`rarbZreturn_annotationZtxtZ local_varsnsrrr _create_fnws   rmcCs0|rd|d|d|dS|d|d|S)NzBUILTINS.object.__setattr__(rFr7rG=r)rArvalue self_namerrr _field_assignsrqcCsd|j}|jtk rV|jr@|j||<|d|jd|j}q|j||<|d}n8|jr|jtkrn|j}q|jtk r|j||<|j}ndS|jtkrdSt||j||S)NZ_dflt_z() if z is _HAS_DEFAULT_FACTORY else rD)rr-rr/r,r2r!rq)rJrAr`rpZ default_namerorrr _field_inits"         rrcCsV|jtkr|jtkrd}n&|jtk r2d|j}n|jtk r@d}|jd|j|S)Nrcz=_dflt_z=_HAS_DEFAULT_FACTORYz:_type_)r,rr-r)rJr,rrr _init_params  rsc Csd}|D]:}|jr|jtkr&|jtks,d}q|rtd|jdqdd|D}|ttdg}|D] }t||||} | rj| | qj|rd d d |D} | |d t d | d |sdg}t d|gdd|D|||ddS)NFTznon-default argument z follows default argumentcSsi|]}d|j|jqS)Z_type_)rr%rHrrr s z_init_fn..)r_HAS_DEFAULT_FACTORYrFcss|]}|jtkr|jVqdSr)r2r!rrHrrrrf s z_init_fn..rGrEr7passrcSsg|]}|jrt|qSr)r/rsrHrrrrMsz_init_fn..)rar`rb) r/r,rr- TypeErrorrupdaterurrappendrO_POST_INIT_NAMErm) rrA has_post_initrpr`Z seen_defaultrJraZ body_lineslineZ params_strrrr_init_fns: r}cCs2tdddddd|Ddg|d}t|S) Nrrz(return self.__class__.__qualname__ + f"(rgcSs g|]}|jd|jdqS)z={self.z!r}rrHrrrrMsz_repr_fn..z)"r`)rmrOr_)rr`fnrrr_repr_fns rcCsp|td}|r,dddd|Dd}nd}tdd d |d d d f||dtddd |d ddf||dfS)N)clsrrErFcss|]}t|jVqdSr)r(rrHrrrrf(sz'_frozen_get_del_attr..rNrD __setattr__)rrroz if type(self) is cls or name in :z> raise FrozenInstanceError(f"cannot assign to field {name!r}")z)super(cls, self).__setattr__(name, value))rar` __delattr__rz; raise FrozenInstanceError(f"cannot delete field {name!r}")z"super(cls, self).__delattr__(name))rrOrm)rrr`raZ fields_strrrr_frozen_get_del_attr$s2  rcCs$t|ddd|||dg|dS)N)rotherz%if other.__class__ is self.__class__:z return zreturn NotImplementedr~)rm)rop self_tuple other_tupler`rrr_cmp_fn=srcCs$td|}tddd|dg|dS)Nr__hash__rz return hash(r7r~)rPrm)rr`rrrr_hash_fnKs   rcCs$||jkp"t||jko"|j|jkSr)ClassVarr%Z _GenericAliasZ __origin__)a_typetypingrrr _is_classvarSs  rcCs||jkpt||jkSr)rr%)r dataclassesrrr _is_initvar[s  rc Cst|}|rd}|d}|s2tj|jj}n2tj|j}|rd|j||krdtj|jj}|r|||d|rdSdS)NTF)_MODULE_IDENTIFIER_REmatchgroupsysmodulesgetr__dict__) Z annotationrZa_modulerZis_type_predicaterrlZ module_namemodulerrr_is_typebs)  rcCs8t||t}t|tr|}nt|tjr,t}t|d}||_||_t |_ t j d}|rt||st|jtrt|j|||jtrt|_ |j t krt j t}t||st|jtrt|j|||jtrt|_ |j ttfkr|jtk rtd|jd|j t kr4t|jtttfr4tdt|jd|jd|S)N)r,rfield z cannot have a default factoryzmutable default z for field z$ is not allowed: use default_factory)r9rr'rr4MemberDescriptorTyperrr%rr2rrrrstrrrr rrrr!r-rwr,listdictr\rC)rZa_namerr,rJrrrrr _get_fieldsF             rcCs||jkrdSt|||dS)NTF)rsetattr)rrrorrr_set_new_attributes  rcCsdSrrrrr`rrr_hash_set_nonesrcCsdd|D}t||S)NcSs(g|] }|jdkr|jrn|jr|qSr)r.r0rHrrrrMs  z_hash_add..)r)rrr`fldsrrr _hash_addsrcCstd|jdS)Nz-Cannot overwrite attribute __hash__ in class )rwrrrrr_hash_exceptionsr))FFFF)FFFT)FFTF)FFTT)FTFF)FTFT)FTTF)FTTT)TFFF)TFFT)TFTF)TFTT)TTFF)TTFT)TTTF)TTTTc si}jtjkr tjjj}ni}ttt||||||d} d} jdddD]D} t| t d} | dk rVd} | D]} | || j <qzt| tj rVd} qVj di}fdd|D}|D]L} | || j <tt| j dtr| jtkrt| j qt| j | jqĈjD].\}}t|tr||krt|dq| rz| rf|sftd | sz|rztd tt |j d t}|tkp|dkod jk }|r|std |rtt}dd| D}tdt|||d|krdnd|dd| D}|rHdd|D}tdt|||rdd|D}td|}td|}td td d|||d|rdd|D}td|}td|}dD]>\}}t|t|||||drtd|djdq|r8t||D].}t|j|rtd|jdjqtt |t |t ||f}|rh|||_!tdsjt"t#$%dd _&S)!NFrT__annotations__csg|]\}}t||qSr)r)rIrr%rrrrM]sz"_process_class..z& is a field but has no type annotationz5cannot inherit non-frozen dataclass from a frozen onez5cannot inherit frozen dataclass from a non-frozen oner__eq__z eq must be true if order is truecSsg|]}|jttfkr|qSr)r2rr!rHrrrrMsrrZ__dataclass_self__cSsg|]}|jtkr|qSrr2rrHrrrrMs cSsg|]}|jr|qSr)r(rHrrrrMsrcSsg|]}|jr|qSrr0rHrrrrMsrz==r~cSsg|]}|jr|qSrrrHrrrrMs))__lt__<)__le__z<=)__gt__>)__ge__z>=zCannot overwrite attribute z in class z). Consider using functools.total_ordering__doc__z -> Nonerc)'rrrrr_PARAMSr<__mro__r9_FIELDSvaluesrrAritemsr'rr,rdelattrrwrChasattrrzrr}rrPrrr _hash_actionboolrrinspectZ signaturer r)rr/r(r>r?r@rArr`Zany_frozen_baseZhas_dataclass_basesreZ base_fieldsrJZcls_annotationsZ cls_fieldsrroZ class_hashZhas_explicit_hashr{rZ field_listrrrrZ hash_actionrrr_process_class's                     rFr=cs*fdd}|dkr"|S||S)aReturns the same class as was passed in, with dunder methods added based on the fields defined in the class. Examines PEP 526 __annotations__ to determine fields. If init is true, an __init__() method is added to the class. If repr is true, a __repr__() method is added. If order is true, rich comparison dunder methods are added. If unsafe_hash is true, a __hash__() method function is added. If frozen is true, fields may not be assigned to after instance creation. cst|Sr)rrr>rAr/r?r(r@rrwrapszdataclass..wrapNr)rr/r(r>r?r@rArrrrrscCsBzt|t}Wntk r*tdYnXtdd|DS)zReturn a tuple describing the fields of this dataclass. Accepts a dataclass or an instance of one. Tuple elements are of type Field. z0must be called with a dataclass type or instancecss|]}|jtkr|VqdSrrrHrrrrf s zfields..)r9rAttributeErrorrwtupler)Zclass_or_instancerrrrrs cCstt|tS)z2Returns True if obj is an instance of a dataclass.)rr%r)objrrr_is_dataclass_instancesrcCs t|tr|nt|}t|tS)zEReturns True if obj is a dataclass or an instance of a dataclass.)r'r%rr)rrrrrr s dict_factorycCst|stdt||S)aReturn the fields of a dataclass instance as a new dictionary mapping field names to field values. Example usage: @dataclass class C: x: int y: int c = C(1, 2) assert asdict(c) == {'x': 1, 'y': 2} If given, 'dict_factory' will be used instead of built-in dict. The function applies recursively to field values that are dataclass instances. This will also look into built-in containers: tuples, lists, and dicts. z0asdict() should be called on dataclass instances)rrw _asdict_inner)rrrrrr scst|rDg}t|D]&}tt||j}||j|fq|St|trrt|drrt |fdd|DSt|t tfrt |fdd|DSt|t rt |fdd| DSt |SdS)N_fieldscsg|]}t|qSrrrIvrrrrMOsz!_asdict_inner..c3s|]}t|VqdSrrrrrrrfTsz _asdict_inner..c3s&|]\}}t|t|fVqdSrrrIkrrrrrfVs)rrrr9rryr'rrr%rrrcopydeepcopy)rrrWrJrorrrr4s  r tuple_factorycCst|stdt||S)aReturn the fields of a dataclass instance as a new tuple of field values. Example usage:: @dataclass class C: x: int y: int c = C(1, 2) assert astuple(c) == (1, 2) If given, 'tuple_factory' will be used instead of built-in tuple. The function applies recursively to field values that are dataclass instances. This will also look into built-in containers: tuples, lists, and dicts. z1astuple() should be called on dataclass instances)rrw_astuple_inner)rrrrrr ]scst|r>g}t|D] }tt||j}||q|St|trlt|drlt |fdd|DSt|t tfrt |fdd|DSt|t rt |fdd| DSt |SdS)Nrcsg|]}t|qSrrrrrrrMsz"_astuple_inner..c3s|]}t|VqdSrrrrrrrfsz!_astuple_inner..c3s&|]\}}t|t|fVqdSrrrrrrrfs)rrrr9rryr'rrr%rrrrr)rrrWrJrorrrrus   rr)bases namespacer/r(r>r?r@rAc sdkrint} i} |D]} t| tr<| } d}nDt| dkrR| \} }n.t| dkrr| \} }}|| <ntd| t| tr| std| t| rtd| | | krtd| | | || | <q$| d <t ||ifd d }t ||||||| d S) aReturn a new dynamically created dataclass. The dataclass name will be 'cls_name'. 'fields' is an iterable of either (name), (name, type) or (name, type, Field) objects. If type is omitted, use the string 'typing.Any'. Field objects are created by the equivalent of calling 'field(name, type [, Field-info])'. C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,)) is equivalent to: @dataclass class C(Base): x: 'typing.Any' y: int z: int = field(init=False) For the bases and namespace parameters, see the builtin type() function. The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to dataclass(). Nz typing.AnyrzInvalid field: z'Field names must be valid identifiers: z"Field names must not be keywords: zField name duplicated: rcs |Sr)rx)rlrrrz make_dataclass..r=) rr\r'rlenrw isidentifierkeyword iskeywordrTr4 new_classr)Zcls_namerrrr/r(r>r?r@rAseenZannsitemrtpspecrrrrr s:          cOst|dkr tdt|d|r,|\}n4d|krX|d}ddl}|jdtdd ntd t|sptd t|t D]v}|j t krq~|j s|j |kr~td |j d q~|j |kr~|j tkr|jtkrtd|j dt||j ||j <q~|jf|S)a,Return a new object replacing specified fields with new values. This is especially useful for frozen classes. Example usage: @dataclass(frozen=True) class C: x: int y: int c = C(1, 2) c1 = replace(c, x=3) assert c1.x == 3 and c1.y == 2 rz*replace() takes 1 positional argument but z were givenrrNz/Passing 'obj' as keyword argument is deprecatedr) stacklevelz7replace() missing 1 required positional argument: 'obj'z1replace() should be called on dataclass instancesrzC is declared with init=False, it cannot be specified with replace()zInitVar z! must be specified with replace())rrwpopwarningswarnDeprecationWarningrr9rrr2r r/rrCr!r,r __class__)rkZchangesrrrJrrrr s4     z(obj, /, **kwargs))N)Crerrr4rrrhr]rR__all__rrrrurrr5r3rrr r!rrrzcompilerr%r"rrr<rrPr_rmrqrrrsr}rrrrrrrrrrrrrrrrrr rr rrr rr r __text_signature__rrrrs  :   62 ;R>  ) B<