
    iC                        U d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
 ddlmZmZ dZdZd	ed
<   ded	 	 	 	 	 	 	 ddZded	 	 	 	 	 	 	 ddZddZd ZddZddZddZ G d d      Zy)    )annotationsN)Iterable)AnyCallableFinalOptional   )inspectto_builtins)schemaschema_components#/$defs/{name}r   _REF_TEMPLATEschema_hookref_templatec               <    t        | f||      \  \  }}|r||d<   |S )a  Generate a JSON Schema for a given type.

    Any schemas for (potentially) shared components are extracted and stored in
    a top-level ``"$defs"`` field.

    If you want to generate schemas for multiple types, or to have more control
    over the generated schema you may want to use ``schema_components`` instead.

    Parameters
    ----------
    type : type
        The type to generate the schema for.
    schema_hook : callable, optional
        An optional callback to use for generating JSON schemas of custom
        types. Will be called with the custom type, and should return a dict
        representation of the JSON schema for that type.
    ref_template : str, optional
        A template to use when generating ``"$ref"`` fields. This template is
        formatted with the type name as ``template.format(name=name)``. This
        can be useful if you intend to store the ``components`` mapping
        somewhere other than a top-level ``"$defs"`` field. For example, you
        might use ``ref_template="#/components/{name}"`` if generating an
        OpenAPI schema.

    Returns
    -------
    schema : dict
        The generated JSON Schema.

    See Also
    --------
    schema_components
    r   z$defs)r   )typer   r   out
componentss        G/root/searxng-venv/lib/python3.12/site-packages/msgspec/_json_schema.pyr   r      s4    N +	!FSJ
 !GJ    c          	       
 t        j                  |       }t        |      }t        |      }t	        |||      
t        
fd|D              }|j                         D ci c]  \  }}||   
j                  |d       }	}}||	fS c c}}w )a  Generate JSON Schemas for one or more types.

    Any schemas for (potentially) shared components are extracted and returned
    in a separate ``components`` dict.

    Parameters
    ----------
    types : Iterable[type]
        An iterable of one or more types to generate schemas for.
    schema_hook : callable, optional
        An optional callback to use for generating JSON schemas of custom
        types. Will be called with the custom type, and should return a dict
        representation of the JSON schema for that type.
    ref_template : str, optional
        A template to use when generating ``"$ref"`` fields. This template is
        formatted with the type name as ``template.format(name=name)``. This
        can be useful if you intend to store the ``components`` mapping
        somewhere other than a top-level ``"$defs"`` field. For example, you
        might use ``ref_template="#/components/{name}"`` if generating an
        OpenAPI schema.

    Returns
    -------
    schemas : tuple[dict]
        A tuple of JSON Schemas, one for each type in ``types``.
    components : dict
        A mapping of name to schema for any shared components used by
        ``schemas``.

    See Also
    --------
    schema
    c              3  @   K   | ]  }j                  |        y wN)	to_schema).0tgens     r   	<genexpr>z$schema_components.<locals>.<genexpr>o   s     9CMM!$9s   F)mimulti_type_info_collect_component_types_build_name_map_SchemaGeneratortupleitemsr   )typesr   r   
type_infoscomponent_typesname_mapschemasclsr   r   r   s             @r   r   r   @   s    N ##E*J.z:O/H
8[,
?C9j99G >M=R=R=T393s}}Q..J  Js   ! Bc                8    i fd| D ]
  } |        S )zFind all types in the type tree that are "nameable" and worthy of being
    extracted out into a shared top-level components mapping.

    Currently this looks for Struct, Dataclass, NamedTuple, TypedDict, and Enum
    types.
    c                p   t        | t        j                  t        j                  t        j                  t        j
                  f      rB| j                  vr3| | j                  <   | j                  D ]  } |j                          y y t        | t        j                        r| | j                  <   y t        | t        j                        r | j                         y t        | t        j                        r | j                         y t        | t        j                        r| j                  D ]
  } |        y t        | t        j                        r% | j                           | j"                         y t        | t        j$                        r| j&                  D ]
  } |        y y r   )
isinstancer!   
StructTypeTypedDictTypeDataclassTypeNamedTupleTyper-   fieldsr   EnumTypeMetadataCollectionType	item_type	TupleType
item_typesDictTypekey_type
value_type	UnionTyper(   )r   fstcollectr   s      r   rB   z)_collect_component_types.<locals>.collect   s<   r//1A1A2CTCTU
 uuJ&$%
