o
    i~                     @  s   U d dl m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mZmZmZmZmZ d dlmZmZ d dlmZmZ d d	lmZ d d
lmZ er_d dlm Z  eddZ!dZ"de#d< z!d dlm Z m$Z$ d dl%m&Z& e& Z'dZ(e(e'j)v sJ dKddZ*W n e+y   dKddZ*Y nw dd Z,dLdd Z-dMd"d#Z.dNd'd(Z/dOd,d-Z0G d.d/ d/Z1e1 Z2ed0Z3dPd4d5Z4ed6Z5ed7Z6dQd>d?Z7zd d@l8m9Z9 W n e+y   G dAdB dBe:Z9Y nw ere!j;Z<nedRdDdEZ<dSdIdJZ=dS )T    )annotationsN)Callable	Generator)contextmanager)partial)TYPE_CHECKINGAny	TypeAliasTypeVarget_args
get_origin)LogfireLogfireSpan)	ParamSpecTypeIs)typing_objects)is_union_origin)Spanzpydantic-graph)
otel_scopezLogfireSpan | Spanr	   AbstractSpan)r   set_span_in_context)TraceContextTextMapPropagatortraceparentspanreturn
str | Nonec                 C  sB   t | tr| }n| j}|sJ t|}i }t|| |tdS )zGGet a string representing the span context to use for annotating spans. )
isinstancer   _spanr   TRACEPARENT_PROPAGATORinjectgetTRACEPARENT_NAME)r   	real_spancontextcarrier r&   [/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_graph/_utils.pyget_traceparent!   s   
r(   c                 C  s   d S Nr&   )r   r&   r&   r'   r(   0   s   c                  C  s6   zt  } W | S  ty   t  } t |  Y | S w r)   )asyncioget_event_loopRuntimeErrornew_event_loopset_event_loop)
event_loopr&   r&   r'   r+   5   s   
r+   tpr   tuple[Any, ...]c                 C  s.   t | r| j} t| }t|rt| S | fS )zeExtract the arguments of a Union type if `response_type` is a union, otherwise return an empty tuple.)r   is_typealiastype	__value__r   r   r   )r0   originr&   r&   r'   get_union_args>   s   
r5   tuple[Any, list[Any]]c                 C  s.   t | }t|rt| ^}}||fS | g fS )zStrip `Annotated` from the type if present.

    Returns:
        `(tp argument, ())` if not annotated, otherwise `(stripped type, annotations)`.
    )r   r   is_annotatedr   )r0   r4   inner_tpargsr&   r&   r'   unpack_annotatedK   s
   
r:   items	list[str]strc                 C  s2   t | dkr
| d S d| dd d | d  S )z.Join with a comma and 'and' for the last item.   r   z, Nz, and )lenjoin)r;   r&   r&   r'   	comma_andY   s   rB   frametypes.FrameType | Nonedict[str, Any] | Nonec                 C  s8   | dur| j  }r|jddkrt|S |jS dS dS )zAttempt to get the namespace where the graph was defined.

    If the graph is defined with generics `Graph[a, b]` then another frame is inserted, and we have to skip that
    to get the correct namespace.
    N__name__typing)f_back	f_globalsr!   get_parent_namespacef_locals)rC   backr&   r&   r'   rJ   b   s   
rJ   c                   @  s   e Zd ZdZdS )UnsetzUA singleton to represent an unset value.

    Copied from pydantic_ai/_utils.py.
    N)rF   
__module____qualname____doc__r&   r&   r&   r'   rM   t   s    rM   T
t_or_unset	T | Unset	TypeIs[T]c                 C  s   | t uS r)   )UNSET)rR   r&   r&   r'   is_set   s   rV   _P_RfuncCallable[_P, _R]r9   _P.argskwargs	_P.kwargsc                   sL   |rt  d t| g|R i |I d H S t  jd | g|R  I d H S r)   )r*   get_running_looprun_in_executorr   )rY   r9   r\   r&   r&   r'   r_      s   (r_   )LogfireNotConfiguredWarningc                   @  s   e Zd ZdS )r`   N)rF   rN   rO   r&   r&   r&   r'   r`      s    r`   "Generator[LogfireSpan, None, None]c               	   o  s    t  3 t jdtd tj| i |}|V  W d   n1 s#w   Y  W d   dS W d   dS 1 s;w   Y  dS )zCCreate a Logfire span without warning if logfire is not configured.ignore)categoryN)warningscatch_warningsfilterwarningsr`   _logfirer   )r9   r\   r   r&   r&   r'   logfire_span   s   
"rh   objdepthintc                C  s   t  }|du r
dS t|D ]}|j}|du r dS q|j D ]\}}|| u r,|  S q |j|jkrE|j D ]\}}|| u rD|  S q8dS )aU  Infer the variable name of an object from the calling frame's scope.

    This function examines the call stack to find what variable name was used
    for the given object in the calling scope. This is useful for automatic
    naming of objects based on their variable names.

    Args:
        obj: The object whose variable name to infer.
        depth: Number of stack frames to traverse upward from the current frame.

    Returns:
        The inferred variable name if found, None otherwise.

    Example:
        Usage should generally look like `infer_name(self, depth=2)` or similar.
    N)inspectcurrentframerangerH   rK   r;   rI   )ri   rj   target_frame_nameitemr&   r&   r'   infer_obj_name   s$   rs   )r   r   r   r   )r0   r   r   r1   )r0   r   r   r6   )r;   r<   r   r=   )rC   rD   r   rE   )rR   rS   r   rT   )rY   rZ   r9   r[   r\   r]   r   rX   )r9   r   r\   r   r   ra   )ri   r   rj   rk   r   r   )>
__future__r   _annotationsr*   rl   typesrd   collections.abcr   r   
contextlibr   	functoolsr   rG   r   r   r	   r
   r   r   logfire_apir   r   typing_extensionsr   r   typing_inspectionr   typing_inspection.introspectionr   opentelemetry.tracer   rg   r   __annotations__r   ,opentelemetry.trace.propagation.tracecontextr   r   r"   fieldsr(   ImportErrorr+   r5   r:   rB   rJ   rM   rU   rQ   rV   rW   rX   r_   logfire._internal.configr`   UserWarningr   rh   rs   r&   r&   r&   r'   <module>   sb     

	


		

	