
    gT              
      n   d dl mZ d dlZd dlmZ d dl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 d d
l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& erd dl'm(Z( d dl)m*Z* d dlm+Z+ d dlm,Z, ejZ                  Z.ddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ G d d      Z/ej`                  ejb                  ejd                  ejf                  ejh                  ejj                  ejl                  ejn                  ejp                  ejr                  iZ:ddZ;ddZ<y)    )annotationsN)	Parameter)	Signaturecleandocgetsourcelines)	signature)TYPE_CHECKINGAny)
ObjectNode)LinesCollectionModulesCollection)KindParameterKind)safe_get_annotation)
Extensionsload_extensions)dynamic_import)logger)Alias	AttributeClass	DocstringFunctionModuler   
Parameters)Sequence)Path)Parser)Expr)filepathimport_paths
extensionsparentdocstring_parserdocstring_optionslines_collectionmodules_collectionc          
     ^    t        | ||xs
 t               |||||      j                  |      S )a`  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: Additional 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%   r&   r'   )	Inspectorr   
get_module)	module_namer    r!   r"   r#   r$   r%   r&   r'   s	            M/var/www/openai/venv/lib/python3.12/site-packages/_griffe/agents/inspector.pyinspectr-       s>    d 'o')+)-	 j	    c                       e Zd ZdZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZddZddZdddZddZddZ	ddZ
dd	Zdd
ZddZddZddZddZddZddZddZddZddZdddZddZdddZ xZS ) r)   zzThis class is used to instantiate an inspector.

    Inspectors iterate on objects members to extract data from them.
    c	                    t         	|           || _        	 || _        	 || _        	 || _        	 d| _        	 || _        	 |xs i | _        	 |xs
 t               | _
        	 |xs
 t               | _        y)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: The docstring parsing options.
            lines_collection: A collection of source code lines.
            modules_collection: A collection of modules.
        N)super__init__r+   r    r"   r#   currentr$   r%   r   r&   r   r'   )
selfr+   r    r"   r#   r$   r%   r&   r'   	__class__s
            r,   r2   zInspector.__init__d   s    . 	 +%-#&04%+='+1/?*1B1Hb,1A1V_EV05G5^K\K^&r.   c                    	 t        |j                  dd       }|y 	 t        |      }t        || j                  | j                        S # t        $ r Y y w xY w# t        $ r Y y w xY w)N__doc__)parserparser_options)getattrobj	Exceptionr   AttributeErrorr   r$   r%   )r4   nodevaluecleaneds       r,   _get_docstringzInspector._get_docstring   s~    	DHHi6E =	 uoG ((11
 	
  		  		s"   A
 A 
	AA	A%$A%c                   | j                   r| j                   | j                  vry	 t        |j                        \  }}||dj                  |      j                         j                  d      z   fS # t        t
        f$ r Y yw xY w)N)NN 
)	r    r&   r   r;   OSError	TypeErrorjoinrstripcount)r4   r>   lineslinenos       r,   _get_linenoszInspector._get_linenos   s{    }}T5J5J J	*4884ME6 v 5 5 7 = =d CCCC # 		s   A2 2BBc                   | j                   }| j                  | j                  j                   d| }t        |xs d      }| j                  r|| j                  j                  }t        |j                  d            D ]  }|j                  } | j                  j                  dk(  r|j                  }||vr|j                  d|       t        ||      }d}| j                  8| j                  j                  j                  d      D ]  }t        d||      } t        || j                   |      }| j                  |       | 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. r2   r   )namer#   r#   )r+   r#   pathlistr    rangerI   steminsertr   splitr   r-   r3   module)	r4   r!   import_pathparent_path_r?   parent_nodepartmodule_nodes	            r,   r*   zInspector.get_module   s3    &&;;"![[--.a}=K L.B/ ==--..K;,,S12)00 3 }}!!Z/)00,.##A{3{L9
 ;;"((..s3(DM 4 (8(8M[!||"""r.   c                X     t        | d|j                   | j                        |       y)zpExtend the base inspection with extensions.

        Parameters:
            node: The node to inspect.
        inspect_N)r:   kindgeneric_inspectr4   r>   s     r,   r-   zInspector.inspect   s)     	D,d.B.BCDIr.   c                H   |j                   D ]  }|j                  x}ro|j                  r|| j                  j                   d|j
                   k(  rt        |j                  d      r_t        j                  d|       t        |j
                  d| j                  j                  | j                  | j                  | j                  | j                  | j                         }|j#                  |       | j                  j%                  |j
                  |j                  j                         %t'        |j
                  |      }| j                  j%                  |j
                  |       | j                  j)                  d|||        | j+                  |        y)zxExtend the base generic inspection with extensions.

        Parameters:
            node: The node to inspect.
        rN   __file__z;Module %s is not discoverable on disk, inspecting right nowN)r    r#   r"   r$   r%   r&   r'   on_alias)aliasr>   agent)childrenalias_target_path	is_moduler3   rR   rP   hasattrr;   r   debugr)   rX   r"   r$   r%   r&   r'   inspect_module
set_memberr   callr-   )r4   r>   childtarget_path	inspectorrg   s         r,   rb   zInspector.generic_inspect   s=    ]]E#555{5 ??{9J9J8K1UZZL6Y'Y"599j9%bdop$-!JJ%)#'<<#6#6'+-1-B-B.2.D.D-1-B-B/3/F/F	%	 "007//

I<M<M<T<TU "%**k:ELL++EJJ>OO((5tSW(XU#; #r.   c                H   | j                   j                  d||        | j                   j                  d||        t        | j                  | j                  | j
                  | j                  |      | j                  | j                        x| _	        }| j                   j                  d|||        | j                   j                  d|||        | j                  |       | j                   j                  d	|||        | j                   j                  d
|||        y)zVInspect a module.

        Parameters:
            node: The node to inspect.
        on_noder>   rh   on_module_node)rP   r    r#   	docstringr&   r'   on_instancer>   r;   rh   on_module_instance)r>   modrh   
on_memberson_module_membersN)r"   rp   r   r+   r    r#   rA   r&   r'   r3   rb   )r4   r>   rX   s      r,   rn   zInspector.inspect_module  s     	YT>-DE &!!]];;))$/!22#66!
 	
v 	]6N1&PTUT"\&M0ttTr.   c                @   | j                   j                  d||        | j                   j                  d||        g }|j                  j                  D ]5  }|t        u r|j                  |j                   d|j                          7 | j                  |      \  }}t        |j                  | j                  |      |||      }| j                  j                  |j                  |       || _        | j                   j                  d|||        | j                   j                  d||| 	       | j                  |       | j                   j                  d
|||        | j                   j                  d||| 	       | j                  j                  | _        y)zUInspect a class.

        Parameters:
            node: The node to inspect.
        ru   rv   on_class_noderN   )rP   rx   basesrK   	endlinenory   rz   on_class_instance)r>   clsrh   r}   on_class_membersN)r"   rp   r;   	__bases__objectappend
__module____qualname__rL   r   rP   rA   r3   ro   rb   r#   )r4   r>   r   baserK   r   class_s          r,   inspect_classzInspector.inspect_class+  si    	YT>_4tDHH&&Dv~LLDOO,Ad.?.?-@AB '
 !--d3	))$/
 			62]6N0ttTT"\&M/ddS||**r.   c                *    | j                  |dh       y)z]Inspect a static method.

        Parameters:
            node: The node to inspect.
        staticmethodNhandle_functionrc   s     r,   inspect_staticmethodzInspector.inspect_staticmethodK  s     	TN#34r.   c                *    | j                  |dh       y)z\Inspect a class method.

        Parameters:
            node: The node to inspect.
        classmethodNr   rc   s     r,   inspect_classmethodzInspector.inspect_classmethodS  s     	TM?3r.   c                *    | j                  |dh       y)zaInspect a method descriptor.

        Parameters:
            node: The node to inspect.
        zmethod descriptorNr   rc   s     r,   inspect_method_descriptorz#Inspector.inspect_method_descriptor[  s     	T$7#89r.   c                *    | j                  |dh       y)z^Inspect a builtin method.

        Parameters:
            node: The node to inspect.
        builtinNr   rc   s     r,   inspect_builtin_methodz Inspector.inspect_builtin_methodc       	TI;/r.   c                &    | j                  |       y)zVInspect a method.

        Parameters:
            node: The node to inspect.
        Nr   rc   s     r,   inspect_methodzInspector.inspect_methodk       	T"r.   c                *    | j                  |dh       y)zYInspect a coroutine.

        Parameters:
            node: The node to inspect.
        asyncNr   rc   s     r,   inspect_coroutinezInspector.inspect_coroutines  s     	TG9-r.   c                *    | j                  |dh       y)z`Inspect a builtin function.

        Parameters:
            node: The node to inspect.
        r   Nr   rc   s     r,   inspect_builtin_functionz"Inspector.inspect_builtin_function{  r   r.   c                &    | j                  |       y)zXInspect a function.

        Parameters:
            node: The node to inspect.
        Nr   rc   s     r,   inspect_functionzInspector.inspect_function  r   r.   c                ,    | j                  |ddh       y)z_Inspect a cached property.

        Parameters:
            node: The node to inspect.
        cachedpropertyNr   rc   s     r,   inspect_cached_propertyz!Inspector.inspect_cached_property  s     	THj#9:r.   c                *    | j                  |dh       y)zXInspect a property.

        Parameters:
            node: The node to inspect.
        r   Nr   rc   s     r,   inspect_propertyzInspector.inspect_property  s     	TJ<0r.   c           	        | j                   j                  d||        | j                   j                  d||        	 t        |j                        }t	        |j
                  j                         D cg c]  }t        || j                         c} }|j                  }|t        u rdnt        || j                        }| j                  |      \  }}	|xs
 t               }d|v r+t        |j                   d|| j#                  |      ||	      }
