o
    $6dc                 	   @  s  U d Z ddlmZ ddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZmZmZ ddl
mZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z, ddlm-Z- ddl.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z<m=Z= ddl>m?Z?m@Z@mAZA ddlBmCZCmDZDmEZE ddlFmGZG ddlHmIZI ddlmJZJ ddlKmLZLmMZMmNZNmOZO ddlPmQZQmRZR ddlSmTZT ddlUmVZVmWZW ddlXmYZYmZZZm[Z[m\Z\m]Z] ddlLm^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZi ddljmkZkmlZl ddlmmnZn dd lompZpmqZqmrZrmsZs dd!ltmuZu dd"lOmvZv dd#lwmxZx erddd$lymzZz dd%l{m|Z| dd&l}m~Z~ dd'ltmZ e	jd(kZee3ed)f Ze*efebeaf Ze)d*ed+Ze*e_ef e_eb e_ea f ZeQZe!e ege1j/f Zee
jgZd,ed-< ee
jejjgZd,ed.< ee
jejjgZd,ed/< ee
jejjgZd,ed0< ee
j#ejjejj'gZd,ed1< dd8d9Zdd?d@ZddDdEZddLdMZddUdVZe#e(e  e=f Zdd[d\ZG d]d^ d^ZG d_d` d`Zdadb dcdb dddb dedb dfdb dgdb dhdb didb djZdkedl< e1je1jdmdb e1jdnZdoedp< ddsdtZddvdwZdd{d|ZdddZdddZG dd de7ZdddddddddZG dd dZdddZdS )z-Convert python types to pydantic-core schema.    )annotationsN)contextmanagernullcontext)copy)Enum)partial)	Parameter_ParameterKind	signature)chain)
attrgetter)FunctionType
LambdaType
MethodType)TYPE_CHECKINGAnyCallableContextManagerDict
ForwardRefIterableIteratorMappingTypeTypeVarUnioncastoverload)warn)
CoreSchemaPydanticUndefinedcore_schema)	AnnotatedFinalLiteralTypeAliasType	TypedDictget_args
get_originis_typeddict   )
ConfigDictJsonEncoder)PydanticSchemaGenerationErrorPydanticUndefinedAnnotationPydanticUserError)AliasChoices	AliasPath	FieldInfo)JsonSchemaValue)version_short)PydanticDeprecatedSince20   )_decorators_discriminated_union_known_annotated_metadata_typing_extra)GetCoreSchemaHandlerGetJsonSchemaHandler)ConfigWrapper)CoreMetadataHandlerbuild_metadata_dict)CoreSchemaOrFielddefine_expected_missing_refsflatten_schema_defsget_type_ref%is_list_like_schema_with_items_schema)ComputedFieldInfo	DecoratorDecoratorInfosFieldSerializerDecoratorInfoFieldValidatorDecoratorInfoModelSerializerDecoratorInfoModelValidatorDecoratorInfoRootValidatorDecoratorInfoValidatorDecoratorInfoinspect_field_serializerinspect_model_serializerinspect_validator)collect_dataclass_fieldsget_type_hints_infer_globalns)PydanticRecursiveRef)get_standard_typevars_maphas_instance_in_typerecursively_defined_type_refsreplace_types)CallbackGetCoreSchemaHandler)is_finalvar)lenient_issubclass	BaseModel)FieldValidatorModes)StandardDataclass)GetJsonSchemaFunction)      {   FieldDecoratorInfoType)boundz
list[type]TUPLE_TYPES
LIST_TYPES	SET_TYPESFROZEN_SET_TYPES
DICT_TYPESinfoFieldDecoratorInfofieldstrreturnboolc                 C  s:   t | ttfrd| jv rdS | jD ]	}||kr dS qdS )zCheck if field name is in validator fields.

    Args:
        info: The field info.
        field: The field name to check.

    Returns:
        `True` if field name is in validator fields, `False` otherwise.
    *TF)
isinstancerM   rI   fields)rj   rl   Zv_field_name rs   zC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\pydantic/_internal/_generate_schema.py)check_validator_fields_against_field_nameo   s   

ru   
decoratorsIterable[AnyFieldDecorator]rr   Iterable[str]Nonec                 C  sv   t |}| D ]2}t|jttfrd|jjv rq|jjdu rq|jjD ]}||vr7td|j d|j	 dddq"qdS )	av  Check if the defined fields in decorators exist in `fields` param.

    It ignores the check for a decorator if the decorator has `*` as field or `check_fields=False`.

    Args:
        decorators: An iterable of decorators.
        fields: An iterable of fields name.

    Raises:
        PydanticUserError: If one of the field names does not exist in `fields` param.
    rp   Fz*Decorators defined with incorrect fields: .zO (use check_fields=False if you're inheriting from the model and intended this)zdecorator-missing-fieldcodeN)
setrq   rj   rM   rI   rr   Zcheck_fieldsr/   Zcls_refcls_var_name)rv   rr   decrl   rs   rs   rt   check_decorator_fields_exist   s   r   validator_functions+Iterable[Decorator[FieldDecoratorInfoType]]'list[Decorator[FieldDecoratorInfoType]]c                   s    fdd| D S )Nc                   s   g | ]
}t |j r|qS rs   )ru   rj   ).0r   rl   rs   rt   
<listcomp>       z8filter_field_decorator_info_by_field.<locals>.<listcomp>rs   )r   rl   rs   r   rt   $filter_field_decorator_info_by_field   s   r   schemacore_schema.CoreSchemaeach_item_validators'list[Decorator[ValidatorDecoratorInfo]]
field_name
str | Nonec                 C  s   | d dkrt | d ||| d< | S t| r.| dd }|d u r$t }t|||| d< | S | d dkrL| dd }|d u rBt }t|||| d< | S |rWtd| d  | S )Ntypenullabler   items_schemadictZvalues_schemazO`@validator(..., each_item=True)` cannot be applied to fields with a schema of )apply_each_item_validatorsrD   getr!   
any_schemaapply_validators	TypeError)r   r   r   inner_schemars   rs   rt   r      s(   
r   schema_or_fieldr@   handlerr<   clsr   r3   c                C  sn   || }| |}d|v r|d }|  d|ig|d< d|vr%|j|d< |j}|r5d|vr5t||d< |S )a0  Add title and description for model-like classes' JSON schema.

    Args:
        schema_or_field: The schema data to generate a JSON schema from.
        handler: The `GetCoreSchemaHandler` instance.
        cls: The model-like class.

    Returns:
        JsonSchemaValue: The updated JSON schema.
    z$refZallOftitledescription)Zresolve_ref_schemaclear__name____doc__inspectcleandoc)r   r   r   json_schemaoriginal_schemaref	docstringrs   rs   rt   modify_model_json_schema   s   

r   json_encodersJsonEncoders | Nonetpr   c                 C  s   | s|S d|v r
|S |gt |d|jjdd R D ]#}| |}|du r&qtdt  dt tj	|dd|d< |  S |S )	a  Iterate over the json_encoders and add the first matching encoder to the schema.

    Args:
        json_encoders: A dictionary of types and their encoder functions.
        tp: The type to check for a matching encoder.
        schema: The schema to add the encoder to.
    serialization__mro__Nz=`json_encoders` is deprecated. See https://docs.pydantic.dev/z9/usage/serialization/#custom-serializers for alternativesjson)	when_used)
getattr	__class__r   r   warningsr   r4   r5   r!   $plain_serializer_function_ser_schema)r   r   r   baseencoderrs   rs   rt   ,_add_custom_serialization_from_json_encoders   s   
$
r   c                   @  s2   e Zd ZdZdddZedddZdddZdS )ConfigWrapperStackz%A stack of `ConfigWrapper` instances.config_wrapperr=   c                 C  s   |g| _ d S N_config_wrapper_stack)selfr   rs   rs   rt   __init__
  s   zConfigWrapperStack.__init__rn   c                 C  s
   | j d S )Nr   r   r   rs   rs   rt   tail  s   
