o
    ii                  
   @  sN  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Zd dlmZ	 d dlm
Z
mZmZmZ d dl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 d d
lmZ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' d dl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.mZm/Z/m0Z0m1Z1m2Z2 erd dl3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9 d dlm:Z: de;d< zd dl<Z<W n e=y   efZ>Y nw ee<fZ>e
j?Z@ddddddddddDd*d+ZG d,d- d-ZAe	jBejCe	jDejEe	jFejGe	jHejIe	jJejKiZLdd.dEd5d6ZMdd.dFd:d;ZNd<d= ejOejPejQd>R D ZSdd.dGd@dAZTdd.dFdBdCZUdS )H    )annotationsN)	Parameter)	Signaturecleandocgetsourcelinesunwrap)	signature)TYPE_CHECKINGAny)
ObjectNode)LinesCollectionModulesCollection)KindParameterKindTypeParameterKind)Expr	ExprBinOpExprSubscript	ExprTuplesafe_get_annotation)
Extensionsload_extensions)dynamic_import)logger)Alias	AttributeClass	DocstringFunctionModuler   
Parameters	TypeAliasTypeParameterTypeParameters)Sequence)Path)DocstringOptionsDocstringStyle)Parserztuple[types.ModuleType, ...]_TYPING_MODULES)filepathimport_paths
extensionsparentdocstring_parserdocstring_optionslines_collectionmodules_collectionmodule_namestrr*   Path | Noner+   Sequence[str | Path] | Noner,   Extensions | Noner-   Module | Noner.   DocstringStyle | Parser | Noner/   DocstringOptions | Noner0   LinesCollection | Noner1   ModulesCollection | Nonereturnr   c          	   
   C  s$   t | ||pt |||||d|S )aU  Inspect a module.

    Sometimes we cannot get the source code of a module or an object,
    typically built-in modules like `itertools`.
    The only way to know what they are made of is to actually import them and inspect their contents.

    Sometimes, even if the source code is available,
    loading the object is desired because it was created or modified dynamically,
    and our static agent is not powerful enough to infer all these dynamic modifications.
    In this case, we load the module using introspection.

    Griffe therefore provides this function for dynamic analysis.
    It uses a [`NodeVisitor`][ast.NodeVisitor]-like class, the [`Inspector`][griffe.Inspector],
    to inspect the module with [`inspect.getmembers()`][inspect.getmembers].

    The inspection agent works similarly to the regular [`Visitor`][griffe.Visitor] agent,
    in that it maintains a state with the current object being handled, and recursively handle its members.

    Important:
        This function is generally not used directly.
        In most cases, users can rely on the [`GriffeLoader`][griffe.GriffeLoader]
        and its accompanying [`load`][griffe.load] shortcut and their respective options
        to load modules using dynamic analysis.

    Parameters:
        module_name: The module name (as when importing [from] it).
        filepath: The module file path.
        import_paths: Paths to import the module from.
        extensions: The extensions to use when inspecting the module.
        parent: The optional parent of this module.
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.

    Returns:
        The module, with its members populated.
    )r.   r/   r0   r1   )	Inspectorr   