n*t%        |j                   ||| j#                  |      ||	      }
|
xj&                  |z  c_        | j                  j)                  |j                   |
       | j                   j                  d	||
| 
       |
j*                  r | j                   j                  d||
|        y| j                   j                  d||
|        yc c}w # t        $ r d}d}Y 6w xY w)zHandle a function.

        Parameters:
            node: The node to inspect.
            labels: Labels to add to the data object.
        ru   rv   on_function_noderQ   Nr   )rP   r?   
annotationrx   rK   r   )rP   
parametersreturnsrx   rK   r   ry   rz   on_attribute_instancer>   attrrh   on_function_instance)r>   funcrh   )r"   rp   getsignaturer;   r   r   values_convert_parameterr3   return_annotation_empty_convert_object_to_annotationr<   rL   setr   rP   rA   r   labelsro   is_attribute)r4   r>   r   r   	parameterr   r   r   rK   r   r;   s              r,   r   zInspector.handle_function  s    	YT>/d$G	$TXX.I $V_VjVjVqVqVstVs$Yt||DVstJ !* ; ; %. 23DT\\Z  !--d3	 35YY"--d3#C YY%--d3#C 	

f
		3/]3dKOO  !8t#UY ZOO  !7dTX YK u  	 JG		s   G) 4G$)G:9G:c                &    | j                  |       y)zZInspect an attribute.

        Parameters:
            node: The node to inspect.
        N)handle_attributerc   s     r,   inspect_attributezInspector.inspect_attribute  s     	d#r.   c                   | j                   j                  d||        | j                   j                  d||        | j                  }t               }|j                  t
        j                  u r|j                  d       nw|j                  t
        j                  u r|j                  d       nI|j                  t
        j                  u r-|j                  dk7  ry|j                  }|j                  d       	 t        |j                        }	 | j                  |      }t!        |j                  |||	      }|xj"                  |z  c_        |j%                  |j                  |       |j                  d