zConfigWrapperStack.tail!ConfigWrapper | ConfigDict | NoneContextManager[None]c                   s>    d u rt  S t tst dd td fdd}| S )NFcheckrn   Iterator[None]c                   3  s2    j   zd V  W j   d S j   w r   )r   appendpoprs   r   r   rs   rt   _context_manager  s
   z1ConfigWrapperStack.push.<locals>._context_manager)rn   r   )r   rq   r=   r   )r   r   r   rs   r   rt   push  s   
zConfigWrapperStack.pushN)r   r=   rn   r=   )r   r   rn   r   )r   
__module____qualname__r   r   propertyr   r   rs   rs   rs   rt   r     s    
r   c                   @  s  e Zd ZdZ	ddd	d
ZedddZedddZedddZ	edddZ
dddZddd Zdd#d$Zdd%d&Zdd'd(Zdd)d*Zdd-d.Zdd/d0Zdd2d3Zdd5d6Zdd;d<Z	=	=dddAdBZddEdFZddHdIZddJdKZddMdNZddOdPZeddTdUZeddWdUZdddYdUZddZd[Zdd]d^Zdd_d`Z ddadbZ!ddddeZ"d=dfddndoZ#ddqdrZ$ddtduZ%ddwdxZ&ddzd{Z'dd|d}Z(dddZ)dddZ*dddZ+e,j-dfdddZ.d ddZ/dddZ0dddZ1dddZ2dddZ3dddZ4dddZ5dddZ6dddZ7dddZ8d	ddZ9d
ddZ:dddZ;dddZ<ddÄ fdddǄZ=dddʄZ>ddd̄Z?dddӄZ@	Xdddd؄ZAdddۄZBdS (  GenerateSchemaz\Generate core schema for a Pydantic model, dataclass and types like `str`, `datetime`, ... .Nr   r=   types_namespacedict[str, Any] | Nonetypevars_mapdict[Any, Any] | Nonec                 C  s"   t || _|| _|| _t | _d S r   )r   r   _types_namespace_typevars_map_Definitionsdefs)r   r   r   r   rs   rs   rt   r   &  s   
zGenerateSchema.__init__config_wrapper_stackr   r   r   rn   c                 C  s&   |  | }||_||_||_||_|S r   )__new__r   r   r   r   )r   r   r   r   r   objrs   rs   rt   Z__from_parent2  s   
zGenerateSchema.__from_parentc                 C     | j jS r   )r   r   r   rs   rs   rt   _config_wrapperA     zGenerateSchema._config_wrapperc                 C  s$   | j jpt}|| j| j| j| jS r   )r   Zschema_generatorr   _GenerateSchema__from_parentr   r   r   r   )r   r   rs   rs   rt   _current_generate_schemaE  s   z'GenerateSchema._current_generate_schemaro   c                 C  r   r   )r   Zarbitrary_types_allowedr   rs   rs   rt   _arbitrary_typesO  r   zGenerateSchema._arbitrary_typesr   c                 C  s   t  S )zGenerate a CoreSchema for `str`)r!   
str_schemar   rs   rs   rt   r   S  r   zGenerateSchema.str_schemar   r   
items_typec                 C     t | |S r   )r!   list_schemagenerate_schemar   r   r   rs   rs   rt   _list_schemaY     zGenerateSchema._list_schema	keys_typevalues_typec                 C  s   t | || |S r   )r!   Zdict_schemar   )r   r   r   r   rs   rs   rt   _dict_schema\  s   zGenerateSchema._dict_schemac                 C  r   r   )r!   Z
set_schemar   r   rs   rs   rt   _set_schema_  r   zGenerateSchema._set_schemac                 C  r   r   )r!   Zfrozenset_schemar   r   rs   rs   rt   _frozenset_schemab  r   z GenerateSchema._frozenset_schemac                 C  r   r   )r!   tuple_variable_schemar   r   rs   rs   rt   _tuple_variable_schemae  r   z%GenerateSchema._tuple_variable_schemaitems_types	list[Any]c                   s    fdd|D }t |S )Nc                   s   g | ]}  |qS rs   )r   )r   r   r   rs   rt   r   i      z;GenerateSchema._tuple_positional_schema.<locals>.<listcomp>)r!   tuple_positional_schema)r   r   r   Zitems_schemasrs   r   rt   _tuple_positional_schemah  s   
z'GenerateSchema._tuple_positional_schemac                 C  s,   t |tst|dt t S t|S )Na   is not a Python type (it may be an instance of an object), Pydantic will allow any object with no validation since we cannot even enforce that the input is an instance of the given type. To get rid of this error wrap the type with `pydantic.SkipValidation`.)rq   r   r   UserWarningr!   r   is_instance_schema)r   r   rs   rs   rt   _arbitrary_type_schemal  s   

z%GenerateSchema._arbitrary_type_schemar   c                 C  s   t d|d)N,Unable to generate pydantic-core schema for a  . Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.

If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.)r-   r   r   rs   rs   rt   _unknown_type_schemax  s   
z#GenerateSchema._unknown_type_schemar   c                 C  s:   t d|dd }|r|| jj|< t|t| jj S )Nr   r   )r   r   r   definitionsr!   Zdefinitions_schemalistvalues)r   r   r   rs   rs   rt   collect_definitions  s   z"GenerateSchema.collect_definitionsmetadata_schemajs_functionCallable[..., Any]ry   c                 C  s0   t |j}|dg }||vr|| d S d S )Npydantic_js_functions)r>   metadata
setdefaultr   )r   r   r   r  r  rs   rs   rt   _add_js_function  s
   
zGenerateSchema._add_js_functionTfrom_dunder_get_core_schemafrom_prepare_argsr   c                 C  s   d}|r	|  |}|r| ||}|dur|}|du r | |}t||}|dur8t|| jj}|r8| || t| j	j
||}|S )a  Generate core schema.

        Args:
            obj: The object to generate core schema for.
            from_dunder_get_core_schema: Whether to generate schema from either the
                `__get_pydantic_core_schema__` function or `__pydantic_core_schema__` property.
            from_prepare_args: Whether to generate schema from either the
                `__prepare_pydantic_annotations__` function or `__prepare_pydantic_annotations__` property.

        Returns:
            The generated core schema.

        Raises:
            PydanticUndefinedAnnotation:
                If it is not possible to evaluate forward reference.
            PydanticSchemaGenerationError:
                If it is not possible to generate pydantic-core schema.
            TypeError:
                - If `alias_generator` returns a non-string value.
                - If V1 style validator with `each_item=True` applied on a wrong field.
            PydanticUserError:
                - If `typing.TypedDict` is used instead of `typing_extensions.TypedDict` on Python < 3.12.
                - If `__modify_schema__` method is used instead of `__get_pydantic_json_schema__`.
        N))_generate_schema_from_prepare_annotations_generate_schema_from_property_generate_schema!_extract_get_pydantic_json_schemaresolve_original_schemar   r   r  r   r   r   )r   r   r  r  r   from_propertymetadata_js_functionr   rs   rs   rt   r     s    


zGenerateSchema.generate_schemar   type[BaseModel]c                   s  j |H\}}|dur|W  d   S |j}|j  j}tt j  j	  j
 h | |  t|jdd}||}ttt|dgd} j }	d}
|ddkr|g|jR D ]2}|jdd}|durt|}|ttfvrtd	j|jd d
dd }|tur|}
 nqej| j|j rΈ!d|d  }|d }t"||	d}t#j$||t%|ddd