155! $AAFFO$ ' 2;;' !Jquu2;;'AFFO2,,-AKK 2<<(ll 2;;'AJJALL!2<<(gg  )r    )r)   r   rB   r   s     @@r   r#   r#   w   s/     J0  
 r   c                P    t        | t              r| j                  S t        |       S r   )r0   r   __name__repr)objs    r   
_type_reprrH      s    %c403<<?d3i?r   c                    t        | d      r@| j                  j                  }dj                  d | j                  D              }| d| dS | j                  S )N
__origin__z, c              3  2   K   | ]  }t        |        y wr   )rH   )r   as     r   r    z"_get_class_name.<locals>.<genexpr>   s     =1A=s   [])hasattrrJ   rE   join__args__)r-   nameargss      r   _get_class_namerT      sP    sL!~~&&yy===qa  <<r   c                   t        | d      sJ t        | j                  d| j                        }t        |dd      }|syt        j                  |      j                  d      }t        | t        j                        r|dk(  ry|S t        | t        j                  t        j                  f      r0|j                  |j                   d      r|j                  d      ry|S )	Nr-   rJ   __doc__ z
zAn enumeration.())rO   getattrr-   textwrapdedentstripr0   r!   r6   r4   r3   
startswithrE   endswith)r   r-   docs      r   _get_docra      s    1e
!%%quu
-C
#y"
%C
//#

$
$V
,C!R[[!## J 
A))2+;+;<	=>>S\\N!,-#,,s2CJr   c                <  	 d 		fd}t               }i }| D ]Y  } 	t        |            }||v r-|j                  |      }|j                  |       || ||      <   ||v r|| ||      <   U|||<   [ |j	                         D ci c]  \  }}||
 c}}S c c}}w )zA mapping from nameable subcomponents to a generated name.

    The generated name is usually a normalized version of the class name. In
    the case of conflicts, the name will be expanded to also include the full
    import path.
    c                0    t        j                  dd|       S )Nz[^a-zA-Z0-9.\-_]_)resubrR   s    r   	normalizez"_build_name_map.<locals>.normalize   s    vv)355r   c                F     | j                    d| j                         S )N.)
__module____qualname__)r-   rh   s    r   fullnamez!_build_name_map.<locals>.fullname   s%    CNN+1S-=-=,>?@@r   )setrT   popaddr'   )
r*   rm   	conflictsnamesr-   rR   oldkvrh   s
            @r   r$   r$      s    6A IE 	-.5=))D/CMM$#&E(3- 9#&E(3- E$K	 #[[]+TQAqD+++s   Bc                  .    e Zd Z	 	 d	 	 	 	 	 ddZdddZy)r%   Nc                .    || _         || _        || _        y r   )r+   r   r   )selfr+   r   r   s       r   __init__z_SchemaGenerator.__init__   s     !&(r   c           	        i }t        |t        j                        rGt        j                  ||j                        }|j
                  }t        |t        j                        rG|rTt        |d      rH| j                  j                  |j                        x}r!| j                  j                  |      |d<   |S t        |t        j                  t        j                  f      r	 |S t        |t        j                        rd|d<   |S t        |t        j                        rd|d<   |S t        |t        j                   t        j"                  f      rt        |t        j                         rdnd|d<   |j$                  |j$                  |d
<   |j&                  |j&                  |d<   |j(                  |j(                  |d<   |j*                  |j*                  |d<   |j,                  |j,                  |d<   |S t        |t        j.                        rXd|d<   |j0                  |j0                  |d<   |j2                  |j2                  |d<   |j4                  |j4                  |d<   |S t        |t        j6                  t        j8                  t        j:                  f      rTd|d<   d|d<   |j0                  d|j0                  dz   dz  z  |d<   |j2                  d|j2                  dz   dz  z  |d<   |S t        |t        j<                        rd|d<   |j>                  du rd|d<   |S t        |t        j@                        r/d|d<   |j>                  du rd|d<   |S |j>                  du rd|d<   |S t        |t        jB                        rd|d<   d|d<   |S t        |t        jD                        rd|d<   d|d<   |S t        |t        jF                        rd|d<   d |d<   |S t        |t        jH                        rd|d<   d!|d<   |S t        |t        jJ                        rd"|d<   t        |jL                  t        j                        s| jO                  |jL                        |d#<   |j0                  |j0                  |d$<   |j2                  |j2                  |d%<   t        |t        jP                  t        jR                  f      rd|d&<   |S t        |t        jT                        rad"|d<   tW        |jX                        x|d%<   |d$<   |jX                  r1|jX                  D cg c]  }| jO                  |       c}|d'<   d|d#<   |S t        |t        jZ                        rd(|d<   t        |j\                  x}t        j.                        rZi }|j2                  |j2                  |d<   |j0                  |j0                  |d<   |j4                  |j4                  |d<   |r||d)<   t        |j^                  t        j                        s| jO                  |j^                        |d*<   |j0                  |j0                  |d+<   |j2                  |j2                  |d,<   |S t        |t        j`                        ri }g }	d	}
|jb                  D ]  }|}t        |t        j                        r'|j
                  }t        |t        j                        r't        |t        jd                        r(|jf                  s|jh                  }