k(  rt        |j                        |_        | j                   j                  d|||        | j                   j                  d|||        y# t        $ r d}Y w xY w# t        $ r d}Y w xY w)zHandle an attribute.

        Parameters:
            node: The node to inspect.
            annotation: A potential annotation.
        ru   rv   on_attribute_noderX   classr2   Ninstance)rP   r?   r   rx   __all__ry   rz   r   r   )r"   rp   r3   r   ra   r   MODULEaddCLASSFUNCTIONrP   r#   reprr;   r<   rA   r   r   ro   exports)r4   r>   r   r#   r   r?   rx   	attributes           r,   r   zInspector.handle_attribute  s    	YT>0t4H 5;;$++%JJx [[DJJ&JJw[[DMM){{j(]]FJJz"	NE	++D1I !	
	 	F"$))Y/99	! ]FN]9DQ44iW[\'  	E	  	I	s$   9G G# G G #G10G1)NNNNN)r+   strr    Path | Noner"   r   r#   Module | Noner$   Parser | Noner%   dict[str, Any] | Noner&   LinesCollection | Noner'   ModulesCollection | NonereturnNone)r>   r   r   zDocstring | None)r>   r   r   z#tuple[int, int] | tuple[None, None])N)r!   Sequence[str | Path] | Noner   r   )r>   r   r   r   )r>   r   r   z
