
    ii                    4   d dl mZ d dlZd dlm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 dlmZ d dlmZmZ d d	lmZmZmZmZmZmZ d d
lmZ erd dlmZ d%dZ ed          d&d            Z d%dZ!d'd(dZ"d)dZ#d'd*dZ$d'd+dZ%d'd(dZ&d,dZ'd-d!Z(d.d$Z)dS )/    )annotationsN)deque)is_dataclass)TYPE_CHECKINGAnyMappingSequence)get_args
get_origin)TYPE_MAPPING)check_for_deprecated_parameters
deprecated)is_annotatedis_new_typeis_optionalis_safe_subclassis_type_aliasis_unionNoneType)Random
annotationr   returnc                P    t          |           r| j        } t          |           | S )zReturn base type if given annotation is a type derived with NewType, otherwise annotation.

    :param annotation: A type annotation, possibly one created using 'types.NewType'

    :returns: The unwrapped annotation.
    )r   __supertype__r   s    q/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/polyfactory/utils/helpers.pyunwrap_new_typer      s6     j
!
! .-
 j
!
! .     zv2.21.0randomr   c                    t          |           r@t          t          |                     }|                    |          } t          |           @| S )zUnwraps union types recursively and picks a random type from each union.

    :param annotation: A type annotation, possibly a type union.
    :param random: An instance of random.Random.
    :returns: A type annotation
    )r   listr
   choicer   r    argss      r   unwrap_unionr&   '   sT     :

 )HZ(())]]4((
 :

 ) r   c                    t          |           r5t          d t          |           D                       } t          |           5| S )zUnwraps optional union types - recursively.

    :param annotation: A type annotation, possibly an optional union.

    :returns: A type annotation
    c              3  0   K   | ]}|t           d fv|V  d S Nr   .0args     r   	<genexpr>z"unwrap_optional.<locals>.<genexpr>=   s2      ]]#XW[L\A\A\#A\A\A\A\]]r   )r   nextr
   r   s    r   unwrap_optionalr/   5   sR     j
!
! ^]]*)=)=]]]]]
 j
!
! ^r   Random | Nonec                   t          dd|ff           t          |           s-t          |           st          |           st	          |           rt          |           rt          |           } nZt          |           rt          |           } n;t          |           rt          |           d         } nt	          |           r| j        } t          |           t          |           t          |           t	          |           | S )zUnwraps an annotation.

    :param annotation: A type annotation.
    :param random: An instance of random.Random.

    :returns: The unwrapped annotation.

    2.21.0r    
parametersr   )	r   r   r   r   r   r   r/   unwrap_annotated	__value__r   r    s     r   unwrap_annotationr8   A   s!    $H8V:L9NOOOO
j
!
! .[%<%< .Z@X@X .\ijt\u\u .z"" 	.(44JJ$$ 	.(44JJ*%% 	.)*55a8JJ:&& 	.#-J j
!
! .[%<%< .Z@X@X .\ijt\u\u . r   	list[Any]c                L   g }t          |           r0|                    t          t          |                                nt	          |           r5t          |           D ]$}|                    t          |                     %nt          |           r6|                    t          t          |           d                              nYt          |           r5t          |           D ]$}|                    t          |                     %n|                    |            |S )uD  Flattens an annotation into an array of possible types. For example:

    * Union[str, int] → [str, int]
    * Optional[str] → [str, None]
    * Union[str, Optional[int]] → [str, int, None]
    * NewType('UserId', int) → [int]

    :param annotation: A type annotation.

    :returns: The flattened annotations.
    r   )	r   extendflatten_annotationr   r   r
   r   r   append)r   flatas      r   r<   r<   X   s'    D:  &z'B'BCCDDDD	Z	 	  	 *%% 	/ 	/AKK*1--....	/	j	!	!  &x
