B -_|@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/dEdFZ0dGdHZ1dIdJZ2dKdLZ3dddde1de2de2e3e2e3e2e3e2e3dMZ4dNdOZ5dcd#d#d#dPdPdPdQdRdZ6dSdZ7dTdUZ8dVd Z9e:dWdXd Z;dYdZZd]d^Z?d_dd#d#d#dPdPdPd`dad Z@dbd ZAdS)dN dataclassfieldFieldFrozenInstanceErrorInitVarMISSINGfieldsasdictastuplemake_dataclassreplace is_dataclassc@s eZdZdS)rN)__name__ __module__ __qualname__rr!/usr/lib/python3.7/dataclasses.pyrsc@seZdZddZdS)_HAS_DEFAULT_FACTORY_CLASScCsdS)Nz r)selfrrr__repr__sz#_HAS_DEFAULT_FACTORY_CLASS.__repr__N)rrrrrrrrrsrc@s eZdZdS) _MISSING_TYPEN)rrrrrrrrsrc@seZdZddZddZdS) _FIELD_BASEcCs ||_dS)N)name)rrrrr__init__sz_FIELD_BASE.__init__cCs|jS)N)r)rrrrrsz_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) _InitVarMetacCs|S)Nr)rZparamsrrr __getitem__sz_InitVarMeta.__getitem__N)rrrrrrrrrsrc@s eZdZdS)rN)rrrrrrrrs) metaclassc@s(eZdZdZddZddZddZdS) r) rtypedefaultdefault_factoryreprhashinitcomparemetadata _field_typecCsRd|_d|_||_||_||_||_||_||_|dkrTsz_tuple_str..z,))join)r=rr)r=r _tuple_strKsr@cs"ttfdd}|S)Nc sDt|tf}|krdS|z |}Wd|X|S)Nz...)id_thread get_identadddiscard)rkeyresult) repr_running user_functionrrwrapper^s   z _recursive_repr..wrapper)set functoolswraps)rIrJr)rHrIr_recursive_reprYs rN)globalslocals return_typecCs|dkr i}|dk r$d|kr$t|d<d}|tk r<||d<d}d|}ddd|D}d |d |d |d |}t|||||S) N __builtins__Z _return_typez->_return_typer9 css|]}d|VqdS) Nr)r;brrr }sz_create_fn..zdef r8r,z: )builtinsrr?exec)rargsZbodyrOrPrQZreturn_annotationZtxtrrr _create_fnls  r[cCs0|rd|d|d|dS|d|d|S)Nz __builtins__.object.__setattr__(r9r,r:=r)r6rvalue self_namerrr _field_assignsr_cCsd|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 z())rr"rr%r!r(rr_)r<r6rOr^Z default_namer]rrr _field_inits"         r`cCsV|jtkr|jtkrd}n&|jtk r2d|j}n|jtk r@d}|jd|j|S)NrSz=_dflt_z=_HAS_DEFAULT_FACTORYz:_type_)r!rr"r)r<r!rrr _init_params  rac Csd}xB|D]:}|jr |jtkr(|jtks.d}q |r td|jdq Wttd}g}x(|D] }t||||}|r\||q\W|rd dd|D} ||d t d | d |sd g}d d|D} t d|gdd|D|| |ddS)NFTznon-default argument z follows default argument)r_HAS_DEFAULT_FACTORYr9css|]}|jtkr|jVqdS)N)r(rr)r;r<rrrrWsz_init_fn..r:r8r,passcSsi|]}|jd|jqS)Z_type_)r r)r;r<rrr sz_init_fn..rcSsg|]}|jrt|qSr)r%ra)r;r<rrrr>sz_init_fn..)rPrOrQ) r%r!rr" TypeErrorrrbr`appendr?_POST_INIT_NAMEr[) rr6 has_post_initr^Z seen_defaultr<rOZ body_lineslineZ params_strrPrrr_init_fns4  rjcCs.tdddddd|Ddg}t|S)Nr)rz(return self.__class__.__qualname__ + f"(z, cSs g|]}|jd|jdqS)z={self.z!r})r)r;r<rrrr>sz_repr_fn..z)")r[r?rN)rfnrrr_repr_fn s  rlcCsl|td}|r,dddd|Dd}nd}tdd d |d d d f|dtddd |d ddf|dfS)N)clsrr8r9css|]}t|jVqdS)N)r#r)r;r<rrrrWsz'_frozen_get_del_attr..z,)z() __setattr__)rrr]z 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))rO __delattr__)rrz; raise FrozenInstanceError(f"cannot delete field {name!r}")z"super(cls, self).__delattr__(name))rr?r[)rmrrOZ fields_strrrr_frozen_get_del_attrs"  rqcCs t|ddd|||dgS)N)rotherz%if other.__class__ is self.__class__:z return zreturn NotImplemented)r[)rop self_tuple other_tuplerrr_cmp_fn.s rvcCs td|}tddd|dgS)Nr__hash__)rz return hash(r,)r@r[)rrtrrr_hash_fn;s rxcCs$||jkp"t||jko"|j|jkS)N)ClassVarr Z _GenericAliasZ __origin__)a_typetypingrrr _is_classvarBs r|cCs ||jkS)N)r)rz dataclassesrrr _is_initvarJsr~c Cst|}|rd}|d}|s2tj|jj}n2tj|j}|rd|j||krdtj|jj}|r|||d|rdSdS)NTF)_MODULE_IDENTIFIER_REmatchgroupsysmodulesgetr__dict__) Z annotationrmZa_modulerzZis_type_predicaternsZ module_namemodulerrr_is_typePs)  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!r{zfield z cannot have a default factoryzmutable default z for field z$ is not allowed: use default_factory)r.r isinstancerr*MemberDescriptorTyperrr rr(rrrr|strrryrrr~rrr"rer!listdictrKr7)rmZa_namerzr!r<r{r}rrr _get_fields:             rcCs||jkrdSt|||dS)NTF)rsetattr)rmrr]rrr_set_new_attributes  rcCsdS)Nr)rmrrrr_hash_set_nonesrcCsdd|D}t|S)NcSs(g|] }|jdkr|jrn|jr|qS)N)r$r&)r;r<rrrr>sz_hash_add..)rx)rmrfldsrrr _hash_addsrcCstd|jdS)Nz-Cannot overwrite attribute __hash__ in class )rer)rmrrrr_hash_exceptionsr))FFFF)FFFT)FFTF)FFTT)FTFF)FTFT)FTTF)FTTT)TFFF)TFFT)TFTF)TFTT)TTFF)TTFT)TTTF)TTTTc sxi}ttt||||||d}d} xXjdddD]D} t| td} | r8d} x| D]} | || j<qZWt| tjr8d}q8Wj di} fdd| D}xR|D]J} | || j<t t| jdt r| jtkrt| jqt| j| jqWx<j D].\}}t |t r|| krt|dqW| rd|rP|sPtd |sd|rdtd tt|j d t}|tkp|dkod j k }|r|std |rtt}dd|D}tdt|||d|krdnddd|D}|r.dd|D}tdt||rldd|D}td|}td|}td td d|||rdd|D}td|}td|}xBdD]:\}}t|t||||rtd|djdqW|rxAsz"_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 onerw__eq__z eq must be true if order is truecSsg|]}|jttfkr|qSr)r(rr)r;r<rrrr>~srrZ__dataclass_self__cSsg|]}|jtkr|qSr)r(r)r;r<rrrr>scSsg|]}|jr|qSr)r#)r;r<rrrr>srcSsg|]}|jr|qSr)r&)r;r<rrrr>srrz==cSsg|]}|jr|qSr)r&)r;r<rrrr>s))__lt__<)__le__z<=)__gt__>)__ge__z>=zCannot overwrite attribute z in class z). Consider using functools.total_ordering__doc__z -> NonerS)$r_PARAMSr2__mro__r._FIELDSvaluesrr6rritemsrrr!rdelattrrer7hasattrrgrrjrlr@rvrrq _hash_actionboolrwrinspectZ signaturer r)rmr%r#r3r4r5r6rZany_frozen_baseZhas_dataclass_basesrVZ base_fieldsr<Zcls_annotationsZ cls_fieldsrr]Z class_hashZhas_explicit_hashrhrZ field_listrtrursrkZ hash_actionr)rmr_process_classs                     rF)r%r#r3r4r5r6cs*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|S)N)r)rm)r3r6r%r4r#r5rrwrapszdataclass..wrapNr)_clsr%r#r3r4r5r6rr)r3r6r%r4r#r5rrscCsByt|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|VqdS)N)r(r)r;r<rrrrWszfields..)r.rAttributeErrorretupler)Zclass_or_instancerrrrrs cCst|t ot|tS)z2Returns True if obj is an instance of a dataclass.)rr rr)objrrr_is_dataclass_instancesrcCs t|tS)zEReturns True if obj is a dataclass or an instance of a dataclass.)rr)rrrrr 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)rre _asdict_inner)rrrrrr scst|rHg}x2t|D]&}tt||j}||j|fqW|St|trvt|drvt |fdd|DSt|t tfrt |fdd|DSt|t rt |fdd| DSt |SdS)N_fieldscsg|]}t|qSr)r)r;v)rrrr>2sz!_asdict_inner..c3s|]}t|VqdS)N)r)r;r)rrrrW7sz _asdict_inner..c3s&|]\}}t|t|fVqdS)N)r)r;kr)rrrrW9s)rrrr.rrfrrrr rrrcopydeepcopy)rrrGr<r]r)rrrs 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)rre_astuple_inner)rrrrrr @scst|rBg}x,t|D] }tt||j}||qW|St|trpt|drpt |fdd|DSt|t tfrt |fdd|DSt|t rt |fdd| DSt |SdS)Nrcsg|]}t|qSr)r)r;r)rrrr>fsz"_astuple_inner..c3s|]}t|VqdS)N)r)r;r)rrrrWksz!_astuple_inner..c3s&|]\}}t|t|fVqdS)N)r)r;rr)rrrrWms)rrrr.rrfrrrr rrrrr)rrrGr<r]r)rrrXs rr)bases namespacer%r#r3r4r5r6c sdkrint} i} x|D]} t| tr>| } d}nDt| dkrT| \} }n.t| dkrt| \} }}|| <ntd| t| tr| std| t| rtd| | | krtd| | | || | <q&W| 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 identifers: z"Field names must not be keywords: zField name duplicated: rcs |S)N)update)r)rrrz make_dataclass..)r%r#r3r4r5r6) rrKrrlenre isidentifierkeyword iskeywordrDr* new_classr)Zcls_namerrrr%r#r3r4r5r6seenZannsitemrtpspecrmr)rrr ss6           cKst|stdx~t|tD]l}|jtkr0q |jsT|j|kr t d|jdq |j|kr |jt krzt d|jdt||j||j<q W|j f|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 instanceszfield zC is declared with init=False, it cannot be specified with replace()zInitVar z! must be specified with replace()) rrer.rrr(rr%rr7r __class__)rZchangesr<rrrr s    )N)Brerrr*rrrXrLrB__all__rrrrbrrr+r)rrrrrrrgcompilerr rrrr2rr@rNr[r_r`rarjrlrqrvrxr|r~rrrrrrrrrrrr rr rrr rr r rrrrs  : 60  ;R4 )@