o
    "i'                     @   s   d dl mZ d dlmZmZmZ d dlmZmZ ddl	m
Z
 ddlmZmZmZmZ ddlmZ g dZd	ed
efddZedefdede
deegef dedeeef d
dfddZ	ddede
ded
dfddZdS )    )defaultdict)AnyCallableUnion)NOTHINGNothingType   )BaseConverter)get_newtype_base
is_literalis_subclassis_union_type)is_type_alias)configure_tagged_unionconfigure_union_passthroughdefault_tag_generatortypreturnc                 C   s   | j S )zReturn the class name.)__name__)r    r   Y/var/www/html/premium_crap/venv/lib/python3.10/site-packages/cattrs/strategies/_unions.pyr      s   r   _typeunion	convertertag_generatortag_namedefaultNc                    s  t | r| j} | j}i }i }|D ]/}|}	||}
||}||
fdtd|fdd}|fd| dtfdd}|||	< |||< qfdd|D } turn| } |fdtfd	d
tfdd|}t fdd|}|||fd| dtfdd} tu rt|ddr||fdtd| fdd}n0||fdtd| fdd}n#t|ddr||| fdtd| fdd}n||| fdtd| fdd}|	| | |
| | dS )a  
    Configure the converter so that `union` (which should be a union, or a type alias
    of one) is un/structured with the help of an additional piece of data in the
    unstructured payload, the tag.

    :param converter: The converter to apply the strategy to.
    :param tag_generator: A `tag_generator` function is used to map each
        member of the union to a tag, which is then included in the
        unstructured payload. The default tag generator returns the name of
        the class.
    :param tag_name: The key under which the tag will be set in the
        unstructured payload. By default, `'_type'`.
    :param default: An optional class to be used if the tag information
        is not present when structuring.

    The tagged union strategy currently only works with the dict
    un/structuring base strategy.

    .. versionadded:: 23.1.0

    ..  versionchanged:: 25.1
        Type aliases of unions are now also supported.
    valr   c                 S   
   || |S Nr   r   _cl_hr   r   r   structure_union_member>      
z6configure_tagged_union.<locals>.structure_union_memberc                 S   s   || S r   r   )r   r"   r   r   r   unstructure_union_memberA   s   z8configure_tagged_union.<locals>.unstructure_union_memberc                    s   i | ]}| |qS r   r   ).0cl)r   r   r   
<dictcomp>G   s    z*configure_tagged_union.<locals>.<dictcomp>c                 S   r   r   r   r    r   r   r   structure_defaultL   r$   z1configure_tagged_union.<locals>.structure_defaultc                          S r   r   r   )r)   r   r   <lambda>O       z(configure_tagged_union.<locals>.<lambda>c                      r*   r   r   r   )r   r   r   r+   P   r,   c                 S   s    || j  | }|| j  ||< |S r   )	__class__)r   _exact_cl_unstruct_hooks
_cl_to_tag	_tag_nameresr   r   r   unstructure_tagged_unionR   s   z8configure_tagged_union.<locals>.unstructure_tagged_unionforbid_extra_keysFc                 S   s   |   } || | | S r   copypopr   _
_tag_to_clr0   r   r   r   structure_tagged_union_   s   z6configure_tagged_union.<locals>.structure_tagged_unionc                 S   s   || |  | S r   r   r7   r   r   r   r:   g   s   c                 S   s,   || v r|   } || | | S || |S r   r4   r   r8   _tag_to_hookr0   _dh_defaultr   r   r   r:   o   s   
c                 S   s"   || v r|| |  | S || |S r   r   r;   r   r   r   r:   ~   s   
N)r   	__value____args__get_structure_hookget_unstructure_hookdictr   r   getattrregister_unstructure_hookregister_structure_hook)r   r   r   r   r   argstag_to_hookexact_cl_unstruct_hooksr'   tagstruct_handlerunstruct_handlerr#   r%   	cl_to_tagdefault_handlerr2   r:   r   )r   r)   r   r   r      s|   





	
r   Taccept_ints_as_floatsc                    sJ   t | jdtdtf fdd}dtdtffdd}|| dS )ae  
    Configure the converter to support validating and passing through unions of the
    provided types and their subsets.

    For example, all mature JSON libraries natively support producing unions of ints,
    floats, Nones, and strings. Using this strategy, a converter can be configured
    to efficiently validate and pass through unions containing these types.

    The most important point is that another library (in this example the JSON
    library) handles producing the union, and the converter is configured to just
    validate it.

    Literals of provided types are also supported, and are checked by value.

    NewTypes of provided types are also supported.

    The strategy is designed to be O(1) in execution time, and independent of the
    ordering of types in the union.

    If the union contains a class and one or more of its subclasses, the subclasses
    will also be included when validating the superclass.

    :param accept_ints_as_floats: When set (the default), if the provided union
        contains both ints and floats, actual unions containing only floats will also accept
        ints. See https://typing.python.org/en/latest/spec/special-types.html#special-cases-for-float-and-complex
        for more information.

    .. versionadded:: 23.2.0
    .. versionchanged:: 25.2.0
        Introduced the `accept_ints_as_floats` parameter.
    