';';A'>??@@@@	*		  *%% 	/ 	/AKK*1--....	/ 	JKr   tuple[Any, ...]c                d    t          dd|ff           t          t          |                     S )zUnwrap the annotation and return any type args.

    :param annotation: A type annotation
    :param random: An instance of random.Random.

    :returns: A tuple of type args.

    r2   r    r3   r   r   r
   r8   r7   s     r   unwrap_argsrC   u   s9     $H8V:L9NOOOO%<<<===r   tuple[Any, list[Any]]c                    t          dd|ff           d t          |           D             }t          |d                   |dd         fS )zUnwrap an annotated type and return a tuple of type args and optional metadata.

    :param annotation: An annotated type annotation
    :param random: An instance of random.Random.

    :returns: A tuple of type args.

    r2   r    r3   c                    g | ]}||S r)    r*   s     r   
<listcomp>z$unwrap_annotated.<locals>.<listcomp>   s    CCCC3?C???r   r      NrB   r$   s      r   r5   r5      sY     $H8V:L9NOOOOCC8J//CCCDT!W%%tABBx//r   c                   t          dd|ff           t          |           rt          |           } t          |           rt	          |           d         } t
          j        dk     r| S t          |           p| }|t          v rt          |         }t          |           x}r,t          d |D                       }|t          ur||         n| S |S )zvNormalize an annotation.

    :param annotation: A type annotation.

    :returns: A normalized type annotation.

    r2   r    r3   r   )   	   c              3  4   K   | ]}t          |          V  d S r)   )normalize_annotationr*   s     r   r-   z'normalize_annotation.<locals>.<genexpr>   s+      ??3)#..??????r   )r   r   r   r   r5   sysversion_infor   r   r
   tupletype)r   r    originr%   s       r   rN   rN      s     $H8V:L9NOOOO: 1$Z00
J 5%j11!4
 &  
##1zFf%
###t B??$?????%T11vd||zAMr   Sequence[Any]c                0    t          |           dd         S )zpGet the metadata in the annotation.

    :param annotation: A type annotation.

    :returns: The metadata.
    rI   N)r
   r   s    r   get_annotation_metadatarV      s     J##r   3type[list | tuple | set | frozenset | dict | deque]c                   t          | t                    rt          S t          | t                    rt          S t          | t                    rt          S t          | t
                    rt
          S t          | t                    rt          S t          | t                    rt          S t          | t                    rt          S d|  }t          |          )zGet the collection type from the annotation.

    :param annotation: A type annotation.

    :returns: The collection type.
    zUnknown collection type - )
r   r"   r   dictrQ   set	frozensetr   r	   
ValueError)r   msgs     r   get_collection_typer^      s     
D)) 
G,, 
E** 
C(( 

I.. 
E** 
H-- 
3z
3
3C
S//r   objboolc                L    t          |           ot          | t                     S r)   )r   
isinstancerR   )r_   s    r   is_dataclass_instancerc      s"    :ZT%:%:!::r   )r   r   r   r   )r   r   r    r   r   r   r)   )r   r   r    r0   r   r   )r   r   r   r9   )r   r   r    r0   r   r@   )r   r   r    r0   r   rD   )r   r   r   rT   )r   r   r   rW   )r_   r   r   r`   )*
__future__r   rO   collectionsr   dataclassesr   typingr   r   r   r	   typing_extensionsr
   r   polyfactory.constantsr   polyfactory.utils.deprecationr   r   polyfactory.utils.predicatesr   r   r   r   r   r   polyfactory.utils.typesr   r    r   r   r&   r/   r8   r<   rC   r5   rN   rV   r^   rc   rG   r   r   <module>rm      s:   " " " " " " 



       $ $ $ $ $ $ 8 8 8 8 8 8 8 8 8 8 8 8 2 2 2 2 2 2 2 2 . . . . . . U U U U U U U U                - , , , , , 
 
 
 
 I
 
 
 
	 	 	 	    .   :
> 
> 
> 
> 
>0 0 0 0 0    @$ $ $ $   6; ; ; ; ; ;r   