t%|dd|||d}nHt#j& fdd|' D  fdd| D |
|j(d}t)| j* d}t+|t, }t"||	d}t#j$||t%|dddt%|dd|||d}-| j. }t"||	d}|j j/|< t#0|W  d   W  d   S 1 sBw   Y  W d   dS 1 sSw   Y  dS )z%Generate schema for a Pydantic model.NFr   r   Zjs_functionsZextra_fields_behaviorZallowZ__pydantic_extra__zEThe type annotation for `__pydantic_extra__` must be `Dict[str, ...]`Trequiredr6   rootr   innerZ__pydantic_custom_init__Z__pydantic_post_init__)Zcustom_initZ
root_model	post_initconfigr   r  c                   s    i | ]\}}| || qS rs   )_generate_md_field_schemar   kvrv   r   rs   rt   
<dictcomp>  s     z0GenerateSchema._model_schema.<locals>.<dictcomp>c                      g | ]	} | jqS rs   _computed_field_schemafield_serializersr   dr  rs   rt   r         z0GenerateSchema._model_schema.<locals>.<listcomp>)computed_fieldsextra_validatorZ
model_nameouter)1r   get_schema_or_refZmodel_fields__pydantic_decorators__r$  r   r   field_validatorsr   r   
validatorskeysr=   Zmodel_configcore_configr?   r   r   model_validatorsr   r   __annotations__r(   r   r   r-    _get_args_resolving_forward_refsr   r   r   r   r   Z__pydantic_root_model___common_field_schemaapply_model_validatorsr!   model_schemar   Zmodel_fields_schemaitemsr   r   root_validatorsrA   rV   _apply_model_serializersmodel_serializersr   definition_reference_schema)r   r   Z	model_refmaybe_schemarr   r$  r   r,  r  r-  r%  r   Zextras_annotationZextra_items_typeZ
root_fieldr   r2  Zfields_schemar   rs   r  rt   _model_schema  s   




	

'$zGenerateSchema._model_schemacore_schema.CoreSchema | Nonec                 C  s,   |  |g \}}||us|r| ||S dS )zTry to generate schema from either the `__prepare_pydantic_annotations__` function or
        `__prepare_pydantic_annotations__` property.
        N)_prepare_annotations_apply_annotations)r   r   Znew_objZnew_annotationsrs   rs   rt   r  #  s   z8GenerateSchema._generate_schema_from_prepare_annotationsc                   sJ   ddd t |}|d dkr#| jj fd	d
|d D  |d }|S )zuUnpack all 'definitions' schemas into `GenerateSchema.defs.definitions`
        and return the inner schema.
        sr   rn   rm   c                 S  s   | d S )Nr   rs   )r=  rs   rs   rt   get_ref4  s   z1GenerateSchema._unpack_refs_defs.<locals>.get_refr   r   c                   s   i | ]} ||qS rs   rs   )r   r=  r>  rs   rt   r  :  r   z4GenerateSchema._unpack_refs_defs.<locals>.<dictcomp>r   N)r=  r   rn   rm   )rB   r   r   update)r   r   rs   r?  rt   _unpack_refs_defs/  s   
 z GenerateSchema._unpack_refs_defssourcec           
      C  s  | j |\}}|dur|W  d   S W d   n1 s w   Y  ||u r,d}nd}t|dd}|du rUt|dd}|du rDdS tdt tdd | D }ntt	|j
d	krd||}n||t| j| |d
}| |}|dd}	|	r|| j j|	< t|	S |S )a0  Try to generate schema from either the `__get_pydantic_core_schema__` function or
        `__pydantic_core_schema__` property.

        Note: `__get_pydantic_core_schema__` takes priority so it can
        decide whether to use a `__pydantic_core_schema__` attribute, or generate a fresh schema.
        Nunpackzto-def__get_pydantic_core_schema__Z__get_validators__zc`__get_validators__` is deprecated and will be removed, use `__get_pydantic_core_schema__` instead.c                 S  s   g | ]}t |qS rs   r!   Z general_plain_validator_functionr   r  rs   rs   rt   r   X  r   zAGenerateSchema._generate_schema_from_property.<locals>.<listcomp>r6   )ref_moder   )r   r'  r   r   r5   r!   chain_schemalenr   r
   
parametersrX   r	  rA  r   r   r7  )
r   r   rB  _r8  rG  Z
get_schemar*  r   r   rs   rs   rt   r  >  s<   


z-GenerateSchema._generate_schema_from_propertyc              
   C  sl   z
t j|| jd}W n ty } zt||d }~ww t|tr+t|jd| | j	r4t
|| j	}|S )N)globalnsz%Unable to evaluate forward reference )r:   Zevaluate_fwd_refr   	NameErrorr.   from_name_errorrq   r   __forward_arg__r   rW   )r   r   ers   rs   rt   _resolve_forward_refj  s   
z#GenerateSchema._resolve_forward_refr  Literal[True]tuple[Any, ...]c                 C     d S r   rs   )r   r   r  rs   rs   rt   r/       z/GenerateSchema._get_args_resolving_forward_refstuple[Any, ...] | Nonec                 C  rT  r   rs   r   rs   rs   rt   r/    rU  Fc                   s>   t |}|rt fdd|D }|S |rtd| d|S )Nc                   s$   g | ]}t |tr |n|qS rs   )rq   r   rQ  )r   ar   rs   rt   r     s   $ zCGenerateSchema._get_args_resolving_forward_refs.<locals>.<listcomp>z	Expected z+ to have generic parameters but it had none)r'   tupler   )r   r   r  argsrs   r   rt   r/    s   c                 C  s   |  |}|s	tS |d S )Nr   )r/  r   )r   r   rY  rs   rs   rt   _get_first_arg_or_any  s   
z$GenerateSchema._get_first_arg_or_anytuple[Any, Any]c                 C  sJ   |  |}|sttfS t|dk rt|}td| d|d |d fS )Nr*   z Expected two type arguments for z, got 1r   r6   )r/  r   rI  r(   r   )r   r   rY  originrs   rs   rt   _get_first_two_args_or_any  s   
z)GenerateSchema._get_first_two_args_or_anyc                 C  s   t |tr
| |S t |tr|S t |trt|}t |tr'| | |S ddlm	} t
||r7| |S t |trCtj|jdS | |S )zJRecursively generate a pydantic-core schema for any supported python type.r*   r[   )
schema_ref)rq   _AnnotatedType_annotated_schemar   rm   r   r   rQ  mainr\   rZ   r9  rS   r!   r7  Ztype_ref
match_type)r   r   r\   rs   rs   rt   r	    s   








zGenerateSchema._generate_schemac                 C  s  |t u r|  S |tu rt S |tu rt S |tu r t S |t	u r(t
 S |tu s0|tu r4t S |du s=|tju rAt S |tv rJ| |S |tv rW| || |S |tv rd| || |S |tv rq| || |S |tv r| j|g| |R  S t|tr| |S |t kr| ! S t"|rt# S t$|r| %|S t&|r| '|dS t(|r| )|dS t*|r| +|j,S |t-j.kr| /|S |t0j1j2u s|t3j2u r| 4 S t|t3j5r| 6|S t7|r|t8u rt S | +| |S t|t9t:t;t<fr| =|S t>?|r&t@|tAr&ddlBmC} ||| jDjES tF|r2| G|dS | H|d}|durG|\}}| I||S tJ|}|durV| K||S | jLr_| M|S | N|S )a  Main mapping of types to schemas.

        The general structure is a series of if statements starting with the simple cases
        (non-generic primitive types) and then handling generics and other more complex cases.

        Each case either generates a schema directly, calls into a public user-overridable method
        (like `GenerateSchema.tuple_variable_schema`) or calls into a private method that handles some
        boilerplate before calling into the user-facing method (e.g. `GenerateSchema._tuple_schema`).

        The idea is that we'll evolve this into adding more and more user facing methods over time
        as they get requested and we figure out what the right API for them is.
        Nr6   )get_enum_core_schemars   )Orm   r   bytesr!   Zbytes_schemaintZ
