
    ,hN2                    
   U d dl mZ d dlZd dlmZ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 d dlmZmZmZmZ dZd	ed
<   	 eeef   Zd	ed<   	 dZd	ed<    G d de      ZddZ G d deeef         ZdddZ G d d      Zy)    )annotationsN)	GeneratorIteratorMapping)contextmanager)cached_property)AnyCallable
NamedTupleTypeVar)	ParamSpec	TypeAliasTypeAliasTypeTypeVarTupledict[str, Any]r   GlobalsNamespaceMappingNamespacez"TypeVar | ParamSpec | TypeVarTuple_TypeVarLikec                  (    e Zd ZU dZded<   	 ded<   y)NamespacesTuplea2  A tuple of globals and locals to be used during annotations evaluation.

    This datastructure is defined as a named tuple so that it can easily be unpacked:

    ```python {lint="skip" test="skip"}
    def eval_type(typ: type[Any], ns: NamespacesTuple) -> None:
        return eval(typ, *ns)
    ```
    r   globalsr   localsN)__name__
__module____qualname____doc____annotations__     e/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/pydantic/_internal/_namespace_utils.pyr   r      s     [Zr   r   c                    t        | dd      }|r	 t        j                  |   j                  S i S # t        $ r i cY S w xY w)zGet the namespace of the module where the object is defined.

    Caution: this function does not return a copy of the module namespace, so the result
    should not be mutated. The burden of enforcing this is on the caller.
    r   N)getattrsysmodules__dict__KeyError)objmodule_names     r    get_module_ns_ofr)   0   sL     #|T2K	;;{+444 I  	I	s   0 >>c                  J    e Zd ZdZd	dZed
d       ZddZddZddZ	ddZ
y)LazyLocalNamespaceao  A lazily evaluated mapping, to be used as the `locals` argument during annotations evaluation.

    While the [`eval`][eval] function expects a mapping as the `locals` argument, it only
    performs `__getitem__` calls. The [`Mapping`][collections.abc.Mapping] abstract base class
    is fully implemented only for type checking purposes.

    Args:
        *namespaces: The namespaces to consider, in ascending order of priority.

    Example:
        ```python {lint="skip" test="skip"}
        ns = LazyLocalNamespace({'a': 1, 'b': 2}, {'a': 3})
        ns['a']
        #> 3
        ns['b']
        #> 2
        ```
    c                    || _         y N)_namespaces)self
namespacess     r    __init__zLazyLocalNamespace.__init__V   s
    %r   c                ~    | j                   D ci c]  }|j                         D ]  \  }}||
  c}}}S c c}}}w r-   )r.   items)r/   nskvs       r    datazLazyLocalNamespace.dataY   s9    #//GGBHHJGDAq1GGGGs   "8c                ,    t        | j                        S r-   )lenr7   r/   s    r    __len__zLazyLocalNamespace.__len__]   s    499~r   c                     | j                   |   S r-   r7   r/   keys     r    __getitem__zLazyLocalNamespace.__getitem__`   s    yy~r   c                    || j                   v S r-   r=   r>   s     r    __contains__zLazyLocalNamespace.__contains__c   s    diir   c                ,    t        | j                        S r-   )iterr7   r:   s    r    __iter__zLazyLocalNamespace.__iter__f   s    DIIr   N)r0   r   returnNone)rF   r   )rF   int)r?   strrF   r	   )r?   objectrF   bool)rF   zIterator[str])r   r   r   r   r1   r   r7   r;   r@   rB   rE   r   r   r    r+   r+   B   s6    && H H r   r+   c                   g }||j                  |       t        | dd      }|||j                  dd      z  }|j                  |D ci c]  }|j                  | c}       t	        |       }t        |t        |       S c c}w )a  Return the global and local namespaces to be used when evaluating annotations for the provided function.

    The global namespace will be the `__dict__` attribute of the module the function was defined in.
    The local namespace will contain the `__type_params__` introduced by PEP 695.

    Args:
        obj: The object to use when building namespaces.
        parent_namespace: Optional namespace to be added with the lowest priority in the local namespace.
            If the passed function is a method, the `parent_namespace` will be the namespace of the class
            the method is defined in. Thus, we also fetch type `__type_params__` from there (i.e. the
            class-scoped type variables).
    __type_params__r   )appendr"   getr   r)   r   r+   )r'   parent_namespacelocals_listtype_paramstglobalnss         r    ns_for_functionrU   j   s     +-K#+, -4C9JB,OK# 	'++,=rBB{;!

A;<  $H8%7%EFF <s   	Bc                  L    e Zd ZdZ	 	 d	 	 	 	 	 ddZedd       Zed	d       Zy)

