o
    i                      @   s  d dl 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
mZ d dlZd dlmZ ejdk r:d dlmZmZ nd dlmZmZ ejdkrNd d	lmZ ndZedZeeed f Zd
d ZdedB defddZdefddZdefddZdefddZdefddZdefddZdefddZ defddZ!defddZ"defd d!Z#dedefd"d#Z$dedefd$d%Z%dedefd&d'Z&dedefd(d)Z'dedefd*d+Z(dedefd,d-Z)de*fd.d/Z+dS )0    N)Flag)	UnionType)	AnnotatedAnyUnionget_args
get_origin)is_class_and_subclass)      )NotRequiredRequired)r
      )TypeAliasTypec                 C   s   | t u S NNoneTypehint r   Z/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/annotations.pyis_nonetype   s   r   type_returnc                 C   sX   | t u s| tu r
dS | tu s| tu s| tu s| tu st| r dS t| }|t u p+|tu S )zChecks if a type is a union.TF)r   r   strintfloatboolis_annotatedr   )r   originr   r   r   is_union   s   (r    c                 C   
   t | dS )N__pydantic_core_schema__hasattrr   r   r   r   is_pydantic,      
r%   c                 C   s.   t | do| jdkot | dott| ddS )zQCheck if a type is a Pydantic secret type (SecretStr, SecretBytes, Secret, etc.).
__module__zpydantic.typesget_secret_valueN)r$   r'   callablegetattrr   r   r   r   is_pydantic_secret0   s   
r+   c                 C   r!   )N__dataclass_fields__r#   r   r   r   r   is_dataclass:   r&   r-   c                 C   s   t | to	t| dS )N_fields)r	   tupler$   r   r   r   r   is_namedtuple>   s   r0   c                 C   s
   t | S r   )attrshasr   r   r   r   is_attrsB   r&   r3   c                 C   s
   t | tS )z.Check if a type hint is an enum.Flag subclass.)r	   r   r   r   r   r   is_enum_flagF   s   
r4   c                 C   s   t | tu S r   )typeAnnotatedTyper   r   r   r   r   K   s   r   c                    s4   t | } t| rt fddt| D S t|  S )zIndicates if ``target_type`` is in a possibly annotated/unioned ``hint``.

    E.g. ``contains_hint(Union[int, str], str) == True``
    c                 3   s    | ]}t | V  qd S r   )contains_hint.0xtarget_typer   r   	<genexpr>V   s    z contains_hint.<locals>.<genexpr>)resolver    anyr   r	   )r   r<   r   r;   r   r7   O   s   
r7   c                 C   s\   t | } t| rtdd t| D S t| tsdS t| do-t| do-t| do-t| dS )zDetermine if a type annotation is a TypedDict.

    This is surprisingly hard! Modified from Beartype's implementation:

        https://github.com/beartype/beartype/blob/main/beartype/_util/hint/pep/proposal/utilpep589.py
    c                 s       | ]}t |V  qd S r   )is_typeddictr8   r   r   r   r=   d       zis_typeddict.<locals>.<genexpr>F__annotations__	__total____required_keys____optional_keys__)r>   r    r?   r   r	   dictr$   r   r   r   r   rA   [   s   

rA   c                 C   sT   | t jju rtS d}| |kr(| }t| } t| } t| } t| } t| } | |ks| S )z$Perform all simplifying resolutions.N)	inspect	Parameteremptyr   resolve_type_aliasresolve_annotatedresolve_optionalresolve_requiredresolve_new_type)r   	type_prevr   r   r   r>   q   s   r>   c                 C   sp   t | } t| s
| S dd t| D }|stdt|dkr%|d } | S t|dkr6ttdd |D  S t)z?Only resolves Union's of None + one other type (i.e. Optional).c                 S   s   g | ]}|t ur|qS r   r   )r9   tr   r   r   
<listcomp>   s    z$resolve_optional.<locals>.<listcomp>z!Union type cannot be all NoneType   r   c                 s   r@   r   )rM   r8   r   r   r   r=      rB   z#resolve_optional.<locals>.<genexpr>)rK   r    r   
ValueErrorlenr   r/   NotImplementedError)r   non_none_typesr   r   r   rM      s   rM   c                 C   s$   t | } t| tu rt| d } | S Nr   )rK   r5   r6   r   r   r   r   r   rL      s   rL   c                 C   s    t | ttfv rt| d } | S rX   )r   r   r   r   rY   r   r   r   rN      s   rN   c                 C   s$   zt | jW S  ty   |  Y S w r   )rO   __supertype__AttributeErrorrY   r   r   r   rO      s
   rO   c                 C   s   t durt| t r| jS | S )zMResolve TypeAliasType (Python 3.12+ 'type' statement) to its underlying type.N)r   
isinstance	__value__rY   r   r   r   rK      s   rK   c                 C   s   t | tr| S t| rdS | tu rdS t| r#ddd t| D S t|  }rEt|}t|  }rC|dddd |D  d	 7 }|S t	| d
rM| j
S t| dd d urX| jS t| S )NNoner   |c                 s   r@   r   get_hint_namer9   argr   r   r   r=      rB   z get_hint_name.<locals>.<genexpr>[z, c                 s   r@   r   r`   rb   r   r   r   r=      rB   ]__name___name)r\   r   r   r   r    joinr   r   ra   r$   rf   r*   rg   )r   r   outargsr   r   r   ra      s$   
 
ra   ),rH   sysenumr   typesr   typingr   r   r   r   r   r1   cyclopts.utilsr	   version_infotyping_extensionsr   r   r   r5   r   r   r6   r   r   r    r%   r+   r-   r0   r3   r4   r   r7   rA   r>   rM   rL   rN   rO   rK   r   ra   r   r   r   r   <module>   sJ    