int_schemafloatZfloat_schemaro   Zbool_schemar   objectr   r:   NoneTypeZnone_schemare   _tuple_schemarf   r   rZ  rg   r   rh   r   ri   r   r]  rq   r%   _type_alias_type_schemar   _type_schemaZis_callable_typeZcallable_schemaZis_literal_type_literal_schemar)   _typed_dict_schemais_namedtuple_namedtuple_schemaZis_new_typer   __supertype__rePattern_pattern_schemacollectionsabcHashabletyping_hashable_schemar   _unsubstituted_typevar_schemarY   r#   r   r   r   r   _callable_schemar   isclass
issubclassr   _std_types_schemarc  r   config_dictis_dataclass_dataclass_schema0_get_prepare_pydantic_annotations_for_known_typer<  r(   _match_generic_typer   r   r   )r   r   rc  ressource_typer   r\  rs   rs   rt   rb    s   















zGenerateSchema.match_typer\  c                 C  s  t |tr
| |S t|r| ||S t|r | ||S | ||}|d ur,|S t	|r6| 
|S |tv r?| |S |tv rL| || |S |tv rY| || |S |tv rf| || |S |tv rv| j|g| |R  S t|r| ||S |tjtfv r| |S |tjtjjhv r|  |S |tj!tjj!tj"tjj"hv r| #|S |t$j%tj%fv r| &|S | j'r| (|S | )|S r   )*rq   r%   rj  r:   r  r  rn  ro  r  origin_is_union_union_schemare   ri  rf   r   rZ  rg   r   rh   r   ri   r   r]  r)   rm  rw  r   r   _subclass_schemaSequencert  ru  _sequence_schemar   	Generator_iterable_schemarq  rr  rs  r   r   r   )r   r   r\  r  rs   rs   rt   r    sD   












z"GenerateSchema._match_generic_typer  namerm   
field_infor2   rv   rG   core_schema.TypedDictFieldc                C  sD   |  |||}tj|d | sdn||d |d |d |d dS )zAPrepare a TypedDictField to represent a model or typeddict field.r   Fserialization_excludevalidation_aliasserialization_aliasr  )r  r  r  r  r  )r0  r!   Ztyped_dict_fieldis_required)r   r  r  rv   r  common_fieldrs   rs   rt   _generate_td_field_schema;  s   	z(GenerateSchema._generate_td_field_schemacore_schema.ModelFieldc                 C  s<   |  |||}tj|d |d |d |d |d |d dS )z0Prepare a ModelField to represent a model field.r   r  r  r  frozenr  r  r  r  r  r  )r0  r!   Zmodel_fieldr   r  r  rv   r  rs   rs   rt   r  N  s   z(GenerateSchema._generate_md_field_schemacore_schema.DataclassFieldc                 C  sR   |  |||}tj||d |jpd|jrdnd|d |d |d |d |d d		S )
zJPrepare a DataclassField to represent the parameter/field, of a dataclass.r   NFr  r  r  r  r  )Z	init_onlykw_onlyr  r  r  r  r  )r0  r!   Zdataclass_fieldZinit_varr  r  rs   rs   rt   _generate_dc_field_schema_  s   z(GenerateSchema._generate_dc_field_schema_CommonFieldc                   s<  t jttfr6| j}| jr!|pi  }|dd | j D  t	
j|d }|jur6t |ts6|_jj}}dfdd}jd urR| j|||d}	n| ||}	t|j |}
t|
rgd	_d
d |
D   fdd|
D }
t|	 |}	t|	t|
||}	t|	t|j ||}	 st|	}	| |	t|j |}	jjjddd  D jdfdd}t|gd}| j j!}|rj"d u sڈj"dkr||}t#|tst$d| d|j% |_&|_'|_(d_"t#j't)t*fr	j'+ }nj'}t,|	j-rd	nd |j(j.|dS )Nc                 S  s   i | ]\}}|j |qS rs   )r   r  rs   rs   rt   r  z      z7GenerateSchema._common_field_schema.<locals>.<dictcomp>r   r   rn   c                   s   t |  j | S r   )r8   set_discriminatordiscriminator)r   )r  rs   rt   r    s   z>GenerateSchema._common_field_schema.<locals>.set_discriminatortransform_inner_schemaTc                 S  s   g | ]
}|j jd u r|qS )T)rj   Z	each_itemrF  rs   rs   rt   r     r   z7GenerateSchema._common_field_schema.<locals>.<listcomp>c                   s   g | ]}| vr|qS rs   rs   rF  )r   rs   rt   r     r  )r   r   examplesc                 S  s   i | ]\}}|d ur||qS r   rs   r  rs   rs   rt   r    s    r@   r   r<   r3   c                   s<   i || }t  tr|  |S t r | |S r   )rq   r   r@  callable)r   r   r   )json_schema_extrajson_schema_updatesrs   rt   json_schema_update_func  s   

zDGenerateSchema._common_field_schema.<locals>.json_schema_update_funcZjs_annotation_functionsr6   alias_generator  must return str, not r  r   r   rn   r   r   r@   r   r<   rn   r3   )/rU   
annotationr   rm   r   r   r   r@  r3  r:   Zeval_type_lenientrS   r  r  r<  r   r*  r   $_validators_require_validate_defaultvalidate_defaultr   r   r)  r  wrap_default_apply_field_serializersr   r   r   r  r  r?   r   alias_generatoralias_priorityrq   r   r   aliasr  r  r0   r1   Zconvert_to_aliases_common_fieldexcluder  )r   r  r  rv   r   Z	evaluatedr  r   r  r   Zthis_field_validatorsr  r  r  r  r  rs   )r   r  r  r  rt   r0  s  sv   
	

z#GenerateSchema._common_field_schema
union_typec                 C  s   | j |dd}g }d}|D ]}|du s|tju rd}q|| | qt|dkr:|d }t|tr7|d n|}nt	|}|rFt
|}|S )zGenerate schema for a Union.Tr  FNr6   r   )r/  r:   rh  r   r   rI  rq   rX  r!   union_schemaZnullable_schema)r   r  rY  choicesr   argZfirst_choicer=  rs   rs   rt   r    s   

zGenerateSchema._union_schemac           
      C  s   t |}|p|}| j|W\}}|d ur|W  d    S | jp"i  }i t||}|j}|| _t|}t	||}| 
|}	|	d dksJJ ||	d< |pQd | _|	| jj|< t|W  d    S 1 shw   Y  d S )Nr   r   r   )r(   r   r'  r   r   r:   Zget_cls_types_namespaceZ	__value__rT   rW   r   r   r!   r7  )
r   r   r\  r   r8  	namespaceZnew_namespacer  r   r   rs   rs   rt   rj    s&   


$z&GenerateSchema._type_alias_type_schemaliteral_typec                 C  s&   t |}|sJ d| t|S )zGenerate schema for a Literal.z(literal "expected" cannot be empty, obj=)r:   Zall_literal_valuesr!   Zliteral_schema)r   r  expectedrs   rs   rt   rl     s   

zGenerateSchema._literal_schematyped_dict_clsc              	     s(  j |\}}|dur|W  d   S t|}|dur!|}ts0t|jdkr0tdddd}|g|jR D ]}t|dd}|durG nq9j	
| j|}j|j}	i }
t| t|jdd D ]B\}}t||}||	v }t|tjkrd}j|dd	d
 }nt|tjkrd}j|dd	d
 }t|}j|| |d	|
