a cD@s*UddlZddlZddlZddlmZmZmZmZmZmZm Z m Z m Z m Z m Z mZmZmZddlmZmZddlmZddlmZddlmZddlmZmZdd lmZdd lmZmZm Z m!Z!m"Z"dd l#m$Z$m%Z%ej&d krdd lm'Z'edddZ(eZ)e e eee edffZ*e e)e efZ+ej&dkrTee*e efZ,ee ee+fZ-neZ,eZ-e,Z.e-Z/GdddeZ0ee eefedddZ1e e0e edfddddZ2i3j4Z5e ee6d<eee)dddZ7e e e8e9fddd Z:e e0e e8efe e8e;fe ee;fdd!d"d#Z._cache_keyNz?Cannot parameterize a concrete instantiation of a generic modelcss|]}t|tVqdSN)r1r .0paramr(r(r6 bz1GenericModel.__class_getitem__..zDType parameters should be placed on typing.Generic, not GenericModelr*zType z< must inherit from typing.Generic before being parameterizedcSs"i|]\}}t|tur||qSr()rr)r:kvr(r(r6 rr=z2GenericModel.__class_getitem__..cs*i|]"}|jvr|tj|jfqSr() __fields__rZ field_info)r:r>r5r(r6r@tr=) __module____base__Z __config__Z__validators__Z__cls_kwargs___cSsi|] }|dqSr8r(r9r(r(r6r@r=rr)(rCacheKey_generic_types_cachegetr)r __bases__ TypeErrorr1r3r%anyhasattr__name__check_parameters_countdictzipr*rkeysvalues__concrete_name__rritemsget_caller_frame_inforr rrB__parameterized_bases___assigned_parameterssysmodules__dict__ setdefaultZConfigiter_contained_typevarsr0_prepare_model_fields)r+r,r7cached typevars_mapZ model_nameZ validatorsZ type_hintsinstance_type_hintsfieldsZ model_moduleZcalled_globally created_modelZobject_by_referenceZreference_nameZreference_module_globals new_paramsr(r5r6__class_getitem__Hsp             zGenericModel.__class_getitem__cCs*dd|D}d|}|jd|dS)aCompute class name for child classes. :param params: Tuple of types the class . Given a generic class `Model` with 2 type variables and a concrete model `Model[str, int]`, the value `(str, int)` would be passed to `params`. :return: String representing a the new class where `params` are passed to `cls` as type variables. This method can be overridden to achieve a custom naming scheme for GenericModels. cSsg|] }t|qSr()rr9r(r(r6 r=z2GenericModel.__concrete_name__..z, [])joinrL)r+r,Z param_namesZparams_componentr(r(r6rRs  zGenericModel.__concrete_name__)r^r-c#sttttttdfdd }jD]r}t|ts:q(q(t|ddsJq(q(tvr|tvr^q(qfddtD}|||EdHq(||EdHq(dS)a Returns unbound bases of cls parameterised to given type variables :param typevars_map: Dictionary of type applications for binding subclasses. Given a generic class `Model` with 2 type variables [S, T] and a concrete model `Model[str, int]`, the value `{S: str, T: int}` would be passed to `typevars_map`. :return: an iterator of generic sub classes, parameterised by `typevars_map` and other assigned parameters of `cls` e.g.: ``` class A(GenericModel, Generic[T]): ... class B(A[V], Generic[V]): ... assert A[int] in B.__parameterized_bases__({V: int}) ``` ) base_model mapped_typesr-c3s@tfdd|jD}||}||us2|ur6dS|VdS)Nc3s|]}|VqdSr8r(r9rir(r6r<r=zQGenericModel.__parameterized_bases__..build_base_model..)r3r*rc)rhriZbase_parametersZparameterized_baser5rjr6build_base_models  z>GenericModel.__parameterized_bases__..build_base_modelr*Ncsi|]\}}|||qSr()rG)r:keyvalue)r^r(r6r@sz8GenericModel.__parameterized_bases__..) r r%ParametrizationrrH issubclassgetattrrVrS)r+r^rkrhrir()r+r^r6rUs       z$GenericModel.__parameterized_bases__N)rLrB __qualname__ __slots__r)rbool__annotations__rr TypeVarTyper r$rrrc classmethodstrrRrnrrUr(r(r(r6r%=s 2_$)type_type_mapr-css|St|}t|}|tur>|^}}tt|t|fS|rtfdd|D}t||rf|S|durt|trt|tst|dddurtt |j }|dusJt j dkr|t jurt||S||S|st|tr|js|j}tfdd|D}t||r|S||St|ttfrXtfdd|D}t||rT|S|S|s|t|tr|t|j|_|S||S)aReturn type with all occurrences of `type_map` keys recursively replaced with their values. :param type_: Any type, class or generic alias :param type_map: Mapping from `TypeVar` instance to concrete types. :return: New type representing the basic structure of `type_` with all `typevar_map` keys recursively replaced. >>> replace_types(Tuple[str, Union[List[str], float]], {str: int}) Tuple[int, Union[List[int], float]] c3s|]}t|VqdSr8 replace_types)r:argryr(r6r<r=z replace_types..N_namer c3s|]}t|VqdSr8rz)r:tr}r(r6r<+r=c3s|]}t|VqdSr8rz)r:elementr}r(r6r<3r=)rrrr{r3rr1rrptypingr~rW version_infotypesZ UnionTyper#rr%r)r*rr2rZ inner_typerG)rxryZ type_argsZ origin_typeannotated_type annotationsZresolved_type_argsZ resolved_listr(r}r6r{sN       r{)r+ parametersr-c CsPt|}t|j}||krL||kr&dnd}td|d|jd|d|dS)NmanyfewzToo z parameters for z ; actual z , expected )r0r*rIrL)r+ractualexpected descriptionr(r(r6rMCs  rM DictValues)r?r-ccst|tr|Vnrt|drsJ @        <K