get_module)	r2   r*   r+   r,   r-   r.   r/   r0   r1    r?   g/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/griffe/_internal/agents/inspector.pyinspect7   s   2	rA   c                      s  e Zd ZdZ					dNdO fddZdPddZdQddZdRdSd"d#ZdTd$d%ZdTd&d'Z	dTd(d)Z
dTd*d+ZdTd,d-ZdTd.d/ZdTd0d1ZdTd2d3ZdTd4d5ZdTd6d7ZdTd8d9ZdTd:d;ZdTd<d=ZdTd>d?ZdTd@dAZdRdUdDdEZdTdFdGZdTdHdIZdRdVdLdMZ  ZS )Wr=   zzThis class is used to instantiate an inspector.

    Inspectors iterate on objects members to extract data from them.
    Nr2   r3   r*   r4   r,   r   r-   r7   r.   r8   r/   r9   r0   r:   r1   r;   r<   Nonec	           	        sd   t    || _	 || _	 || _	 || _	 d| _	 || _	 |p i | _	 |p't	 | _
	 |p.t | _dS )a  Initialize the inspector.

        Parameters:
            module_name: The module name.
            filepath: The optional filepath.
            extensions: Extensions to use when inspecting.
            parent: The module parent.
            docstring_parser: The docstring parser to use.
            docstring_options: Docstring parsing options.
            lines_collection: A collection of source code lines.
            modules_collection: A collection of modules.
        N)super__init__r2   r*   r,   r-   currentr.   r/   r   r0   r   r1   )	selfr2   r*   r,   r-   r.   r/   r0   r1   	__class__r?   r@   rD   {   s&   

zInspector.__init__noder   Docstring | Nonec                 C  sh   z	t |jdd }W n
 ty   Y d S w |d u rd S zt|}W n
 ty*   Y d S w t|| j| jdS )N__doc__)parserparser_options)getattrobj	Exceptionr   AttributeErrorr   r.   r/   )rF   rI   valuecleanedr?   r?   r@   _get_docstring   s"   zInspector._get_docstring#tuple[int, int] | tuple[None, None]c              	   C  s^   | j r	| j | jvrdS z	t|j\}}W n ttfy    Y dS w ||d| d fS )N)NN 
)	r*   r0   r   rO   OSError	TypeErrorjoinrstripcount)rF   rI   lineslinenor?   r?   r@   _get_linenos   s   zInspector._get_linenosr+   r5   r   c           	      C  s   | j }| jdur| jj d| }t|pd}| jr>| jj}t|dD ]}|j}q%| jjdkr4|j}||vr>|d| t	||}d}| jdur[| jj
dD ]	}td||d}qQt|| j |d}| | | jjS )a4  Build and return the object representing the module attached to this inspector.

        This method triggers a complete inspection of the module members.

        Parameters:
            import_paths: Paths replacing `sys.path` to import the module.

        Returns:
            A module instance.
        N.r?   rD   r   )namer-   )r-   )r2   r-   pathlistr*   ranger\   steminsertr   splitr   rA   rE   module)	rF   r+   import_pathparent_path_rR   parent_nodepartmodule_noder?   r?   r@   r>      s(   



zInspector.get_modulec                 C  s   t | d|j | j| dS )zpExtend the base inspection with extensions.

        Parameters:
            node: The node to inspect.
        inspect_N)rN   kindgeneric_inspectrF   rI   r?   r?   r@   rA      s   zInspector.inspectc                 C  s   |j D ]f}|j }rd|jrI|| jj d|j krIt|jdsHt	d| t
|jd| jj| j| j| j| j| jd}|| | j|j|jj qt|j|dd}| j|j| | jjd||| d	 q| | qdS )
zxExtend the base generic inspection with extensions.

        Parameters:
            node: The node to inspect.
        r`   __file__z;Module %s is not discoverable on disk, inspecting right nowN)r*   r-   r,   r.   r/   r0   r1   dynamic)analysison_alias_instance)aliasrI   agent)childrenalias_target_path	is_modulerE   rb   ra   hasattrrO   r   debugr=   rh   r,   r.   r/   r0   r1   inspect_module
set_memberr   callrA   )rF   rI   childtarget_path	inspectorrw   r?   r?   r@   rq     s.   



zInspector.generic_inspectc              	   C  s   | j jd|| d | j jd|| d t| j| j| j| || j| jdd | _	}| j jd||| d | j jd||| d	 | 
| | j jd
||| d | j jd||| d	 dS )zVInspect a module.

        Parameters:
            node: The node to inspect.
        on_noderI   rx   on_module_nodert   )ra   r*   r-   	docstringr0   r1   ru   on_instancerI   rO   rx   on_module_instance)rI   modrx   
on_memberson_module_membersN)r,   r   r   r2   r*   r-   rT   r0   r1   rE   rq   )rF   rI   rh   r?   r?   r@   r~   ,  s    	
zInspector.inspect_modulec              
   C  s  | j jd|| d | j jd|| d g }|jjD ]}|tu rq||j d|j  q| |\}}t	|j
| ||tt|j| j|j
d ||dd}| j|j
| || _| j jd||| d	 | j jd
||| d | | | j jd||| d	 | j jd||| d | jj| _dS )zUInspect a class.

        Parameters:
            node: The node to inspect.
        r   r   on_class_noder`   r-   memberrt   )ra   r   basestype_parametersr^   	endlinenoru   r   r   on_class_instance)rI   clsrx   r   on_class_membersN)r,   r   rO   	__bases__objectappend
