o w[eF @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__rr"/usr/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_INITVAR__dataclass_fields____dataclass_params__ __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)r7r8rr5r_recursive_reprs r<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&rrDrrrrrs  rc@s4eZdZdZddZeddZddZee Z dS) r) r$r>defaultdefault_factoryrAhashinitcomparemetadatakw_only _field_typec CsXd|_d|_||_||_||_||_||_||_|durtnt ||_ ||_ d|_ dSr")r$r>rFrGrIrArHrJ_EMPTY_METADATAtypesMappingProxyTyperKrLrM) rrFrGrIrArHrJrKrLrrrr&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>rFrGrIrArHrJrKrLrMrrrrr.s.   zField.__repr__cCs,tt|jdd}|r||j||dSdS)N __set_name__)getattrr>rF)rownerr$funcrrrrSFszField.__set_name__N) rrrrEr&r<rrS classmethodrrDrrrrrs   rc@s eZdZdZddZddZdS)_DataclassParamsrIrAeqorder unsafe_hashfrozencCs(||_||_||_||_||_||_dSr"rY)rrIrArZr[r\r]rrrr&Ys  z_DataclassParams.__init__c Cs6d|jd|jd|jd|jd|jd|jd S)Nz_DataclassParams(init=rQz,eq=z,order=z ,unsafe_hash=z,frozen=rRrYrrrrrasz_DataclassParams.__repr__N)rrrrEr&rrrrrrXPs rXTrFrGrIrArHrJrKrLc 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 ValueErrorrr^rrrros rcCs$tdd|Dtdd|DfS)Ncss |] }|jr|js|VqdSr"rIrL.0frrr z(_fields_in_init_order..css |] }|jr|jr|VqdSr"r`rarrrrdre)tupler rrr_fields_in_init_ordersrhcs(|sdSddfdd|DdS)N()(,csg|] }d|jqS).r#raobj_namerr z_tuple_str..,))join)rnr rrmr _tuple_strs rs)globalslocals return_typec Cs|duri}d}|tur||d<d}d|}ddd|D}d|d |d |d |}d |}d |d|d|}i} t||| | ddi|S)N _return_typez->_return_typerk css|]}d|VqdS)z Nr)rbbrrrrdz_create_fn..z def rjrRz: , zdef __create_fn__(z): z return __create_fn__r)r rrkeysexec) r$argsbodyrtrurvreturn_annotationtxt local_varsnsrrr _create_fns  rcCs0|rd|d|d|dS|d|d|S)Nz*__dataclass_builtins_object__.__setattr__(rkrRrl=r)r]r$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)N_dflt_z() if z is _HAS_DEFAULT_FACTORY else ri)r$rGr rIrFrMr*r)rcr]rtrslots default_namerrrr _field_inits2          rcCsV|jtur |jtur d}n|jturd|j}n|jtur d}|jd|j|S)Nrwz=_dflt_z=_HAS_DEFAULT_FACTORYz:_type_)rFr rGr$)rcrFrrr _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)_type_)r$r>rarrr #rpz_init_fn..)r _HAS_DEFAULT_FACTORY__dataclass_builtins_object__rkcss |] }|jtur|jVqdSr")rMr*r$rarrrrd4s  z_init_fn..rlrjrRpasscSg|]}t|qSrrrarrrro<z_init_fn..*cSrrrrarrrroBrr&)rurtrv)rIrFr rG TypeErrorr$updaterobjectrappendrr_POST_INIT_NAMEr)r std_fieldskw_only_fieldsr] has_post_initrrtr seen_defaultrcru body_linesline params_str _init_paramsrrr_init_fnsH   rcCs2tdddddd|Ddg|d}t|S) Nrrz(return self.__class__.__qualname__ + f"(r|cSs g|] }|jd|jdqS)z={self.z!r}r#rarrrroOsz_repr_fn..z)"rt)rrrr<)r rtfnrrr_repr_fnKs rcCsp|td}|rdddd|Dd}nd}tdd d |d d d f||dtddd |d ddf||dfS)N)rCrrjrkcss|]}t|jVqdSr")rAr$rarrrrdZr{z'_frozen_get_del_attr..rqri __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))rurt __delattr__r%z; raise FrozenInstanceError(f"cannot delete field {name!r}")z"super(cls, self).__delattr__(name))rrrr)rCr rtru 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_tuplertrrr_cmp_fnosrcCs$td|}tddd|dg|dS)Nr__hash__rz return hash(rRr)rsr)r rtrrrr_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__) annotationrCa_moduleris_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)rFrfield z cannot have a default factoryz$ is a ClassVar but specifies kw_onlyzmutable default z for field z$ is not allowed: use default_factory)rTr r@rrOMemberDescriptorTyperr$r>r(rMrrrrstrrrr)rrrr*rGrrLrFlistdictr9r_)rCa_namerdefault_kw_onlyrFrcrrrrr _get_fieldsX               rcCs"t|tr|jd|j|_|S)Nrl)r@rrr)rCrrrr _set_qualname1s rcCs(||jvrdSt||t|||dS)NTF)rrsetattr)rCr$rrrr_set_new_attribute8s   rcCsdSr"rrCr rtrrr_hash_set_noneGrrcCsdd|D}t|t||S)NcSs(g|]}|jdur |jrn|jr|qSr")rHrJrarrrroKs(z_hash_add..)rr)rCr rtfldsrrr _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)rMr(r*rarrrrosz"_process_class..r&r__dataclass_self__cSsg|] }|jtur|qSrrMr(rarrrroscSg|]}|jr|qSr)rArarrrrorcSrrrJrarrrrorrz==rcSrrrrarrrro#r))__lt__<)__le__z<=)__gt__>)__ge__z>=zCannot overwrite attribute z in class z). Consider using functools.total_ordering__doc__z -> Nonerw__match_args__cs|]}|jVqdSr"r#rarrrrdJz!_process_class..)1rrrrr_PARAMSrX__mro__rT_FIELDSvaluesr$r]rritemsrr@rrr rrrrrFr delattrr_rhhasattrrrrrrsrr _hash_actionboolrinspect signaturerrrf _add_slotsabcupdate_abstractmethods)%rCrIrArZr[r\r] match_argsrLrr rtany_frozen_basehas_dataclass_basesrz base_fieldsrccls_annotations cls_fields KW_ONLY_seenrr$r>r class_hashhas_explicit_hashall_init_fieldsstd_init_fieldskw_only_init_fieldsr field_listrrrrr hash_actionrrr_process_classqs                         r csfddtDS)Ncsg|]}t|jqSr)rTr$rarrrroYsz'_dataclass_getstate..rgrrrr_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)NrEz already specifies __slots__csrr"r#rarrrrdlrz_add_slots..rr)rrrrrfr poprTr> __bases__rr __getstate__r __setstate__)rC is_frozencls_dict field_names field_namequalnamerrrrbs    rF rIrArZr[r\r]rrLrc 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 )rC rZr]rIrLrr[rArr\rrwrapszdataclass..wrapNr) rCrIrArZr[r\r]rrLrrrrrrsrcCs>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"rrarrrrdszfields..)rTrAttributeErrorrrfr)class_or_instancer rrrr s   r cCstt|tS)z2Returns True if obj is an instance of a dataclass.)rr>r)objrrr_is_dataclass_instancesr!cCs*t|tr t|ts |nt|}t|tS)zEReturns True if obj is a dataclass or an instance of a dataclass.)r@r>rrr)r rCrrrrs  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)r!r _asdict_inner)r r#rrrr  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|qSrr%rbvr"rrrorz!_asdict_inner..c3|]}t|VqdSr"r)r*r"rrrdr{z _asdict_inner..c3(|]\}}t|t|fVqdSr"r)rbkr+r"rrrds  )r!r r%rTr$rr@rfrr>rrrcopydeepcopy)r r#r4rcrrr"rr%s   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)r!r_astuple_inner)r r3rrrr r&r cst|rg}t|D]}tt||j}||q |St|tr6t|dr6t |fdd|DSt|t tfrJt |fdd|DSt|t r^t |fdd| DSt |S)Nr'cr(rr4r*r2rrro(rz"_astuple_inner..c3r,r"r5r*r2rrrd-r{z!_astuple_inner..c3r-r"r5r.r2rrrd/s)r!r r4rTr$rr@rfrr>rrrr0r1)r r3r4rcrrr2rr4s    r4r) bases namespacerIrArZr[r\r]rrLrc  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 annotationsdefaultsr7rrexec_body_callbackns   z*make_dataclass..exec_body_callbackr) r9r@rlenr isidentifierkeyword iskeywordr1rO new_classr)cls_namer r6r7rIrArZr[r\r]rrLrseenitemr$tpspecr<rCrr9rr5s:          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)r!rrTrrrMr)rIr$r_r*rFr __class__)r changesrcrrrr}s   rr")Krerr0rOrr?builtinsr:rr/rr__all__rrrrrr rr rPrNr r(r)r*rrrcompilerr<rrrXrrhrsrrrrrrrrrrrrrrrrrrrrr rrrrr r!rrr r%rfr r4rrrrrrs  @     << ;_     h " ) H