a DOg@stddlZddlZddlZddlZddlZddlZddlZddlZddlZddlm Z gdZ Gddde Z GdddZ e ZGdd d ZeZeiZGd d d Zed Zed ZedZdZdZdZedZGdddZGdddZGdddZeeddddddddZddZ dd Z!dded!d"d#Z"d$d%Z#d&d'Z$d(d)Z%d*d+Z&d,d-Z'd.d/Z(d0d1Z)d2d3Z*d4d5Z+d6d7Z,d8d9Z-d:d;Z.dd?Z0d@dAZ1dBdCZ2dddde0de1de1e2e1e2e1e2e1e2dDZ3dEdFZ4daddddGdGdGdHdIdJZ5dKdLZ6dMdNZ7dOdPZ8e9dQdRdSZ:dTdUZ;ed[dddddGdGdGd\d]d^Z?d_d`Z@dS)bN) GenericAlias) dataclassfieldFieldFrozenInstanceErrorInitVarMISSINGfieldsasdictastuplemake_dataclassreplace is_dataclassc@s eZdZdS)rN__name__ __module__ __qualname__rr#/usr/lib64/python3.9/dataclasses.pyrrc@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)rrrr rrrrrrsr_FIELD_FIELD_CLASSVAR_FIELD_INITVARZ__dataclass_fields__Z__dataclass_params__Z __post_init__z^(?:\s*(\w+)\s*\.)?\s*(\w+)c@s(eZdZdZddZddZddZdS) rtypecCs ||_dSrr$)rr%rrrr szInitVar.__init__cCs8t|jtr"t|jts"|jj}n t|j}d|dS)Nzdataclasses.InitVar[]) isinstancer%rrrepr)r type_namerrrrs  zInitVar.__repr__cCst|Sr)r)clsr%rrr__class_getitem__szInitVar.__class_getitem__N)rrr __slots__r rr+rrrrrsrc@s0eZdZdZddZddZddZeeZ dS) r) rr%defaultdefault_factoryr(hashinitcomparemetadata _field_typecCsRd|_d|_||_||_||_||_||_||_|dur#sr>Tr-r.r0r(r/r1r2cCs,|tur|turtdt|||||||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 ValueErrorrrDrrrrBs rcs(|sdSddfdd|DdS)N()(,csg|]}d|jqS).r.0fobj_namerr arz_tuple_str..,))join)rNr rrMr _tuple_strXsrRcs"ttfdd}|S)Nc sNt|tf}|vrdS|z|}W|n |0|S)Nz...)id_thread get_identadddiscard)rkeyresult repr_running user_functionrrwrapperks  z _recursive_repr..wrapper)set functoolswraps)r\r]rrZr_recursive_reprfs ra)globalslocals return_typec Cs|dur i}d|vrt|d<d}|tur4||d<d}d|}ddd|D}d |d |d |d |}d |}d|d|d|}i} t||| | dfi|S)NZBUILTINSZ _return_typez->_return_typerH css|]}d|VqdS)z Nr)rKbrrr rz_create_fn..z def rGr8z: , zdef __create_fn__(z): z return Z __create_fn__)builtinsrrQkeysexec) rargsbodyrbrcrdZreturn_annotationZtxtZ local_varsnsrrr _create_fnys   rpcCs0|rd|d|d|dS|d|d|S)NzBUILTINS.object.__setattr__(rHr8rI=r)rCrvalue self_namerrr _field_assignsrtcCsd|j}|jturV|jr@|j||<|d|jd|j}q|j||<|d}n8|jr|jturn|j}q|jtur|j||<|j}ndS|jturdSt||j||S)NZ_dflt_z() if z is _HAS_DEFAULT_FACTORY else rF)rr.rr0r-r3r#rt)rLrCrbrsZ default_namerrrrr _field_inits*         rucCsV|jtur|jturd}n&|jtur2d|j}n|jtur@d}|jd|j|S)Nrez=_dflt_z=_HAS_DEFAULT_FACTORYz:_type_)r-rr.r)rLr-rrr _init_params  rvc Csd}|D]:}|jr|jtur&|jtus,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%rJrrr rz_init_fn..)r_HAS_DEFAULT_FACTORYrHcss|]}|jtur|jVqdSr)r3r#rrJrrrrh s z_init_fn..rIrGr8passr cSsg|]}|jrt|qSr)r0rvrJrrrrOrz_init_fn..)rcrbrd) r0r-rr. TypeErrorrupdaterxruappendrQ_POST_INIT_NAMErp) r rC has_post_initrsrbZ seen_defaultrLrcZ body_lineslineZ params_strrrr_init_fns: rcCs2tdddddd|Ddg|d}t|S) Nrrz(return self.__class__.__qualname__ + f"(ricSs g|]}|jd|jdqS)z={self.z!r}rrJrrrrOsz_repr_fn..z)"rb)rprQra)r rbfnrrr_repr_fns rcCsp|td}|r,dddd|Dd}nd}tdd d |d d d f||dtddd |d ddf||dfS)N)r*rrGrHcss|]}t|jVqdSr)r(rrJrrrrh*rz'_frozen_get_del_attr..rPrF __setattr__)rrrrz 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))rcrb __delattr__rz; raise FrozenInstanceError(f"cannot delete field {name!r}")z"super(cls, self).__delattr__(name))rrQrp)r*r rbrcZ fields_strrrr_frozen_get_del_attr&s2  rcCs$t|ddd|||dg|dS)N)rotherz%if other.__class__ is self.__class__:z return zreturn NotImplementedr)rp)rop self_tuple other_tuplerbrrr_cmp_fn?srcCs$td|}tddd|dg|dS)Nr__hash__rz return hash(r8r)rRrp)r rbrrrr_hash_fnMs   rcCs$||jup"t||juo"|j|juSr)ClassVarr% _GenericAlias __origin__)a_typetypingrrr _is_classvarUs  rcCs||jupt||juSr)rr%)r dataclassesrrr _is_initvar]s  rc Cst|}|rd}|d}|s2tj|jj}n2tj|j}|rd|j||urdtj|jj}|r|||d|rdSdS)NTF)_MODULE_IDENTIFIER_REmatchgroupsysmodulesgetr__dict__) annotationr*Za_modulerZis_type_predicaterro module_namemodulerrr_is_typeds)  rcCs8t||t}t|tr|}nt|tjr,t}t|d}||_||_t |_ t j d}|rt||st|jtrt|j|||jtrt|_ |j t urt j t}t||st|jtrt|j|||jtrt|_ |j ttfvr|jturtd|jd|j t ur4t|jtttfr4tdt|jd|jd|S)N)r-rfield z cannot have a default factoryzmutable default z for field z$ is not allowed: use default_factory)r:rr'rr5MemberDescriptorTyperrr%r!r3rrrrstrrrr"rrrr#r.rzr-listdictr^rE)r*Za_namerr-rLrrrrr _get_fieldsJ              rcCs||jvrdSt|||dS)NTF)rsetattr)r*rrrrrr_set_new_attributes  rcCsdSrrr*r rbrrr_hash_set_nonesrcCsdd|D}t||S)NcSs(g|] }|jdur|jrn|jr|qSr)r/r1rJrrrrOrz_hash_add..)r)r*r rbfldsrrr _hash_addsrcCstd|jdS)Nz-Cannot overwrite attribute __hash__ in class )rzrrrrr_hash_exceptionsr))FFFF)FFFT)FFTF)FFTT)FTFF)FTFT)FTTF)FTTT)TFFF)TFFT)TFTF)TFTT)TTFF)TTFT)TTTF)TTTTc si}jtjvr tjjj}ni}ttt||||||d} d} jdddD]D} t| t d} | durVd} | D]} | || j <qzt| tj rVd} qVj di}fdd|D}|D]L} | || j <tt| j dtr| jturt| j qt| j | jqĈjD].\}}t|tr||vrt|dq| rz| rf|sftd | sz|rztd tt |j d t}|tup|duod jv }|r|std |rtt}dd| D}tdt|||d|vrdnd|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)rKrr%r*rrrO_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|]}|jttfvr|qSr)r3r!r#rJrrrrOsr rZ__dataclass_self__cSsg|]}|jtur|qSrr3r!rJrrrrOrcSsg|]}|jr|qSr)r(rJrrrrOrrcSsg|]}|jr|qSrr1rJrrrrOrrz==rcSsg|]}|jr|qSrrrJrrrrOr))__lt__<)__le__z<=)__gt__>)__ge__z>=zCannot overwrite attribute z in class z). Consider using functools.total_ordering__doc__z -> Nonere)'rrrrr_PARAMSr>__mro__r:_FIELDSvaluesrrCritemsr'rr-rdelattrrzrEhasattrr}rrrrRrrr _hash_actionboolrrinspectZ signaturer r)r*r0r(r@rArBrCr rbZany_frozen_baseZhas_dataclass_basesrgZ base_fieldsrLZcls_annotationsZ cls_fieldsrrrZ class_hashZhas_explicit_hashr~rZ field_listrrrrZ hash_actionrrr_process_class)s                        rFr?cs*fdd}|dur"|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@rCr0rAr(rBrrwrapszdataclass..wrapNr)r*r0r(r@rArBrCrrrrrsrcCs@zt|t}Wnty(tdYn0tdd|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|]}|jtur|VqdSrrrJrrrrhrzfields..)r:rAttributeErrorrztupler)Zclass_or_instancer rrrr s  r cCstt|tS)z2Returns True if obj is an instance of a dataclass.)rr%r)objrrr_is_dataclass_instancesrcCs*t|trt|ts|nt|}t|tS)zEReturns True if obj is a dataclass or an instance of a dataclass.)r'r%rrr)rr*rrrrs r 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)rrz _asdict_inner)rrrrrr sr cst|rDg}t|D]&}tt||j}||j|fq|St|trrt|drrt |fdd|DSt|t tfrt |fdd|DSt|t rt |fdd| DSt |SdS)N_fieldscsg|]}t|qSrrrKvrrrrOQrz!_asdict_inner..c3s|]}t|VqdSrrrrrrrhVrz _asdict_inner..c3s&|]\}}t|t|fVqdSrrrKkrrrrrhXs)rr rr:rr|r'rrr%rrrcopydeepcopy)rrrYrLrrrrrr6s  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)rrz_astuple_inner)rrrrrr _sr cst|r>g}t|D] }tt||j}||q|St|trlt|drlt |fdd|DSt|t tfrt |fdd|DSt|t rt |fdd| DSt |SdS)Nrcsg|]}t|qSrrrrrrrOrz"_astuple_inner..c3s|]}t|VqdSrrrrrrrhrz!_astuple_inner..c3s&|]\}}t|t|fVqdSrrrrrrrhs)rr rr:rr|r'rrr%rrrrr)rrrYrLrrrrrrws   rr)bases namespacer0r(r@rArBrCc sdurint} i} |D]} t| tr<| } d}nDt| dkrR| \} }n.t| dkrr| \} }}|| <ntd| t| tr| std| t| rtd| | | vrtd| | | || | <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)r{)rorrrrz make_dataclass..r?) rr^r'rlenrz isidentifierkeyword iskeywordrVr5 new_classr)cls_namer rrr0r(r@rArBrCseenZannsitemrtpspecr*rrrr s8          r cKst|stdt|tD]v}|jtur.q|jsR|j|vrt d|jdq|j|vr|jt ur|j t urt d|jdt||j||j<q|j fi|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 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())rrzr:rrr3r"r0rrEr#r-r __class__)rZchangesrLrrrr s   r )N)Arerrr5rrrjr_rTr__all__rrrrxrrr6r4rr!r"r#rrr}compilerrrr>rrRrarprtrurvrrrrrrrrrrrrrrrrr rrrr rrr rr r rrrrs   <   62 ;R>  ) B