|< qlttt|dgd}t j!|
 fdd j"# D |||d}$| j%# }t&| j'# d}|j j(|< t )|W  d   W  d   S 1 sw   Y  W d   dS 1 sw   Y  dS )a  Generate schema for a TypedDict.

        It is not possible to track required/optional keys in TypedDict without __required_keys__
        since TypedDict.__new__ erases the base classes (it replaces them with just `dict`)
        and thus we can track usage of total=True/False
        __required_keys__ was added in Python 3.9
        (https://github.com/miss-islington/cpython/blob/1e9939657dd1f8eb9f596f77c1084d2d351172fc/Doc/library/typing.rst?plain=1#L1546-L1548)
        however it is buggy
        (https://github.com/python/typing_extensions/blob/ac52ac5f2cb0e00e7988bae1e2a1b8257ac88d6d/src/typing_extensions.py#L657-L666).

        On 3.11 but < 3.12 TypedDict does not preserve inheritance information.

        Hence to avoid creating validators that do not do what users expect we only
        support typing.TypedDict on Python >= 3.12 or typing_extension.TypedDict on all versions
        Nrw  zXPlease use `typing_extensions.TypedDict` instead of `typing.TypedDict` on Python < 3.12.ztyped-dict-versionr{   __pydantic_config__T)localnsinclude_extrasr  r   Fr  r  c                   r  rs   r  r!  r  rs   rt   r   Q  r#  z5GenerateSchema._typed_dict_schema.<locals>.<listcomp>)r$  r   r  r  all)*r   r'  rT   _SUPPORTS_TYPEDDICTr   r   r/   __orig_bases__r   r   r   r   r,  r   __required_keys__rG   buildrR   r   r3  rW   r(   r:   ZRequiredr/  ZNotRequiredr2   from_annotationr  r?   r   r   r!   Ztyped_dict_schemar$  r   r5  r6  r1  r-  r   r7  )r   r  r\  Ztyped_dict_refr8  r   r  r   r,  required_keysrr   r   r  r  r  r  Z	td_schemar   rs   r  rt   rm    s   



$z!GenerateSchema._typed_dict_schemanamedtuple_clsc                   s   j  \\}}|dur|W  d   S t |dur | t djd}|s2dd  jD }r?fdd| D }tj fdd| D t	dd	d
}tj
| |dW  d   S 1 sew   Y  dS )z!Generate schema for a NamedTuple.NT)r  r  c                 S  s   i | ]}|t qS rs   )r   )r   r  rs   rs   rt   r  m      z5GenerateSchema._namedtuple_schema.<locals>.<dictcomp>c                   s   i | ]
\}}|t | qS rs   rW   r   r   r  r   rs   rt   r  p  s    
c              	     s,   g | ]\}}j || j|tjd qS ))default)_generate_parameter_schema_field_defaultsr   r   emptyr  )r  r   rs   rt   r   v  s    z5GenerateSchema._namedtuple_schema.<locals>.<listcomp>)Zjs_prefer_positional_arguments)r  )r   )r   r'  rT   rR   r   _fieldsr3  r!   arguments_schemar?   call_schema)r   r  r\  Znamedtuple_refr8  r   r  rs   )r  r   r   rt   ro  _  s0   
	$z!GenerateSchema._namedtuple_schemar  	type[Any]r  modeJLiteral['positional_only', 'positional_or_keyword', 'keyword_only'] | Nonecore_schema.ArgumentsParameterc                 C  s   |t ju rt|}nt||}|jdusJ d|j|j}}| ||}| s0t	||}t
||}	|dur>||	d< |jdurJ|j|	d< |	S | jj}
|
rV|
||	d< |	S )zXPrepare a ArgumentsParameter to represent a field in a namedtuple or function signature.Nz<field.annotation should not be None when generating a schemar  r  )r   r  r2   r  Zfrom_annotated_attributer  r  r<  r  r  r!   Zarguments_parameterr  r   r  )r   r  r  r  r  rl   r  r   r   Zparameter_schemar  rs   rs   rt   r    s$   



z)GenerateSchema._generate_parameter_schema
tuple_typec                   s   t | | |} r|rt fdd|D }|s'|tv r"t S tg S |d tu r?t|dkr;| 	||d S t
dt|dkrQ|d dkrQ| |g S | |t|S )	zIGenerate schema for a Tuple, e.g. `tuple[int, str]` or `tuple[int, ...]`.c                 3  s    | ]}t | V  qd S r   r  )r   paramr  rs   rt   	<genexpr>  s    z/GenerateSchema._tuple_schema.<locals>.<genexpr>r   r*   r   z&Variable tuples can only have one typer6   rs   )rT   r/  rX  re   r!   r   r   EllipsisrI  r   
ValueErrorr   r   )r   r  paramsrs   r  rt   ri    s   

zGenerateSchema._tuple_schemac                 C  s   t jt tdddS )NZis_typezInput should be a typeZcustom_error_typeZcustom_error_message)r!   custom_error_schemar   r   r   rs   rs   rt   rk    s
   zGenerateSchema._type_schematype_c                   s     |}|tkr  S t|tjr0|jrt|jS |j	r,t
 fdd|j	D S   S tt|rJ j|dd}t
 fdd|D S t|S )z-Generate schema for a Type, e.g. `Type[int]`.c                      g | ]
}  tj| qS rs   r   rw  r   )r   cr   rs   rt   r     r   z3GenerateSchema._subclass_schema.<locals>.<listcomp>Tr  c                   r  rs   r  )r   rY  r   rs   rt   r     r   )rZ  r   rk  rq   rw  r   	__bound__r!   Zis_subclass_schema__constraints__r  r:   r  r(   r/  )r   r  Z
type_paramrY  rs   r   rt   r    s   

zGenerateSchema._subclass_schemasequence_typec                   s    | d fdd}t|gd	}t }tjtjd
d} tkr8ddl	m
} t|t||g}tj|||dS )z5Generate schema for a Sequence, e.g. `Sequence[int]`._schemar@   r   r<   rn   r3   c                   s     }|t|S r   )r	  r!   r   )r  r   r   	item_typer   rs   rt   json_schema_func  s   
z9GenerateSchema._sequence_schema.<locals>.json_schema_funcr  r  )Zcls_reprr6   )sequence_validator)r   python_schemar  N)r  r@   r   r<   rn   r3   )rZ  r?   r!   r   r   r   rw  r  r   _validatorsr  rH  no_info_wrap_validator_functionZjson_or_python_schema)r   r  r  r  r   r  r  rs   r  rt   r    s   
zGenerateSchema._sequence_schemacore_schema.GeneratorSchemac                 C  s   |  |}t| |S )z$Generate a schema for an `Iterable`.)rZ  r!   Zgenerator_schemar   )r   r  r  rs   rs   rt   r    s   
zGenerateSchema._iterable_schemapattern_typec                 C  s   ddl m} tdd gd}tjtddt d}|tjks$|t	jkr-tj
|j||d	S | j|d
dd }|tkrCtj
|j||d	S |tkrPtj
|j||d	S td|d)Nr6   )r  c                 S  s
   dddS )Nstringregex)r   formatrs   )_1_2rs   rs   rt   <lambda>     
 z0GenerateSchema._pattern_schema.<locals>.<lambda>r  patternr   )r   return_schema)r   r  Tr  r   r   rz   ) r  r?   r!   r   r   r   rw  rr  rq   no_info_plain_validator_functionZpattern_either_validatorr/  rm   Zpattern_str_validatorrd  Zpattern_bytes_validatorr-   )r   r  r  r  Zserr  rs   rs   rt   rs    s0   zGenerateSchema._pattern_schemac                 C  s   t jt tjjdddS )NZis_hashablezInput should be hashabler  )r!   r  r   rt  ru  rv  r   rs   rs   rt   rx    s
   zGenerateSchema._hashable_schema	dataclasstype[StandardDataclass]type[StandardDataclass] | Nonec                   s  j |\}}|dur|W  d   S t|}|dur |}t|dd}j| j|}jddl	m
} ||rT|j}	|rS|	 D ]	}
|
|j qInt|j|d}	|jdpft| t fdd|	 D d	d
 d}t|d}t|d}tj|j| fdd j D |d}t| j d} j }t||d}tj||||dd t !|D ||d}"| j# }t||d}|j j$|< t%|W  d   W  d   S 1 sw   Y  W d   dS 1 sw   Y  dS )z Generate schema for a dataclass.Nr  r6   )is_pydantic_dataclassr  r(  c                 3  s"    | ]\}} || V  qd S r   )r  r  r  rs   rt   r  9  s     z3GenerateSchema._dataclass_schema.<locals>.<genexpr>c                 S  s   |  dduS )Nr  Fr   )rW  rs   rs   rt   r  :  s    z2GenerateSchema._dataclass_schema.<locals>.<lambda>)key__post_init__	__slots__c                   r  rs   r  r!  r  rs   rt   r   B  r#  z4GenerateSchema._dataclass_schema.<locals>.<listcomp>)r$  Zcollect_init_onlyr  c                 S  s   g | ]}|j qS rs   )r  )r   rl   rs   rs   rt   r   S  r  )r  r   rr   slotsr  r&  )&r   r'  rT   r   r   r   r   r,  r   _dataclassesr  Z__pydantic_fields__r   Zapply_typevars_mapr   rQ   __dict__r   rG   r  sortedr3  hasattrr!   Zdataclass_args_schemar   r$  r   r4  r-  r1  Zdataclass_schemadataclassesrr   r5  r6  r   r7  )r   r  r\  Zdataclass_refr8  r   r  r,  r  rr   rl   rY  has_post_initZ	has_slotsZargs_schemar   r-  Z	dc_schemar   rs   r  rt   r    st   



		"z GenerateSchema._dataclass_schemafunctioncore_schema.CallSchemac                 C  s  t |}t|}tjdtjdtjdi}g }d}d}|j D ]F\}}	|	j	|j