__module____qualname__r_   r   ra   rT   r#   _convert_type_parametersrE   r   rq   r-   )rF   rI   r   baser^   r   class_r?   r?   r@   inspect_classC  s2   	
zInspector.inspect_classc                 C     |  |dh dS )z]Inspect a static method.

        Parameters:
            node: The node to inspect.
        staticmethodNhandle_functionrr   r?   r?   r@   inspect_staticmethode     zInspector.inspect_staticmethodc                 C  r   )z\Inspect a class method.

        Parameters:
            node: The node to inspect.
        classmethodNr   rr   r?   r?   r@   inspect_classmethodm  r   zInspector.inspect_classmethodc                 C  r   )zaInspect a method descriptor.

        Parameters:
            node: The node to inspect.
        zmethod descriptorNr   rr   r?   r?   r@   inspect_method_descriptoru  r   z#Inspector.inspect_method_descriptorc                 C  r   )z^Inspect a builtin method.

        Parameters:
            node: The node to inspect.
        builtinNr   rr   r?   r?   r@   inspect_builtin_method}  r   z Inspector.inspect_builtin_methodc                 C     |  | dS )zVInspect a method.

        Parameters:
            node: The node to inspect.
        Nr   rr   r?   r?   r@   inspect_method     zInspector.inspect_methodc                 C  r   )zYInspect a coroutine.

        Parameters:
            node: The node to inspect.
        asyncNr   rr   r?   r?   r@   inspect_coroutine  r   zInspector.inspect_coroutinec                 C  r   )z`Inspect a builtin function.

        Parameters:
            node: The node to inspect.
        r   Nr   rr   r?   r?   r@   inspect_builtin_function  r   z"Inspector.inspect_builtin_functionc                 C  r   )zXInspect a function.

        Parameters:
            node: The node to inspect.
        Nr   rr   r?   r?   r@   inspect_function  r   zInspector.inspect_functionc                 C  s   |  |ddh dS )z_Inspect a cached property.

        Parameters:
            node: The node to inspect.
        cachedpropertyNr   rr   r?   r?   r@   inspect_cached_property  s   z!Inspector.inspect_cached_propertyc                 C  r   )zXInspect a property.

        Parameters:
            node: The node to inspect.
        r   Nr   rr   r?   r?   r@   inspect_property  r   zInspector.inspect_propertyc                 C  r   )zbInspect a get/set descriptor.

        Parameters:
            node: The node to inspect.
        r   Nr   rr   r?   r?   r@   inspect_getset_descriptor  r   z#Inspector.inspect_getset_descriptorlabels
set | Nonec           
   
     sl  j jd d j jd d zt j}W n ty&   d}d}Y n"w t fdd|j D  }|j}|t	u r?dnt
|j jd} \}}|pSt }d|v rht jd| ||d	d
}	nt j||tt jj jd  ||d	d}	|	 j|O  _j j|	 j jd |	d |	jrj jd |	d dS j jd |	d dS )zHandle a function.

        Parameters:
            node: The node to inspect.
            labels: Labels to add to the data object.
        r   r   on_function_nodeNc                   s   g | ]}t |j jd qS r   )_convert_parameterrE   ra   ).0	parameterrI   rF   r?   r@   
<listcomp>  s    z-Inspector.handle_function.<locals>.<listcomp>r   r   rt   )ra   rR   
annotationr   r^   r   ru   )ra   
parametersreturnsr   r   r^   r   ru   r   r   on_attribute_instancerI   attrrx   on_function_instance)rI   funcrx   )r,   r   getsignaturerO   rP   r    r   valuesreturn_annotation_empty_convert_object_to_annotationrE   ra   r_   setr   rT   r   r#   r   r   r   is_attribute)
rF   rI   r   r   r   r   r   r^   r   rO   r?   r   r@   r     s`   

zInspector.handle_functionc                 C  s   | j jd|| d | j jd|| d | |\}}t|jt|jj| j|jd||t	t
|j| j|jd | || jdd}| j|j| | j jd||| d | j jd	||| d
 dS )zZInspect a type alias.

        Parameters:
            node: The node to inspect.
        r   r   on_type_alias_noder   rt   )ra   rR   r^   r   r   r   r-   ru   r   r   on_type_alias_instance)rI   
type_aliasrx   N)r,   r   r_   r!   ra   _convert_type_to_annotationrO   	__value__rE   r#   r   rT   r   )rF   rI   r^   r   r   r?   r?   r@   inspect_type_alias  s    
zInspector.inspect_type_aliasc                 C  r   )zZInspect an attribute.

        Parameters:
            node: The node to inspect.
        N)handle_attributerr   r?   r?   r@   inspect_attribute  r   zInspector.inspect_attributer   str | Expr | Nonec                 C  sH  | j jd|| d | j jd|| d | j}t }|jtju r$|d n!|jtju r0|d n|jtj	u rE|j
dkr=dS |j}|d zt|j}W n tyW   d}Y nw z| |}W n tyj   d}Y nw t|j
|||d	d
}| j|O  _||j
| |j
dkrt|j|_| j jd||| d | j jd||| d dS )zHandle an attribute.

        Parameters:
            node: The node to inspect.
            annotation: A potential annotation.
        r   r   on_attribute_nodezmodule-attributezclass-attributerD   Nzinstance-attributert   )ra   rR   r   r   ru   __all__r   r   r   r   )r,   r   rE   r   rp   r   MODULEaddCLASSFUNCTIONra   r-   reprrO   rP   rT   r   r   r   rc   exports)rF   rI   r   r-   r   rR   r   	attributer?   r?   r@   r      sH   