exact_typer   c                    s   dd | j D }dd | j D  fdd| j D fddD O fdd| j D }rFtv rFtv rFtv rFtvrFt |rnt|dkrTtt| ntt|}||fdt	d	t	d
| f fdd}|S |fdt	d	t	d
| f fdd}|S )Nc                 S   s$   h | ]}t |r|jD ]}|qqS r   )r   r@   r&   tvr   r   r   	<setcomp>   s    zSconfigure_union_passthrough.<locals>.make_structure_native_union.<locals>.<setcomp>c                 S   &   h | ]}t |r|jD ]}|jqqS r   r   r@   r-   rQ   r   r   r   rT          c                    s0   h | ]}t |st|p| v rt|p|qS r   r   r
   r&   rR   rG   r   r   rT      s    
c                    s&   h | ] t  fd dD r qS )c                 3   s    | ]}t  |V  qd S r   )r   )r&   car   r   	<genexpr>   s    z]configure_union_passthrough.<locals>.make_structure_native_union.<locals>.<setcomp>.<genexpr>)any)r&   non_literal_classesr\   r   rT      s
    c                    s(   h | ]}t |p	| vrt|s|qS r   )r
   r   )r&   r]   r`   r   r   rT      s       r   r8   r   c                    s0   | j  v r| |v r| S | j |v r| S || |S r   )r-   	structure)r   r8   classesvalsr   	spilloverliteral_classesr   r   structure_native_union   s
   
z`configure_union_passthrough.<locals>.make_structure_native_union.<locals>.structure_native_unionc                    s>   | j  v r| |v r| S | j |v r| S t|  d| j  d| )Nz (z) not part of )r-   	TypeError)r   r8   rd   re   rg   r   r   ri      s
   
)
r@   intfloataddlenr   tuplenextiterr   )rP   literal_valuesrf   spillover_typeri   rO   rG   r   )rh   ra   r   make_structure_native_union   s\   

	
"	z@configure_union_passthrough.<locals>.make_structure_native_unionc                    sZ   t | r+t| j}t|dkrtd|v rdS dd |D }dd |D }||B  @ S dS )zCan we handle this type?r   NFc                 S   rU   r   rV   )r&   rR   lit_argr   r   r   rT     rW   zMconfigure_union_passthrough.<locals>.contains_native_union.<locals>.<setcomp>c                 S   s    h | ]}t |st|p|qS r   rX   rY   r   r   r   rT     s
    
)r   setr@   rn   type)rP   	type_argsrh   non_literal_typesrZ   r   r   contains_native_union  s   
z:configure_union_passthrough.<locals>.contains_native_unionN)rw   r@   r   r   boolregister_structure_hook_factory)r   r   rO   ru   r{   r   rt   r   r      s   
"Qr   )T)collectionsr   typingr   r   r   attrsr   r    r	   _compatr
   r   r   r   typealiasesr   __all__rx   strr   r   r|   r   r   r   r   r   <module>   sD    

y