|||jj                  <   |	jm                  |        |	D cg c]  }| jO                  |       }}tW        |      dk\  r|jo                         D ci c]9  \  }}|| j                  j                  | j                  |j                           ; }}}|jq                         D cg c]  }| jO                  |       c}|
|d-d.}|r|jm                  |       ||d/<   |S |js                  |       |S tW        |      d0k(  r:|ju                         \  }}|jm                  | jO                  |             ||d/<   |S ||d/<   |S t        |t        jv                        rty        |jp                        |d1<   |S t        |t        jz                        rf|j}                  d2|j                  j~                         t        |      x}r|j}                  d3|       ty        d4 |j                  D              |d1<   |S t        |t        jd                        rD|j}                  d2t        |j                               t        |      x}r|j}                  d3|       g }g }g }|jh                  T|jm                  |jh                         |jm                  |jh                         |jm                  d1|jj                  gi       |j                  D ]  }| jO                  |j
                        }|j                  r|jm                  |j                         nl|j                  t        j                  urt        |j                  d5      |d6<   n5|j                  t        t        t        t        fv r|j                         |d6<   |jm                  |j                         |jm                  |        |jf                  rmd7}t        t        |j                              D ]  \  }}|j                  s n d"|d<   ||d'<   tW        |      |z
  |d%<   |j                  rCtW        |      |d$<   |S d(|d<   t        t        ||            |d8<   ||d9<   |j                  rd|d*<   |S t        |t        j                  t        j                  t        j                  f      rf|j}                  d2t        |j                               t        |      x}r|j}                  d3|       g }g }g }|j                  D ]  }| jO                  |j
                        }|j                  r|jm                  |j                         n6|j                  t        j                  urt        |j                  d5      |d6<   |jm                  |j                         |jm                  |        t        |t        j                        r(d"|d<   ||d'<   tW        |      |d%<   tW        |      |d$<   |S d(|d<   t        t        ||            |d8<   ||d9<   |S t        |t        j                        rt        d:      t        |t        j                        rY| j                  r0	 t        j                  | j                  |j                        |      }|st        d;|j                  d<      |S t        d=|      c c}w c c}w c c}}w c c}w # t        $ r Y Kw xY w)>z!Converts a Type to a json-schema.r-   rg   z$refnullr   booleanintegernumberNminimumexclusiveMinimummaximumexclusiveMaximum
multipleOfstring	maxLength	minLengthpatternbase64contentEncoding         Tz	date-timeformattimeFzpartial-timedatedurationuuiddecimalarrayr'   maxItemsminItemsuniqueItemsprefixItemsobjectpropertyNamesadditionalPropertiesmaxPropertiesminProperties)propertyNamemapping)anyOfdiscriminatorr   r	   enumtitledescriptionc              3  4   K   | ]  }|j                     y wr   )value)r   es     r   r    z-_SchemaGenerator.to_schema.<locals>.<genexpr>t  s     #;AGG#;s   )str_keysdefaultr   
propertiesrequiredz-json-schema doesn't support msgpack Ext typeszGenerating JSON schema for custom types requires either:
- specifying a `schema_hook`
- annotating the type with `Meta(extra_json_schema=...)`

type z is not supportedz!json-schema doesn't support type )Yr0   r!   r7   _merge_jsonextra_json_schemar   rO   r+   getr-   r   r   AnyTypeRawTypeNoneTypeBoolTypeIntType	FloatTypegegtleltmultiple_ofStrType
max_length
min_lengthr   	BytesTypeByteArrayTypeMemoryViewTypeDateTimeTypetzTimeTypeDateTypeTimeDeltaTypeUUIDTypeDecimalTyper8   r9   r   SetTypeFrozenSetTyper:   lenr;   r<   r=   r>   r?   r(   r1   
array_like	tag_fieldtagappendr'   valuesupdatepopitemLiteralTypesortedr6   
setdefaultrE   ra   rT   r5   r   encode_namer   	NODEFAULTr   default_factorylistdictrn   	bytearray	enumeratereversedforbid_unknown_fieldszipr2   r3   r4   ExtType	TypeError
CustomTyper   NotImplementedError)rx   r   	check_refr   rR   ir=   property_namesstructsotherr   subtype	real_typerL   optionsrt   ru   r   struct_schemard   r`   r   rr   r5   fieldfield_scheman_trailing_defaultsr@   s                               r   r   z_SchemaGenerator.to_schema   s   !#BKK(^^FA,?,?@FA BKK( E*}}((//t/!%!2!2!9!9t!9!Dva"**bjj12p o 2;;'#F6Nl k 2;;'&F6Nh g BJJ56*4Q

*CYF6Ntt$%DDy!tt-.TT)*tt$%DDy!tt-.TT)*}}('(}}|$P O 2::&%F6N||'&'ll{#||'&'ll{#yy$$%IIy!@  BLL"*:*:B<M<MNO%F6N(0F$%||'&'ALL1,<+B&C{#||'&'ALL1,<+B&C{#r q 2??+%F6Nttt|#.x j i 2;;'%F6Nttt|#)x b a #1x ^ ] 2;;'%F6N%F8X W 2++,%F6N)F8R Q 2;;'%F6N%F8L K 2>>*%F6N(F8F E 2,,-$F6Nakk2::6"&.."=w||'%&\\z"||'%&\\z"!bjj"*:*:;<(,}%r q 2<<($F6N69!,,6GGF:
!3||DELL(Qq):(Q}%"'wf e 2;;'%F6Najj0("**=13&&22:2E2EN;/&&22:2E2EN;/##/080@0@N9-!.<F?+allBJJ7151M-.||'*+,,'||'*+,,'@  2<<(GEI77 *#	 BKK8 )I !BKK8i7	@T@T ) 3 3I-6GIMM*LL)* 388Qt~~a(8G87|q  !(1 t((//T]]1555I/JJ 
 :A9IJAdnnQ/J6?G%T! NN=1&-F7OH E MM-0D C W"$__.
7t~~g67")w| y #*wx w 2>>*#AHH-F6Nt s 2;;'gquu~~6qk!s!!!-5##;QUU#;;F6Nj i 2==)gquu'=>qk!s!!!-5HEF{{&,Q[[)vw/0 	,#~~ejj9>>OOE$5$56]]",,6.9%--RV.WL+**tT3	.JJ.3.C.C.EL+U../l+	, ||&'#.78J.K *'zz ")v(.}%%([3F%Fz"**),VF:&f c "*v'+Cv,>'?|$%-z"**5:F12Z Y B,,b.>.>@Q@QRSgquu'=>qk!s!!!-5EFH ,#~~ejj9>>OOE$5$56]]",,6.9%--RV.WL+U../l+, !R../!(v(.}%%(]z"%([z"2 / "*v'+Cv,>'?|$%-z"* ) 2::&KLL2==)^^D,<,<QUU,CVLF  EE9$5	7   ?uEFFe )RF 9
 Kv + s*   /v=9w4>ww#/w 	ww)Nr   )r+   dict[Any, str]r   *Optional[Callable[[type], dict[str, Any]]]r   str)T)r   mi.Typer   boolreturndict[str, Any])rE   rk   rl   ry   r   rC   r   r   r%   r%      s4     CG,	) ) @) 	)fr   r%   )r   r   r   r   r   r   r   r   )r(   zIterable[Any]r   r   r   r   r   z1tuple[tuple[dict[str, Any], ...], dict[str, Any]])r)   zIterable[mi.Type]r   dict[Any, mi.Type])r-   r   r   r   )r   r   r   r   )r*   r   r   r   )
__future__r   re   r[   collections.abcr   typingr   r   r   r   rW   r
   r!   r   __all__r   __annotations__r   r   r#   rH   rT   ra   r$   r%   rC   r   r   <module>r      s    " 	  $ 1 1 (
)'u ' ?C%	.
. <. 	.
 .h ?C%	44 <4 	4
 74n$N@ ,<q qr   