u r,t}
n|| }
||	j}|durI| ||
|	j|}|| q|	jtjkrU| |
}q|	jtjks`J |	j| |
}qd}| j}|jr||d}|dur|| |}tjtj||||jd||dS )zsGenerate schema for a Callable.

        TODO support functional validators once we support them in Config
        Zpositional_onlyZpositional_or_keywordkeyword_onlyNrn   )var_args_schemavar_kwargs_schemapopulate_by_name)r  )r
   r:   Zget_function_type_hintsr   POSITIONAL_ONLYPOSITIONAL_OR_KEYWORDKEYWORD_ONLYrJ  r3  r  r  r   r   kindr  r  r   VAR_POSITIONALr   VAR_KEYWORDr   Zvalidate_returnr!   r  r  r  )r   r  sigZ
type_hintsZmode_lookupZarguments_listr  r  r  pr  Zparameter_modeZ
arg_schemar  r   Zreturn_hintrs   rs   rt   rz  \  sJ   


zGenerateSchema._callable_schematypevartyping.TypeVarc                 C  sB   t |tjsJ |jr| |jS |jr| tj|j S t	 S r   )
rq   rw  r   r  r   r  r  r   r!   r   )r   r  rs   rs   rt   ry    s   z,GenerateSchema._unsubstituted_typevar_schemar"  Decorator[ComputedFieldInfo]r   2dict[str, Decorator[FieldSerializerDecoratorInfo]]core_schema.ComputedFieldc           
   
     s  zt  j jj| j}W n ty } zt||d }~ww |t	u r*t
dddt|| j}| |}| j|t|  jdd}| jj}|rr jjd u sU jjdkrr| j}t|tsjtd| d|j | j_d j_d fdd}t|gd}	tj j| jj|	dS )NzComputed field is missing return type annotation or specifying `return_type` to the `@computed_field` decorator (e.g. `@computed_field(return_type=int|str)`)zmodel-field-missing-annotationr{   Tcomputed_fieldr6   r  r  r   r@   r   r<   rn   r3   c                   sD   || }d|d<  j j}|d ur||d<  j j}|d ur ||d< |S )NTZreadOnlyr   r   )rj   r   r   )r   r   r   r   r   r"  rs   rt   set_computed_field_metadata  s   zJGenerateSchema._computed_field_schema.<locals>.set_computed_field_metadatar  )r  r  r  r  )r7   get_function_return_typefuncrj   return_typer   rM  r.   rN  r    r/   rW   r   r   r  r   r   r~   r   r  r  rq   rm   r   r   r  r?   r!   r  )
r   r"  r   r$  rP  Zreturn_type_schemar  r  r!  r  rs   r   rt   r    s<   


z%GenerateSchema._computed_field_schemaannotated_typec                 C  s@   | j |dd^}}| ||}|D ]}t|trt||}q|S )zdGenerate schema for an Annotated type, e.g. `Annotated[int, Field(...)]` or `Annotated[int, Gt(0)]`.Tr  )r/  r<  rq   r2   r  )r   r%  r  r   r   r  rs   rs   rt   r`    s   


z GenerateSchema._annotated_schemar   tuple[Any, list[Any]] | Nonec                 C  s\   ddl m} zt| W n
 ty   Y d S w |D ]}|||| jj}|d ur+|  S qd S )Nr6   )PREPARE_METHODS)r}  r'  hashr   r   r~  )r   r   r   r'  genr  rs   rs   rt   r    s   z?GenerateSchema._get_prepare_pydantic_annotations_for_known_typer  Iterable[Any]tuple[Any, list[Any]]c                 C  sf   t |dd}t|}|dur||t|| jj\}}t|}n| ||}|dur-|\}}|t|fS )a/  Call `__prepare_pydantic_annotations__` if it exists and return a tuple of
            (new_source_type, new_annotations).

        This should be treated conceptually similar to the transformation
        `Annotated[source_type, *annotations]` -> `Annotated[new_source_type, *new_annotations]`
         __prepare_pydantic_annotations__N)r   rX  r   r~  r   r  )r   r  r   preparer  rs   rs   rt   r;    s   
z#GenerateSchema._prepare_annotationsc                 C  s   | S r   rs   )xrs   rs   rt   r    s    zGenerateSchema.<lambda>r  "Callable[[CoreSchema], CoreSchema]c                   sh  t t|}d}t|dd}|r"||t| jj\}}t |}n |t|}|dur2|\}}g }d fdd	}t| }		 |d7 }|t	|krMnK|| }
|
du rVqAt|
dd}|dur|d| }||d d }||t| jj\}}|t | }||ur j
||dS || }
 |	|
|}	qB|	|}|rt|j}|dg | t jj||S )a  Apply arguments from `Annotated` or from `FieldInfo` to a schema.

        This gets called by `GenerateSchema._annotated_schema` but differs from it in that it does
        not expect `source_type` to be an `Annotated` object, it expects it to be  the first argument of that
        (in other words, `GenerateSchema._annotated_schema` just unpacks `Annotated`, this process it).
        r   r,  Nr   r   rn   r   c                   s`     | | }|d u r | }n|}t| |}|d ur,t| jj}|d ur, || |S r   )r  r	  r
  r  r   r   r  )r   r  r   r  r   r   r  rs   rt   inner_handler#  s   
z8GenerateSchema._apply_annotations.<locals>.inner_handlerTr6   r   pydantic_js_annotation_functionsr   r   rn   r   )r   r9   Zexpand_grouped_metadatar   rX  r   r~  r  rX   rI  r<  _get_wrapped_inner_schemar>   r  r  extendr   r   )r   r  r   r  idxr-  r  r2  r1  get_inner_schemar  previous	remainingZnew_source_typer   r  rs   r0  rt   r<    sR   


z!GenerateSchema._apply_annotationsr  c           	      C  sL  t |tr|jD ]}| ||}q|jd urt||j |S |d dkr;|dt	 }| ||}|r9||d< |S |}|dd }|d ure|
 }|dt|  }|| jjv r`| jj| S ||d< n1|d dkr|d }|| jjv r| jj| 
 }|dt|  }|| jjv r| jj| S ||d< t||
 }|d ur|S |S )Nr   r   r   r   rK  definition-refr^  )rq   r2   r  _apply_single_annotationr  r8   r  r   r!   r   r   reprr   r   r9   Zapply_known_metadata)	r   r   r  field_metadatar  r   r   Znew_refZmaybe_updated_schemars   rs   rt   r;  P  sF   



