o
    h^                     @  s   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	 er.d dl
mZmZ d dlmZ dddZedddZddddZdS )    )annotationsN)contextmanager)import_module)TYPE_CHECKINGAny)IteratorSequence)PatherrorBaseExceptionobjpathstrreturnc                 C  s"   dt jd|d| jj d|  S )NzWith sys.path = z, accessing z raises z: )syspath	__class____name__)r
   r    r   Z/var/www/html/openai_agents/venv/lib/python3.10/site-packages/griffe/_internal/importer.py_error_details   s   "r   paths
str | PathIterator[None]c                  g  sB    | sdV  dS t j}dd | D t _z	dV  W |t _dS |t _w )zRedefine `sys.path` temporarily.

    Parameters:
        *paths: The paths to use when importing modules.
            If no paths are given, keep `sys.path` untouched.

    Yields:
        Nothing.
    Nc                 S  s   g | ]}t |qS r   )r   ).0r   r   r   r   
<listcomp>$   s    zsys_path.<locals>.<listcomp>)r   r   )r   old_pathr   r   r   sys_path   s   r   import_pathimport_pathsSequence[str | Path] | Noner   c           
      C  s  |  d}g }g }t|pd t |rDd|}zt|}W n# ty@ } z|t|| |d|d W Y d}~nd}~ww n	|st	d||}|D ]+}	zt
||	}W qO tyz } z|t||d d|  t	d|d}~ww W d   |S 1 sw   Y  |S )a  Dynamically import the specified object.

    It can be a module, class, method, function, attribute, type alias,
    nested arbitrarily.

    It works like this:

    - for a given object path `a.b.x.y`
    - it tries to import `a.b.x.y` as a module (with `importlib.import_module`)
    - if it fails, it tries again with `a.b.x`, storing `y`
    - then `a.b`, storing `x.y`
    - then `a`, storing `b.x.y`
    - if nothing worked, it raises an error
    - if one of the iteration worked, it moves on, and...
    - it tries to get the remaining (stored) parts with `getattr`
    - for example it gets `b` from `a`, then `x` from `b`, etc.
    - if a single attribute access fails, it raises an error
    - if everything worked, it returns the last obtained attribute

    Since the function potentially tries multiple things before succeeding,
    all errors happening along the way are recorded, and re-emitted with
    an `ImportError` when it fails, to let users know what was tried.

    IMPORTANT: The paths given through the `import_paths` parameter are used
    to temporarily patch `sys.path`: this function is therefore not thread-safe.

    IMPORTANT: The paths given as `import_paths` must be *correct*.
    The contents of `sys.path` must be consistent to what a user of the imported code
    would expect. Given a set of paths, if the import fails for a user, it will fail here too,
    with potentially unintuitive errors. If we wanted to make this function more robust,
    we could add a loop to "roll the window" of given paths, shifting them to the left
    (for example: `("/a/a", "/a/b", "/a/c/")`, then `("/a/b", "/a/c", "/a/a/")`,
    then `("/a/c", "/a/a", "/a/b/")`), to make sure each entry is given highest priority
    at least once, maintaining relative order, but we deem this unnecessary for now.

    Parameters:
        import_path: The path of the object to import.
        import_paths: The (sys) paths to import the object from.

    Raises:
        ModuleNotFoundError: When the object's module could not be found.
        ImportError: When there was an import error or when couldn't get the attribute.

    Returns:
        The imported object.
    .r   r   Nz; :)splitr   joinr   r   appendr   insertpopImportErrorgetattr)
r   r   module_partsobject_partserrorsmodule_pathmoduler
   valuepartr   r   r   dynamic_import+   s<   
/

r1   )r
   r   r   r   r   r   )r   r   r   r   )N)r   r   r   r   r   r   )
__future__r   r   
contextlibr   	importlibr   typingr   r   collections.abcr   r   pathlibr	   r   r   r1   r   r   r   r   <module>   s   
