o ?OgF @sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddlm Z m Z gdZ Gddde ZGdddZeZGdd d ZeZGd d d ZeZeiZGd d d ZedZedZedZdZdZdZedZddZ GdddZ!GdddZ"GdddZ#eedddddeddd Z$d!d"Z%d#d$Z&dded%d&d'Z'd(d)Z(d*d+Z)d,d-Z*d.d/Z+d0d1Z,d2d3Z-d4d5Z.d6d7Z/d8d9Z0d:d;Z1dd?Z3d@dAZ4dBdCZ5dDdEZ6dFdGZ7dHdIZ8dJdKZ9idLddMddNddOddPe7dQddRe8dSddTe8dUe9dVe8dWe9dXe8dYe9dZe8d[e9Z:d\d]Z;d^d_Zd~ddddddddddddddde dfdgZ?dhdiZ@djdkZAdldmZBeCdndodpZDdqdrZEeFdsdtduZGdvdwZHdxddddddddddddddddy dzd{ZId|d}ZJdS)N) FunctionType GenericAlias) dataclassfieldFieldFrozenInstanceErrorInitVarKW_ONLYMISSINGfieldsasdictastuplemake_dataclassreplace is_dataclassc@ eZdZdS)rN__name__ __module__ __qualname__rr2/opt/alt/python310/lib64/python3.10/dataclasses.pyrs rc@seZdZddZdS)_HAS_DEFAULT_FACTORY_CLASScCsdS)Nz rselfrrr__repr__z#_HAS_DEFAULT_FACTORY_CLASS.__repr__N)rrrrrrrrrs rc@r) _MISSING_TYPENrrrrrrrc@r) _KW_ONLY_TYPENrrrrrrrrc@seZdZddZddZdS) _FIELD_BASEcC ||_dSNnamerr$rrr__init__ z_FIELD_BASE.__init__cCs|jSr"r#rrrrrsz_FIELD_BASE.__repr__N)rrrr&rrrrrr s r _FIELD_FIELD_CLASSVAR_FIELD_INITVARZ__dataclass_fields__Z__dataclass_params__Z __post_init__z^(?:\s*(\w+)\s*\.)?\s*(\w+)cs"ttfdd}|S)Nc sLt|tf}|vrdS|z |}W||S|w)Nz...)id_thread get_identadddiscard)rkeyresult repr_running user_functionrrwrappers    z _recursive_repr..wrapper)set functoolswraps)r4r5rr2r_recursive_reprs r9c@s(eZdZdZddZddZddZdS) rtypecCr!r"r:)rr;rrrr&r'zInitVar.__init__cCs8t|jtrt|jts|jj}nt|j}d|dS)Nzdataclasses.InitVar[]) isinstancer;rrrepr)rZ type_namerrrrs   zInitVar.__repr__cCst|Sr")r)clsr;rrr__class_getitem__szInitVar.__class_getitem__N)rrr __slots__r&rr@rrrrrs  rc@s4eZdZdZddZeddZddZee Z dS) r) r$r;defaultdefault_factoryr>hashinitcomparemetadatakw_only _field_typec CsXd|_d|_||_||_||_||_||_||_|durtnt ||_ ||_ d|_ dSr")r$r;rBrCrEr>rDrF_EMPTY_METADATAtypesMappingProxyTyperGrHrI) rrBrCrEr>rDrFrGrHrrrr&s zField.__init__cCs^d|jd|jd|jd|jd|jd|jd|jd|jd |jd |j d |j d S) Nz Field(name=z,type=z ,default=z,default_factory=z,init=,repr=z,hash=z ,compare=z ,metadata=z ,kw_only=z ,_field_type=)) r$r;rBrCrEr>rDrFrGrHrIrrrrr.s.   zField.__repr__cCs,tt|jdd}|r||j||dSdS)N __set_name__)getattrr;rB)rownerr$funcrrrrOFszField.__set_name__N) rrrrAr&r9rrO classmethodrr@rrrrrs   rc@s eZdZdZddZddZdS)_DataclassParamsrEr>eqorder unsafe_hashfrozencCs(||_||_||_||_||_||_dSr"rU)rrEr>rVrWrXrYrrrr&Ys  z_DataclassParams.__init__c Cs6d|jd|jd|jd|jd|jd|jd S)Nz_DataclassParams(init=rMz,eq=z,order=z ,unsafe_hash=z,frozen=rNrUrrrrrasz_DataclassParams.__repr__N)rrrrAr&rrrrrrTPs rTTrBrCrEr>rDrFrGrHc Cs.|tur |tur tdt||||||||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. If kw_only is true, the field will become a keyword-only parameter to __init__(). It is an error to specify both default and default_factory. z/cannot specify both default and default_factory)r ValueErrorrrZrrrros rcCs$tdd|Dtdd|DfS)Ncss |] }|jr|js|VqdSr"rErH.0frrr z(_fields_in_init_order..css |] }|jr|jr|VqdSr"r\r]rrrr`ra)tupler rrr_fields_in_init_ordersrdcs(|sdSddfdd|DdS)N()(,csg|] }d|jqS).r#r]obj_namerr z_tuple_str..,))join)rjr rrir _tuple_strs ro)globalslocals return_typec Cs|duri}d}|tur||d<d}d|}ddd|D}d|d |d |d |}d |}d |d|d|}i} t||| | ddi|S)NZ _return_typez->_return_typerg css|]}d|VqdS)z Nr)r^brrrr`z_create_fn..z def rfrNz: , zdef __create_fn__(z): z return Z __create_fn__r)r rnkeysexec) r$argsbodyrprqrrZreturn_annotationZtxtZ local_varsnsrrr _create_fns  r}cCs0|rd|d|d|dS|d|d|S)Nz*__dataclass_builtins_object__.__setattr__(rgrNrh=r)rYr$value self_namerrr _field_assignsrcCsd|j}|jtur+|jr |j||<|d|jd|j}n6|j||<|d}n+|jrE|jtur7|j}n|jturD|j||<|j}n|rT|jturT|j||<|}ndS|jtur]dSt||j||S)NZ_dflt_z() if z is _HAS_DEFAULT_FACTORY else re)r$rCr rErBrIr*r)r_rYrprslotsZ default_namerrrr _field_inits2          rcCsV|jtur |jtur d}n|jturd|j}n|jtur d}|jd|j|S)Nrsz=_dflt_z=_HAS_DEFAULT_FACTORYz:_type_)rBr rCr$)r_rBrrr _init_params  rcCsd}|D]} | jr!| jtur| jtusd}q|r!td| jdqdd|D} | tttdg} |D]} t | || ||} | rG| | q6|rbd d d |D} | |d t d | d | sgdg} dd|D}|r~|dg7}|dd|D7}t d|g|| | |ddS)NFTznon-default argument z follows default argumentcSsi|] }d|j|jqS)Z_type_)r$r;r]rrr #rlz_init_fn..)r _HAS_DEFAULT_FACTORYZ__dataclass_builtins_object__rgcss |] }|jtur|jVqdSr")rIr*r$r]rrrr`4s  z_init_fn..rhrfrNpasscSg|]}t|qSrrr]rrrrk<z_init_fn..*cSrrrr]rrrrkBrr&)rqrprr)rErBr rC TypeErrorr$updaterobjectrappendrn_POST_INIT_NAMEr})r Z std_fieldsZkw_only_fieldsrY has_post_initrrprZ seen_defaultr_rqZ body_lineslineZ params_strZ _init_paramsrrr_init_fnsH   rcCs2tdddddd|Ddg|d}t|S) Nrrz(return self.__class__.__qualname__ + f"(rwcSs g|] }|jd|jdqS)z={self.z!r}r#r]rrrrkOsz_repr_fn..z)"rp)r}rnr9)r rpfnrrr_repr_fnKs rcCsp|td}|rdddd|Dd}nd}tdd d |d d d f||dtddd |d ddf||dfS)N)r?rrfrgcss|]}t|jVqdSr")r>r$r]rrrr`Zrvz'_frozen_get_del_attr..rmre __setattr__)rr$rz 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))rqrp __delattr__r%z; raise FrozenInstanceError(f"cannot delete field {name!r}")z"super(cls, self).__delattr__(name))rrnr})r?r rprqZ fields_strrrr_frozen_get_del_attrVs2  rcCs$t|ddd|||dg|dS)N)rotherz%if other.__class__ is self.__class__:z return zreturn NotImplementedr)r})r$op self_tuple other_tuplerprrr_cmp_fnosrcCs$td|}tddd|dg|dS)Nr__hash__rz return hash(rNr)ror})r rprrrr_hash_fn}s  rcCs$||jupt||juo|j|juSr")ClassVarr;Z _GenericAlias __origin__)a_typetypingrrr _is_classvars  rcCs||jup t||juSr")rr;r dataclassesrrr _is_initvars  rcCs ||juSr")r rrrr _is_kw_onlyr'rc Cst|}|rAd}|d}|stj|jj}ntj|j}|r2|j||ur2tj|jj}|rA|||d|rAdSdS)NTF)_MODULE_IDENTIFIER_REmatchgroupsysmodulesgetr__dict__) annotationr?Za_modulerZis_type_predicaterr|Z module_namemodulerrr_is_types ) rcCs~t||t}t|tr|}n t|tjrt}t|d}||_||_t |_ t j d}|rDt||sAt|jtrDt|j|||jtrDt|_ |j t urft j t}t||sct|jtrft|j|||jtrft|_ |j ttfvr{|jtur{td|jd|j t tfvr|jtur||_n|j tusJ|jturtd|jd|j t urt|jtttfrtdt|jd|jd|S) N)rBrfield z cannot have a default factoryz$ is a ClassVar but specifies kw_onlyzmutable default z for field z$ is not allowed: use default_factory)rPr r=rrKMemberDescriptorTyperr$r;r(rIrrrrstrrrr)rrrr*rCrrHrBlistdictr6r[)r?Za_namerZdefault_kw_onlyrBr_rrrrr _get_fieldsX               rcCs"t|tr|jd|j|_|S)Nrh)r=rrr)r?rrrr _set_qualname1s rcCs(||jvrdSt||t|||dS)NTF)rrsetattr)r?r$rrrr_set_new_attribute8s   rcCsdSr"rr?r rprrr_hash_set_noneGrrcCsdd|D}t|t||S)NcSs(g|]}|jdur |jrn|jr|qSr")rDrFr]rrrrkKs(z_hash_add..)rr)r?r rpfldsrrr _hash_addJsrcCstd|j)Nz-Cannot overwrite attribute __hash__ in class )rrrrrr_hash_exceptionNsr)FFFF)FFFT)FFTF)FFTT)FTFF)FTFT)FTTF)FTTT)TFFF)TFFT)TFTF)TFTT)TTFF)TTFT)TTTF)TTTTc % Cs0i} |jtjvrtj|jj} ni} t|tt||||||d} d} |jdddD]"}t|t d}|durMd} | D]}|| |j <q=t|tj rMd} q+|j di}g}d}tjt}|D]/\}}t||syt|trt||||jtr|rt|dd}d}qb|t||||qb|D]%}|| |j <tt||j dtr|jturt||j qt||j |jq|jD]\}}t|tr||vrt|dq| r| r|std| s|rtd t|t | |j d t}|tup|duod |jv }|r |s td d d| D}t|\}}|r8t|t}t |dt!|||||d| vr2dnd| | dd| D}|rTdd|D}t |dt"|| |rudd|D}t#d|} t#d|}!t |d t$d d| |!| d|rdd|D}t#d|} t#d|}!dD]\}}"t ||t$||"| |!| drtd|d|jdq|rt%||| D]}#t ||#j|#rtd|#jd|jqt&t'|t'|t'||f}$|$r|$||| |_(t|ds|jtt)*|+dd |_,|r t |d!t-d"d#|D| rt.||}t/0||S)$NFrT__annotations__z3 is KW_ONLY, but KW_ONLY has already been specifiedz& 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)rIr(r*r]rrrrksz"_process_class..r&rZ__dataclass_self__cSsg|] }|jtur|qSrrIr(r]rrrrkscSg|]}|jr|qSr)r>r]rrrrkrcSrrrFr]rrrrkrrz==rcSrrrr]rrrrk#r))__lt__<)__le__z<=)__gt__>)__ge__z>=zCannot overwrite attribute z in class z). Consider using functools.total_ordering__doc__z -> Noners__match_args__cs|]}|jVqdSr"r#r]rrrr`Jz!_process_class..)1rrrrr_PARAMSrT__mro__rP_FIELDSvaluesr$rYrritemsrr=rrr rrrrrBr delattrr[rdhasattrrrrrrorr _hash_actionboolrinspectZ signaturerrrb _add_slotsabcupdate_abstractmethods)%r?rEr>rVrWrXrY match_argsrHrr rpZany_frozen_baseZhas_dataclass_basesruZ base_fieldsr_Zcls_annotationsZ cls_fieldsZ KW_ONLY_seenrr$r;rZ class_hashZhas_explicit_hashZall_init_fieldsZstd_init_fieldsZkw_only_init_fieldsrZ field_listrrrrrZ hash_actionrrr_process_classqs                         rcsfddtDS)Ncsg|]}t|jqSr)rPr$r]rrrrkYsz'_dataclass_getstate..rcrrrr_dataclass_getstateXsrcCs,tt||D] \}}t||j|qdSr")zipr rrr$)rstaterrrrr_dataclass_setstate\srcCsd|jvr t|jdt|j}tddt|D}||d<|D]}||dq#|ddt|dd}t||j|j |}|durI||_ |rQt |_ t |_|S)NrAz already specifies __slots__csrr"r#r]rrrr`lrz_add_slots..rr)rrrrrbr poprPr; __bases__rr __getstate__r __setstate__)r? is_frozenZcls_dict field_namesZ field_namequalnamerrrrbs    rF rEr>rVrWrXrYrrHrc s0f dd} |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. If match_args is true, the __match_args__ tuple is added. If kw_only is true, then by default all fields are keyword-only. If slots is true, an __slots__ attribute is added. c st| Sr")r)r? rVrYrErHrrWr>rrXrrwrapszdataclass..wrapNr) r?rEr>rVrWrXrYrrHrrrrrrsrcCs>zt|t}Wn tytddwtdd|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 instanceNcss|] }|jtur|VqdSr"rr]rrrr`szfields..)rPrAttributeErrorrrbr)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|tr t|ts |nt|}t|tS)zEReturns True if obj is a dataclass or an instance of a dataclass.)r=r;rrr)rr?rrrrs  r dict_factorycCt|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)rr _asdict_inner)rrrrrr  r cst|r"g}t|D]}tt||j}||j|fq |St|tr9t|dr9t |fdd|DSt|t tfrMt |fdd|DSt|t rat |fdd| DSt |S)N_fieldscg|]}t|qSrrr^vrrrrkrz!_asdict_inner..c3|]}t|VqdSr"rrrrrr`rvz _asdict_inner..c3(|]\}}t|t|fVqdSr"rr^krrrrr`s  )rr rrPr$rr=rbrr;rrrcopydeepcopy)rrr1r_rrrrrs   r tuple_factorycCr)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)rr_astuple_inner)rrrrrr rr cst|rg}t|D]}tt||j}||q |St|tr6t|dr6t |fdd|DSt|t tfrJt |fdd|DSt|t r^t |fdd| DSt |S)Nrcrrrrrrrrk(rz"_astuple_inner..c3rr"r rrrrr`-rvz!_astuple_inner..c3rr"r rrrrr`/s)rr rrPr$rr=rbrr;rrrrr)rrr1r_rrrrrs    rr) bases namespacerEr>rVrWrXrYrrHrc  sdurit} ii|D]^}t|tr|}d}n"t|dkr&|\}}nt|dkr6|\}}}||<ntd|t|trF|sMtd|t|rYtd||| vrdtd|| |||<qfd d }t ||i|}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: cs |||d<dS)Nr)r)r|Z annotationsdefaultsr rrexec_body_callbackns   z*make_dataclass..exec_body_callbackr) r6r=rlenr isidentifierkeyword iskeywordr.rK new_classr)cls_namer r r rEr>rVrWrXrYrrHrseenitemr$tpspecrr?rr rr5s:          rcKst|stdt|tD];}|jturq|js)|j|vr(t d|jdq|j|vrJ|jt urA|j t urAt d|jdt||j||j<q|j di|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()Nr)rrrPrrrIr)rEr$r[r*rBr __class__)rZchangesr_rrrr}s   rr")KrerrrKrrbuiltinsr7rr,rr__all__rrrrrr rr rLrJr r(r)r*rrrcompilerr9rrrTrrdror}rrrrrrrrrrrrrrrrrrrrrrrrr rrrr rrbr rrrrrrrs  @     << ;_     h " ) H