z'GenerateSchema._apply_single_annotationc                   s   t |trF|jD ]}| ||}qi |jr|jd< |jr#|jd< |jr+|jd< |j s2 rFd fd
d}t|j	dg 
| |S )Nr   r   r  r!   r@   r   r<   rn   r3   c                   s>   || }|  t tr|   |S t r | |S r   )r@  rq   r   r  )r!   r   r   r  Zjson_schema_updaters   rt   r    s   


zTGenerateSchema._apply_single_annotation_json_schema.<locals>.json_schema_update_funcr2  )r!   r@   r   r<   rn   r3   )rq   r2   r  $_apply_single_annotation_json_schemar   r   r  r  r>   r  r   )r   r   r  r=  r  rs   r>  rt   r?  {  s"   




z3GenerateSchema._apply_single_annotation_json_schemar7  r;   r2  list[GetJsonSchemaFunction]rX   c                   s4   t  dd p	dd d
 fdd	}t|S )NrD  c                 S  s   || S r   rs   )rB  r   rs   rs   rt   r    s    z:GenerateSchema._get_wrapped_inner_schema.<locals>.<lambda>rB  r   rn   r   c                   sB   | } | }| }t |}|d ur| |S r   )r;  r?  r
  r   )rB  r   r  r  r7  Zmetadata_get_schemar2  r   rs   rt   new_handler  s   


z=GenerateSchema._get_wrapped_inner_schema.<locals>.new_handler)rB  r   rn   r   )r   rX   )r   r7  r  r2  rB  rs   rA  rt   r4    s
   

z(GenerateSchema._get_wrapped_inner_schemaserializers-list[Decorator[FieldSerializerDecoratorInfo]]r  c              
   C  s2  |rt |}|d dkr|d }| |||d< |S td|dd}|dur-t|}|d }t|j|j	j
|d\}}zt|j|j	j| j}	W n ty\ }
 zt|
|
d}
~
ww |	tu rdd}n| |	}|j	j
d	krtj|j||||j	jd
|d< |S |j	j
dksJ tj|j||||j	jd
|d< |S )z$Apply field serializers to a schema.r   r   r   zstr|Noner   Nr   r  wrap)is_field_serializerinfo_argr  r   r   plain)r   r  rw  r   r   r!   r7  rN   r#  rj   r  r7   r"  r$  r   rM  r.   rN  r    r   #wrap_serializer_function_ser_schemar   r   )r   r   rC  r  r   r   
serializerrF  rG  r$  rP  r  rs   rs   rt   r    sT   




z'GenerateSchema._apply_field_serializers1Iterable[Decorator[ModelSerializerDecoratorInfo]]c           
   
   C  s   | dd}|ret|d }t|j|jj}zt|j|jj| j	}W n t
y5 } zt||d}~ww |tu r=d}n| |}|jjdkrUtj|j|||jjd}	ntj|j|||jjd}	|	|d< |rk||d< |S )z$Apply model serializers to a schema.r   Nr   rE  )rG  r  r   r   )r   r   rO   r#  rj   r  r7   r"  r$  r   rM  r.   rN  r    r   r!   rI  r   r   )
r   r   rC  r   rJ  rG  r$  rP  r  Z
ser_schemars   rs   rt   r5    s@   
z'GenerateSchema._apply_model_serializersr   )r   r=   r   r   r   r   )
r   r   r   r   r   r   r   r   rn   r   r   )rn   r   )rn   ro   )rn   r   )r   r   r   r   rn   r   )r   r   r   r   r   r   rn   r   )r   r   r   r   rn   r   )r   r   rn   r   r3  r  )r   r   r   r   rn   ry   )TT)r   r   r  ro   r  ro   rn   r   )r   r  rn   r   )r   r   rn   r:  )r   r   rB  r   rn   r:  )r   r   rn   r   )r   r   r  rR  rn   rS  )r   r   rn   rV  )F)r   r   r  ro   rn   rV  )r   r   rn   r[  )r   r   rn   r   )r   r   r\  r   rn   r   )
r  rm   r  r2   rv   rG   r  ro   rn   r  )r  rm   r  r2   rv   rG   rn   r  )r  rm   r  r2   rv   rG   rn   r  )r  rm   r  r2   rv   rG   rn   r  )r  r   rn   r   )r  r   rn   r   )r  r   r\  r   rn   r   )r  r   r\  r   rn   r   )
r  rm   r  r  r  r   r  r  rn   r  )r  r   rn   r   )rn   r   )r  r   rn   r   )r  r   rn   r   )r  r   rn   r  )r  r   rn   r   )r  r  r\  r  rn   r   )r  r   rn   r  )r  r  rn   r   )r"  r  r   r  rn   r  )r%  r   rn   r   )r   r   r   rS  rn   r&  )r  r   r   r*  rn   r+  )r  r   r   r   r  r/  rn   r   )r   r   r  r   rn   r   )r7  r;   r  r   r2  r@  rn   rX   )r   r   rC  rD  r  ro   rn   r   )r   r   rC  rK  rn   r   )Cr   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r9  r  rA  r  rQ  r   r/  rZ  r]  r	  rb  r  r  r  r  r0  r  rj  rl  rm  ro  r   r  r  ri  rk  r  r  r  rs  rx  r  rz  ry  r  r`  r  r;  r<  r;  r?  r4  r  r5  rs   rs   rs   rt   r   #  s    	










	
5
W


,


	

W2