NsResolvera  A class responsible for the namespaces resolving logic for annotations evaluation.

    This class handles the namespace logic when evaluating annotations mainly for class objects.

    It holds a stack of classes that are being inspected during the core schema building,
    and the `types_namespace` property exposes the globals and locals to be used for
    type annotation evaluation. Additionally -- if no class is present in the stack -- a
    fallback globals and locals can be provided using the `namespaces_tuple` argument
    (this is useful when generating a schema for a simple annotation, e.g. when using
    `TypeAdapter`).

    The namespace creation logic is unfortunately flawed in some cases, for backwards
    compatibility reasons and to better support valid edge cases. See the description
    for the `parent_namespace` argument and the example for more details.

    Args:
        namespaces_tuple: The default globals and locals to use if no class is present
            on the stack. This can be useful when using the `GenerateSchema` class
            with `TypeAdapter`, where the "type" being analyzed is a simple annotation.
        parent_namespace: An optional parent namespace that will be added to the locals
            with the lowest priority. For a given class defined in a function, the locals
            of this function are usually used as the parent namespace:

            ```python {lint="skip" test="skip"}
            from pydantic import BaseModel

            def func() -> None:
                SomeType = int

                class Model(BaseModel):
                    f: 'SomeType'

                # when collecting fields, an namespace resolver instance will be created
                # this way:
                # ns_resolver = NsResolver(parent_namespace={'SomeType': SomeType})
            ```

            For backwards compatibility reasons and to support valid edge cases, this parent
            namespace will be used for *every* type being pushed to the stack. In the future,
            we might want to be smarter by only doing so when the type being pushed is defined
            in the same module as the parent namespace.

    Example:
        ```python {lint="skip" test="skip"}
        ns_resolver = NsResolver(
            parent_namespace={'fallback': 1},
        )

        class Sub:
            m: 'Model'

        class Model:
            some_local = 1
            sub: Sub

        ns_resolver = NsResolver()

        # This is roughly what happens when we build a core schema for `Model`:
        with ns_resolver.push(Model):
            ns_resolver.types_namespace
            #> NamespacesTuple({'Sub': Sub}, {'Model': Model, 'some_local': 1})
            # First thing to notice here, the model being pushed is added to the locals.
            # Because `NsResolver` is being used during the model definition, it is not
            # yet added to the globals. This is useful when resolving self-referencing annotations.

            with ns_resolver.push(Sub):
                ns_resolver.types_namespace
                #> NamespacesTuple({'Sub': Sub}, {'Sub': Sub, 'Model': Model})
                # Second thing to notice: `Sub` is present in both the globals and locals.
                # This is not an issue, just that as described above, the model being pushed
                # is added to the locals, but it happens to be present in the globals as well
                # because it is already defined.
                # Third thing to notice: `Model` is also added in locals. This is a backwards
                # compatibility workaround that allows for `Sub` to be able to resolve `'Model'`
                # correctly (as otherwise models would have to be rebuilt even though this
                # doesn't look necessary).
        ```
    Nc                J    |xs t        i i       | _        || _        g | _        y r-   )r   _base_ns_tuple
_parent_ns_types_stack)r/   namespaces_tuplerP   s      r    r1   zNsResolver.__init__   s'    
 /I/"b2I*=?r   c                f   | j                   s| j                  S | j                   d   }t        |      }g }| j                  |j	                  | j                         t        | j                         dkD  r,| j                   d   }|j	                  |j                  |i       t        |dd      }|r*|j	                  |D ci c]  }|j                  | c}       t        |d      r|j	                  t        |             |j	                  |j                  |i       t        |t        |       S c c}w )zNThe current global and local namespaces to be used for annotations evaluation.   r   rM   r   r%   )r[   rY   r)   rZ   rN   r9   r   r"   hasattrvarsr   r+   )r/   typrT   rQ   
first_typerR   rS   s          r    types_namespacezNsResolver.types_namespace   s       &&&##C(.0 ??&t/t  !A%**1-J
 3 3Z@A
 18=NPR0S {C!

ACD 3
#tCy) 	CLL#./x);[)IJJ  Ds   >D.c             #  f  K   | j                   j                  |       | j                  j                  dd       	 d | j                   j                          | j                  j                  dd       y# | j                   j                          | j                  j                  dd       w xY ww)zPush a type to the stack.rd   N)r[   rN   r%   pop)r/   rb   s     r    pushzNsResolver.push  s      	  %+T2	7!!#MM/6 !!#MM/6s   8B1A6 7B168B..B1)NN)r\   zNamespacesTuple | NonerP   MappingNamespace | NonerF   rG   )rF   r   )rb   ztype[Any] | TypeAliasTyperF   zGenerator[None])	r   r   r   r   r1   r   rd   r   rg   r   r   r    rW   rW      sa    Mb 4848@0@ 2@ 
	@ 0K 0Kd 	7 	7r   rW   )r'   r	   rF   r   r-   )r'   zCallable[..., Any]rP   rh   rF   r   )
__future__r   r#   collections.abcr   r   r   
contextlibr   	functoolsr   typingr	   r
   r   r   typing_extensionsr   r   r   r   r   r   rI   r   r   r   r)   r+   rU   rW   r   r   r    <module>ro      s    " 
 8 8 % % 5 5 O O. ) . &c3h/ ) / ?i >[j [$$%c* %P"GJV7 V7r   