set | Noner   r   )r>   r   r   str | Expr | Noner   r   )__name__r   r   r7   r2   rA   rL   r*   r-   rb   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r5   s   @r,   r)   r)   ^   s     !%*.37377;2'2' 2' 	2'
 2' (2' 12' 12' 52' 
2'h
,D.#`J#$JU,+@54:0#.0#;18Zt$-] -]r.   r)   c                b   | j                   }| j                  t        u rd nt        | j                  |      }t        | j
                     }| j                  t        u rd }nBt        | j                  d      r| j                  j                  }nt        | j                        }t        ||||      S )NrQ   r   )r   ra   default)rP   r   r   r   	_kind_mapra   r   rl   r   r   r   )r   r#   rP   r   ra   r   s         r,   r   r     s    >>D$$.4QR[RfRfou4v  Y^^$DF"	""J	/##,,y(()TjtWMMr.   c                    t        | t              s$t        | d      r| j                  } nt	        |       } 	 t        | ddt        j                  d      }t        |j                  |      S # t        $ r | cY S w xY w)Nr   evalz<>   )modefilenameflagsoptimizerQ   )
isinstancer   rl   r   r   compileastPyCF_ONLY_ASTSyntaxErrorr   body)r;   r#   annotation_nodes      r,   r   r   &  su     c33
#,,C s)C!#FTIZIZefg 33FCC  
s   A+ +A98A9)r+   r   r    r   r!   r   r"   zExtensions | Noner#   r   r$   r   r%   r   r&   r   r'   r   r   r   )r   SignatureParameterr#   Module | Classr   r   )r;   r
   r#   r   r   r   )=
__future__r   r   r-   r   r   r   r   r   r   r   typingr	   r
   _griffe.agents.nodes.runtimer   _griffe.collectionsr   r   _griffe.enumerationsr   r   _griffe.expressionsr   _griffe.extensions.baser   r   _griffe.importerr   _griffe.loggerr   _griffe.modelsr   r   r   r   r   r   r   collections.abcr   pathlibr   r   r   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   rO   r.   r,   <module>r     sO   # 
 3 7 7 - % 3 B 4 3 ? + ! f f f(+( 
 !04$( &*/3/337;; ; .	;
 "; ; $; -; -; 1; ;|l] l]` &&(E(E,,m.Q.Q%%}'C'C##]%?%?""M$=$=	N Dr.   