o Ƌ|cC @sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddlm Z m Z gdZ Gddde ZGdddZeZGdd d ZeZGd d d ZeZeiZGd d d ZedZedZedZdZdZdZedZGdddZ GdddZ!GdddZ"eedddddedddZ#dd 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__rr(/opt/conda/lib/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____dataclass_params__Z __post_init__z^(?:\s*(\w+)\s*\.)?\s*(\w+)c@s(eZdZdZddZddZddZdS) rtypecCr!r"r,)rr-rrrr&r'zInitVar.__init__cCs8t|jtrt|jts|jj}nt|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&rr3rrrrrs  rc@s0eZdZdZddZddZddZeeZ dS) r) r$r-defaultdefault_factoryr0hashinitcomparemetadatakw_only _field_typec CsXd|_d|_||_||_||_||_||_||_|durtnt ||_ ||_ d|_ dSr")r$r-r5r6r8r0r7r9_EMPTY_METADATAtypesMappingProxyTyper:r;r<) rr5r6r8r0r7r9r:r;rrrr& 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-r5r6r8r0r7r9r:r;r<rrrrrs.   zField.__repr__cCs,tt|jdd}|r||j||dSdS)N __set_name__)getattrr-r5)rownerr$funcrrrrB1szField.__set_name__N) rrrr4r&rrB classmethodrr3rrrrrs   rc@s eZdZdZddZddZdS)_DataclassParamsr8r0eqorder unsafe_hashfrozencCs(||_||_||_||_||_||_dSr"rH)rr8r0rIrJrKrLrrrr&Ds  z_DataclassParams.__init__c Cs6d|jd|jd|jd|jd|jd|jd S)Nz_DataclassParams(init=r@z,eq=z,order=z ,unsafe_hash=z,frozen=rArHrrrrrLsz_DataclassParams.__repr__N)rrrr4r&rrrrrrG;s rGTr5r6r8r0r7r9r:r;c 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 ValueErrorrrMrrrrZs rcCs$tdd|Dtdd|DfS)Ncss |] }|jr|js|VqdSr"r8r;.0frrr vz(_fields_in_init_order..css |] }|jr|jr|VqdSr"rOrPrrrrSwrT)tupler rrr_fields_in_init_orderrsrWcs(|sdSddfdd|DdS)N()(,csg|] }d|jqS).r#rPobj_namerr z_tuple_str..,))join)r]r rr\r _tuple_str{s rbcs"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)rlrmrrjr_recursive_reprs rq)globalslocals return_typec Cs|duri}d|vrt|d<d}|tur||d<d}d|}ddd|D}d |d |d |d |}d |}d|d|d|}i} t||| | ddi|S)NZBUILTINSZ _return_typez->_return_typerZ css|]}d|VqdS)z Nr)rQbrrrrSz_create_fn..z def rYrAz: , zdef __create_fn__(z): z return __create_fn__r)builtinsr rakeysexec) r$argsbodyrrrsrtreturn_annotationZtxt local_varsnsrrr _create_fns   rcCs0|rd|d|d|dS|d|d|S)NzBUILTINS.object.__setattr__(rZrAr[=r)rLr$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 rX)r$r6r r8r5r<r*r)rRrLrrrslots default_namerrrr _field_inits2          rcCsV|jtur |jtur d}n|jturd|j}n|jtur d}|jd|j|S)Nruz=_dflt_z=_HAS_DEFAULT_FACTORYz:_type_)r5r r6r$)rRr5rrr _init_params  rcCsd}|D]} | jr!| jtur| jtusd}q|r!td| jdqdd|D} | ttdg} |D]} t| || ||} | rF| | q5|rad d d |D} | |d t d | d | sfdg} dd|D}|r}|dg7}|dd|D7}t d|g|| | |ddS)NFTznon-default argument z follows default argumentcSsi|] }d|j|jqS)Z_type_)r$r-rPrrr %r_z_init_fn..)r _HAS_DEFAULT_FACTORYrZcss |] }|jtur|jVqdSr")r<r*r$rPrrrrS5s  z_init_fn..r[rYrApasscSg|]}t|qSrrrPrrrr^=z_init_fn..*cSrrrrPrrrr^Crr&)rsrrrt) r8r5r r6 TypeErrorr$updaterrappendra_POST_INIT_NAMEr)r Z std_fieldsZkw_only_fieldsrL has_post_initrrrrZ seen_defaultrRrsZ body_lineslineZ params_strZ _init_paramsrrr_init_fnsF   rcCs2tdddddd|Ddg|d}t|S) Nrrz(return self.__class__.__qualname__ + f"(rycSs g|] }|jd|jdqS)z={self.z!r}r#rPrrrr^Psz_repr_fn..z)"rr)rrarq)r rrfnrrr_repr_fnLs rcCsp|td}|rdddd|Dd}nd}tdd d |d d d f||dtddd |d ddf||dfS)N)r2rrYrZcss|]}t|jVqdSr")r0r$rPrrrrS[rxz'_frozen_get_del_attr..r`rX __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))rsrr __delattr__r%z; raise FrozenInstanceError(f"cannot delete field {name!r}")z"super(cls, self).__delattr__(name))rrar)r2r rrrsZ fields_strrrr_frozen_get_del_attrWs2  rcCs$t|ddd|||dg|dS)N)rotherz%if other.__class__ is self.__class__:z return zreturn NotImplementedr)r)r$op self_tuple other_tuplerrrrr_cmp_fnpsrcCs$td|}tddd|dg|dS)Nr__hash__rz return hash(rAr)rbr)r rrrrrr_hash_fn~s  rcCs$||jupt||juo|j|juSr")ClassVarr- _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__) annotationr2Za_modulerZis_type_predicaterr 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)r5rfield z cannot have a default factoryz$ is a ClassVar but specifies kw_onlyzmutable default z for field z$ is not allowed: use default_factory)rCr r/rr>MemberDescriptorTyperr$r-r(r<rrrrstrrrr)rrrr*r6rr;r5listdictrnrN)r2Za_namerZdefault_kw_onlyr5rRrrrrr _get_fieldsX               rcCs"t|tr|jd|j|_|S)Nr[)r/rrr)r2rrrr _set_qualname2s rcCs(||jvrdSt||t|||dS)NTF)rrsetattr)r2r$rrrr_set_new_attribute9s   rcCsdSr"rr2r rrrrr_hash_set_noneHrrcCsdd|D}t|t||S)NcSs(g|]}|jdur |jrn|jr|qSr")r7r9rPrrrr^Ls(z_hash_add..)rr)r2r rrfldsrrr _hash_addKsrcCstd|j)Nz-Cannot overwrite attribute __hash__ in class )rrrrrr_hash_exceptionOsr)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)r<r(r*rPrrrr^sz"_process_class..r&rZ__dataclass_self__cSsg|] }|jtur|qSrr<r(rPrrrr^scSg|]}|jr|qSr)r0rPrrrr^rcSrrr9rPrrrr^rrz==rcSrrrrPrrrr^$r))__lt__<)__le__z<=)__gt__>)__ge__z>=zCannot overwrite attribute z in class z). Consider using functools.total_ordering__doc__z -> Noneru__match_args__cs|]}|jVqdSr"r#rPrrrrSKz!_process_class..)1rrrrr_PARAMSrG__mro__rC_FIELDSvaluesr$rLrritemsrr/rrr rrrrr5r delattrrNrWhasattrrrrrrbrr _hash_actionboolrinspect signaturerrrU _add_slotsabcupdate_abstractmethods)%r2r8r0rIrJrKrL match_argsr;rr rrZany_frozen_baseZhas_dataclass_basesrwZ base_fieldsrRZcls_annotationsZ cls_fieldsZ KW_ONLY_seenrr$r-rZ class_hashZhas_explicit_hashZall_init_fieldsZstd_init_fieldsZkw_only_init_fieldsrZ field_listrrrrrZ hash_actionrrr_process_classrs                         rcsfddtDS)Ncsg|]}t|jqSr)rCr$rPrrrr^Zsz'_dataclass_getstate..rVrrrr_dataclass_getstateYsrcCs,tt||D] \}}t||j|qdSr")zipr objectrr$)rstaterrrrr_dataclass_setstate]srcCsd|jvr t|jdt|j}tddt|D}||d<|D]}||dq#|ddt|dd}t||j|j |}|durI||_ |rQt |_ t |_|S)Nr4z already specifies __slots__csrr"r#rPrrrrSmrz_add_slots..rr)rrrrrUr poprCr- __bases__rr __getstate__r __setstate__)r2 is_frozenZcls_dict field_names field_namequalnamerrrrcs    rF r8r0rIrJrKrLrr;rc 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)r2 rIrLr8r;rrJr0rrKrrwrapszdataclass..wrapNr) r2r8r0rIrJrKrLrr;rrrrrrsrcCs<zt|t}Wn tytdwtdd|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|VqdSr"rrPrrrrSszfields..)rCrAttributeErrorrrUr)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)rr2rrrrs  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|qSrrrQvrrrr^rz!_asdict_inner..c3|]}t|VqdSr"rr rrrrSrxz _asdict_inner..c3(|]\}}t|t|fVqdSr"rrQkr rrrrSs  )rr rrCr$rr/rUrr-rrrcopydeepcopy)rrrirRrrrrrs   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)Nrcrrrr rrrr^)rz"_astuple_inner..c3r r"rr rrrrS.rxz!_astuple_inner..c3r r"rr rrrrS0s)rr rrCr$rr/rUrr-rrrrr)rrrirRrrrrrs    rr) bases namespacer8r0rIrJrKrLrr;rc  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 annotationsdefaultsrrrexec_body_callbackos   z*make_dataclass..exec_body_callbackr) rnr/rlenr isidentifierkeyword iskeywordrfr> new_classr)cls_namer rrr8r0rIrJrKrLrr;rseenitemr$tpspecrr2rrrr6s:          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)rrrCrrr<r)r8r$rNr*r5r __class__)rZchangesrRrrrr~s   rr")Krerrr>rrr{rorrdrr__all__rrrrrr rr r?r=r r(r)r*rrrcompilerrrrGrrWrbrqrrrrrrrrrrrrrrrrrrrrrrrrrr rrrr rrUr rrrrrrrs  ?    <; ;_     h " ) H