`



Y%F5
7H+!7r   c                 C     t | |S r   )r!   !no_info_before_validator_functionfrK  r   rs   rs   rt   r        r  c                 C  rM  r   )r!    no_info_after_validator_functionrO  rs   rs   rt   r    rQ  c                 C  
   t | S r   )r!   r  rP  r  r  rs   rs   rt   r    r  c                 C  rM  r   )r!   r  rO  rs   rs   rt   r    rQ  c                 C  rM  r   )r!   !general_before_validator_functionrO  rs   rs   rt   r    rQ  c                 C  rM  r   )r!    general_after_validator_functionrO  rs   rs   rt   r    rQ  c                 C  rS  r   rE  rT  rs   rs   rt   r    r  c                 C  rM  r   )r!   general_wrap_validator_functionrO  rs   rs   rt   r    rQ  ))beforeno-info)afterrY  )rH  rY  )rE  rY  )rX  general)rZ  r[  )rH  r[  )rE  r[  zMapping[tuple[FieldValidatorModes, Literal['no-info', 'general', 'field']], Callable[[Callable[..., Any], None, core_schema.CoreSchema], core_schema.CoreSchema]]_VALIDATOR_F_MATCHc                 C  s   t | |S r   )r!   Zfield_plain_validator_function)rP  r   rK  rs   rs   rt   r  '  rQ  ))rX  rl   )rZ  rl   )rH  rl   )rE  rl   zMapping[tuple[FieldValidatorModes, Literal['no-info', 'general', 'field']], Callable[[Callable[..., Any], str, core_schema.CoreSchema], core_schema.CoreSchema]]_FIELD_VALIDATOR_F_MATCHr*  Iterable[Decorator[RootValidatorDecoratorInfo]] | Iterable[Decorator[ValidatorDecoratorInfo]] | Iterable[Decorator[FieldValidatorDecoratorInfo]]c                 C  s   |D ]G}t |j|jj}|sd}nt|jttfr$|dus!J dd}nd}|du s.|dkr<t|jj|f |jd| } qt|jj|f |j|| } q| S )a  Apply validators to a schema.

    Args:
        schema: The schema to apply validators on.
        validators: An iterable of validators.
        field_name: The name of the field if validators are being applied to a model field.

    Returns:
        The updated schema.
    rY  Nz2field validators must be used within a model fieldrl   r[  )	rP   r#  rj   r  rq   rI   rM   r\  r]  )r   r*  r   	validatorrG  Zval_typers   rs   rt   r   ,  s   r   +Iterable[Decorator[ValidatorDecoratorInfo]]c                 C  s   | D ]	}|j jr dS qdS )a?  In v1, if any of the validators for a field had `always=True`, the default value would be validated.

    This serves as an auxiliary function for re-implementing that logic, by looping over a provided
    collection of (v1-style) ValidatorDecoratorInfo's and checking if any of them have `always=True`.

    We should be able to drop this function and the associated logic calling it once we drop support
    for v1-style validator decorators. (Or we can extend it and keep it if we add something equivalent
    to the v1-validator `always` kwarg to `field_validator`.)
    TF)rj   always)r*  r_  rs   rs   rt   r  N  s
   
r  0Iterable[Decorator[ModelValidatorDecoratorInfo]]r   Literal['inner', 'outer', 'all']c                 C  s   |  dd}|D ]o}|dkr|jjdkrq|dkr |jjdkr qt|j|jj}|jjdkrB|r9tj|j| d} qtj|j| d} q|jjdkr\|rStj|j| d} qtj	|j| d} q|jjdksdJ |rotj
|j| d} qtj|j| d} q|r~|| d< | S )	a  Apply model validators to a schema.

    If mode == 'inner', only "before" validators are applied
    If mode == 'outer', validators other than "before" are applied
    If mode == 'all', all validators are applied

    Args:
        schema: The schema to apply validators on.
        validators: An iterable of validators.
        mode: The validator mode.

    Returns:
        The updated schema.
    r   Nr  rX  r&  rE  )r  r   rZ  )r   rj   r  rP   r#  r!   rW  r  rU  rN  rV  rR  )r   r*  r  r   r_  rG  rs   rs   rt   r1  ^  s,   r1  r  r2   c                 C  s<   | j rtj|| j | jdS | jturtj|| j| jdS |S )a  Wrap schema with default schema if default value or `default_factory` are available.

    Args:
        field_info: The field info object.
        schema: The schema to apply default on.

    Returns:
        Updated schema by default value or `default_factory`.
    )default_factoryr  )r  r  )rd  r!   Zwith_default_schemar  r  r    )r  r   rs   rs   rt   r    s   



r  GetJsonSchemaFunction | Nonec                 C  s   t | dd}t| dr)ddlm} |duo |jj|t |ddfv}|s)tddd	t| d
r?t| tt	t
df s?t| j|S |du rEdS |S )z`Extract `__get_pydantic_json_schema__` from a type, handling the deprecated `__modify_schema__`.__get_pydantic_json_schema__NZ__modify_schema__r   r[   __func__zkThe `__modify_schema__` method is not supported in Pydantic v2. Use `__get_pydantic_json_schema__` instead.zcustom-json-schemar{   
__origin__placeholder)r   r  Zpydanticr\   rf  rg  r/   rq   r   r"   re  r
  rh  )r   r   Zjs_modify_functionr\   Zhas_custom_v2_modify_js_funcrs   rs   rt   r
    s$   
 r
  c                   @  s>   e Zd ZU ded< ded< ded< ded< ded	< d
ed< dS )r  r   r   4str | list[str | int] | list[list[str | int]] | Noner  r   r  bool | Noner  r  zdict[str, Any]r  N)r   r   r   r.  rs   rs   rs   rt   r    s   
 r  )r  r  r  r  r  r  rj  r  r  rk  r  r  c                C  s   | |||||dS )Nr   r  r  r  r  r  rs   rl  rs   rs   rt   r    s   
r  c                   @  s(   e Zd ZdZdddZedd	d
ZdS )r   z*Keeps track of references and definitions.rn   ry   c                 C  s   t  | _i | _d S r   )r}   seenr   r   rs   rs   rt   r     s   
z_Definitions.__init__r   r   3Iterator[tuple[str, None] | tuple[str, CoreSchema]]c              	   c  sj    t |}|| jv s|| jv r|t|fV  dS | j| z|dfV  W | j| dS | j| w )ao  Get a definition for `tp` if one exists.

        If a definition exists, a tuple of `(ref_string, CoreSchema)` is returned.
        If no definition exists yet, a tuple of `(ref_string, None)` is returned.

        Note that the returned `CoreSchema` will always be a `DefinitionReferenceSchema`,
        not the actual definition itself.

        This should be called for any type that can be identified by reference.
        This includes any recursive types.

        At present the following types can be named/recursive:

        - BaseModel
        - Dataclasses
        - TypedDict
        - TypeAliasType
        N)rC   rm  r   r!   r7  adddiscard)r   r   r   rs   rs   rt   r'    s   z_Definitions.get_schema_or_refN)rn   ry   )r   r   rn   rn  )r   r   r   r   r   r   r'  rs   rs   rs   rt   r     s
    
r   r   dict[str, CoreSchema]CoreSchema | Nonec                 C  s4   | d dkr| | d d S | d dkr| d S | S )Nr   r:  r^  r   r   r   )r   r   rs   rs   rt   r    s
   r  )rj   rk   rl   rm   rn   ro   )rv   rw   rr   rx   rn   ry   )r   r   rl   rm   rn   r   )r   r   r   r   r   r   rn   r   )r   r@   r   r<   r   r   rn   r3   )r   r   r   r   r   r   rn   r   )r   r   r*  r^  r   r   rn   r   )r*  r`  rn   ro   )r   r   r*  rb  r  rc  rn   r   )r  r2   r   r   rn   r   )r   r   r   r   rn   re  )r   r   r  rj  r  r   r  rk  r  rk  r  r   rn   r  )r   r   r   rq  rn   rr  )r   
__future__r   Z_annotationscollections.abcrt  r	  r   rq  sysrw  r   
contextlibr   r   r   enumr   	functoolsr   r   r	   r
   	itertoolsr   operatorr   typesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zpydantic_corer   r    r!   Ztyping_extensionsr"   r#   r$   r%   r&   r'   r(   r)   r  r+   r,   errorsr-   r.   r/   rr   r0   r1   r2   r   r3   versionr4   r5   r  r7   r8   r9   r:   Z_annotated_handlersr;   r<   _configr=   Z_core_metadatar>   r?   Z_core_utilsr@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   r  rQ   rR   Z_forward_refrS   Z	_genericsrT   rU   rV   rW   Z_schema_generation_sharedrX   rY   _utilsrZ   ra  r\   r*  r]   r  r^   r_   version_infor  r   re  r_  rk   rc   ZAnyFieldDecoratorZModifyCoreSchemaWrapHandlerZGetCoreSchemaFunctionrX  Tuplere   r.  r   Listru  MutableSequencerf   r}   Set
MutableSetrg   	frozenset	FrozenSetrh   r   MutableMappingri   ru   r   r   r   r   ZJsonEncodersr   r   r   r\  Zfield_before_validator_functionZfield_after_validator_functionZfield_wrap_validator_functionr]  r   r  r1  r  r
  r  r  r   r  rs   rs   rs   rt   <module>   s    @(8





 
"           ~



"

/
'