zInspector.handle_attribute)NNNNN)r2   r3   r*   r4   r,   r   r-   r7   r.   r8   r/   r9   r0   r:   r1   r;   r<   rB   )rI   r   r<   rJ   )rI   r   r<   rU   )N)r+   r5   r<   r   )rI   r   r<   rB   )rI   r   r   r   r<   rB   )rI   r   r   r   r<   rB   )__name__r   r   rK   rD   rT   r_   r>   rA   rq   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r?   r?   rG   r@   r=   u   s<    

4


0

%

"










B
r=   r   r   SignatureParameterModule | Classr   
str | Noner   c                C  sn   | j }| jtu r
d nt| j||d}t| j }| jtu rd }nt| jdr*| jj}nt	| j}t
||||dS )Nr   r   )r   rp   default)ra   r   r   r   _parameter_kind_maprp   r   r|   r   r   r   )r   r-   r   ra   r   rp   r   r?   r?   r@   r   Z  s   




r   rO   r
   r   c                C  sz   t | tr| }nt| }t| drd|v r| jn|}n|}zt|ddtjdd}W n ty4   |  Y S w t	|j
||dS )Nr   <evalz<>   )modefilenameflagsoptimizer   )
isinstancer3   r   r|   r   compileastPyCF_ONLY_ASTSyntaxErrorr   body)rO   r-   r   r   obj_reprannotation_noder?   r?   r@   r   q  s   

r   c                 C  s0   i | ]\}}t D ]}t||rt|||qqS r?   )r)   r|   rN   )r   r   rR   rh   r?   r?   r@   
<dictcomp>  s    
r   )TypeVarTypeVarTuple	ParamSpeclist[TypeParameter]c             
     s   t | } t| dsg S g }| jD ]H}t|dd }|d ur#t| d} fddt|ddD }t|dd	d
  rCt|j d}nd }|t|jt	t
| ||pSd |d q|S )N__type_params__	__bound__r   c                      g | ]	}t | d qS r   r   )r   
constraintr   r-   r?   r@   r     s    z,_convert_type_parameters.<locals>.<listcomp>__constraints__r?   has_defaultc                   S  s   dS )NFr?   r?   r?   r?   r@   <lambda>  s    z*_convert_type_parameters.<locals>.<lambda>)rp   boundconstraintsr   )r   r|   r   rN   r   __default__r   r"   r   _type_parameter_kind_maptype)rO   r-   r   r   type_parameterr  r  r   r?   r  r@   r     s8   




r   c                  s   t | }|d u rt|  dS  fddt | D }|tju r*tdd |S t| d}|d u r7d S t	|t
|ddS )Nr   c                   r   r   r   )r   argr  r?   r@   r     s    z/_convert_type_to_annotation.<locals>.<listcomp>c                 S  s   t | d|S )N|)r   )leftrightr?   r?   r@   r    s    z-_convert_type_to_annotation.<locals>.<lambda>T)implicit)typing
get_originr   get_argstypes	UnionType	functoolsreducer   r   r   )rO   r-   r   originargsr?   r  r@   r     s   

r   )r2   r3   r*   r4   r+   r5   r,   r6   r-   r7   r.   r8   r/   r9   r0   r:   r1   r;   r<   r   )r   r   r-   r   r   r   r<   r   )rO   r
   r-   r   r   r   r<   r   )rO   r
   r-   r   r   r   r<   r   )V
__future__r   r   r  sysr  r  rA   r   r   r   r   r   r   r   r   r	   r
   %griffe._internal.agents.nodes.runtimer   griffe._internal.collectionsr   r   griffe._internal.enumerationsr   r   r   griffe._internal.expressionsr   r   r   r   r    griffe._internal.extensions.baser   r   griffe._internal.importerr   griffe._internal.loggerr   griffe._internal.modelsr   r   r   r   r   r   r    r!   r"   r#   collections.abcr$   pathlibr%   #griffe._internal.docstrings.parsersr&   r'   r(   __annotations__typing_extensionsImportErrorr)   emptyr   r=   POSITIONAL_ONLYpositional_onlyPOSITIONAL_OR_KEYWORDpositional_or_keywordVAR_POSITIONALvar_positionalKEYWORD_ONLYkeyword_onlyVAR_KEYWORDvar_keywordr   r   r   type_vartype_var_tuple
param_specitemsr	  r   r   r?   r?   r?   r@   <module>   s~   4
>   `+