o
    bJZ                     @   s  d 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ZddlmZ	 ddlm
Z
 ddlmZ dZdd	 ZG d
d dZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZi d&ed'ed(ed)ed*ed+ed,ed-ed.ed/ed0ed1ed2ed3ed4ed5ed6ed7eiZG d8d9 d9eZdS ):a  
Data object model, as per https://docs.python.org/3/reference/datamodel.html.

This module describes, at least partially, a data object model for some
of astroid's nodes. The model contains special attributes that nodes such
as functions, classes, modules etc have, such as __doc__, __class__,
__module__ etc, being used when doing attribute lookups over nodes.

For instance, inferring `obj.__class__` will first trigger an inference
of the `obj` variable. If it was successfully inferred, then an attribute
`__class__ will be looked for in the inferred object. This is the part
where the data model occurs. The model is attached to those nodes
and the lookup mechanism will try to see if attributes such as
`__class__` are defined by the model or not. If they are defined,
the model will be requested to return the corresponding value of that
attribute. Thus the model can be viewed as a special part of the lookup
mechanism.
    N)	lru_cachecontext)
exceptions)node_classesZattr_c                    sP   t j| d  fddt| D }dd | D } tt||  S )Nparentc                    s   g | ]	}t j| d qS )valuer   r   Const).0r
   obj ^/var/www/secure340b-portal/env/lib/python3.10/site-packages/astroid/interpreter/objectmodel.py
<listcomp>/   s    z _dunder_dict.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r   )r   elemr   r   r   r   6       )r   Dictlistkeysvaluespostinitzip)instance
attributesr   r   r   r   r   _dunder_dict+   s   

r   c                   @   sP   e Zd Zdd Zdd Zdd Zddd	Zd
d Zedddd Z	dd Z
dS )ObjectModelc                 C   s
   d | _ d S N	_instanceselfr   r   r   __init__=   s   
zObjectModel.__init__c           
      C   s   g }t | j}d}t|d }t|  D ]1}dt| | }tj|d|dd}|d g}|dd  D ]}	|d| |	  q5|| q||d	d|  	|d
 S )Nz%(cname)s(%(fields)s)   P      )indentwidthTr    z,
)cnamefields)
type__name__lensortedr   pprintpformat
splitlinesappendjoin)
r$   resultr,   string	alignmentfieldr*   linesinnerliner   r   r   __repr__@   s   

zObjectModel.__repr__c                 C   s
   || _ | S r    r!   )r$   r   r   r   r   __call__S   s   zObjectModel.__call__Nc                 C   s   | |S r    r   )r$   r   clsr   r   r   __get__W   s   zObjectModel.__get__c                 C   s   ||   v S r    )r   r$   namer   r   r   __contains__a   s   zObjectModel.__contains__)maxsizec                 C   s   dd t | D S )z;Get the attributes which are exported by this object model.c                 S   s&   g | ]}| tr|ttd  qS r    )
startswithIMPL_PREFIXr0   )r   r   r   r   r   r   g   s
    
z*ObjectModel.attributes.<locals>.<listcomp>)dirr#   r   r   r   r   d   s   zObjectModel.attributesc                 C   s*   ||   v rt| t| S tj| j|d)zLook up the given *name* in the current model

        It should return an AST or an interpreter object,
        but if the name is not found, then an AttributeInferenceError will be raised.
        target	attribute)r   getattrrG   r   AttributeInferenceErrorr"   rB   r   r   r   lookupk   s   zObjectModel.lookupr    )r/   
__module____qualname__r%   r>   r?   rA   rD   r   r   rN   r   r   r   r   r   <   s    


r   c                   @   s   e Zd Zdd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zedd ZdS )ModuleModelc                 C   s   t jj}|jdS )N__dict__)astroidMANAGERbuiltins_moduleZspecial_attributesrN   )r$   builtins_ast_moduler   r   r   	_builtinsx   s   zModuleModel._builtinsc                 C   s   |   S r    )rW   r#   r   r   r   attr_builtins|      zModuleModel.attr_builtinsc                    sJ    j jstj j dd fdd j jD }tj j d}|| |S )N__path__rI   c                    s2   g | ]}t j|d s|ntj| jdqS )z__init__.pyr	   )r   r   endswithospathdirnamer"   )r   r]   r#   r   r   r      s    
z-ModuleModel.attr___path__.<locals>.<listcomp>r   )r"   packager   rM   r]   r   Listr   )r$   Z	path_objs	containerr   r#   r   attr___path__   s   


zModuleModel.attr___path__c                 C      t j| jj| jdS Nr	   r   r   r"   rC   r#   r   r   r   attr___name__      zModuleModel.attr___name__c                 C   rc   rd   r   r   r"   docr#   r   r   r   attr___doc__   rg   zModuleModel.attr___doc__c                 C   rc   rd   )r   r   r"   filer#   r   r   r   attr___file__   rg   zModuleModel.attr___file__c                 C      t | j| jjS r    )r   r"   globalsr#   r   r   r   attr___dict__      zModuleModel.attr___dict__c                 C   s&   | j jsd}n| j j}tj|| j dS )N r	   )r"   r_   rC   r   r   )r$   r
   r   r   r   attr___package__   s   zModuleModel.attr___package__c                 C      t  S r    r   Unknownr#   r   r   r   attr___spec__      zModuleModel.attr___spec__c                 C   rs   r    rt   r#   r   r   r   attr___loader__   rw   zModuleModel.attr___loader__c                 C   rs   r    rt   r#   r   r   r   attr___cached__   rw   zModuleModel.attr___cached__N)r/   rO   rP   rW   propertyrX   rb   rf   rj   rl   ro   rr   rv   rx   ry   r   r   r   r   rQ   w   s,    








rQ   c                   @   s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	e	Z
edd Zedd Zedd Zedd ZeZeZeZeZeZeZeZeZeZeZeZeZeZeZeZeZeZeZ eZ!eZ"eZ#dS )FunctionModelc                 C   rc   rd   re   r#   r   r   r   rf      rg   zFunctionModel.attr___name__c                 C   rc   rd   rh   r#   r   r   r   rj      rg   zFunctionModel.attr___doc__c                 C   s   t j| j | jdS rd   r   r   r"   qnamer#   r   r   r   attr___qualname__   s   zFunctionModel.attr___qualname__c                 C   s:   | j }|jjstjd |dS tj|d}||jj |S )Nr	   r   )r"   argsdefaultsr   r   Tupler   )r$   funcZdefaults_objr   r   r   attr___defaults__   s   zFunctionModel.attr___defaults__c                    s   t j| jd | jjsd }n| jj}| jj}tt|jpg |jt|j	|j
}dd |D }|jr8|j||j< |jrA|j||j< |rG||d<  fdd| D } |  S )Nr   c                 S   s   i | ]
\}}|r|j |qS r   rC   )r   arg
annotationr   r   r   
<dictcomp>   s
    z6FunctionModel.attr___annotations__.<locals>.<dictcomp>returnc                    s"   g | ]\}}t j| d |fqS )r   r   )r   keyr
   r   r   r   r      s    z6FunctionModel.attr___annotations__.<locals>.<listcomp>)r   r   r"   returnsr   	itertoolschainr   annotations
kwonlyargsZkwonlyargs_annotationsZvarargannotationvarargZkwargannotationkwargitemsr   )r$   r   r   Zpair_annotationsr   r   r   r   r   attr___annotations__   s.   

z"FunctionModel.attr___annotations__c                 C      t j| jdS Nr   r   r   r"   r#   r   r   r   ro         zFunctionModel.attr___dict__c                 C   sB   dd }| j j}tj| j d}t|||}|t|  |S )Nc              	   s   sR    | j D ]"}z| |j}W n
 tjy   Y qw tj|j|d}||fV  qd S r   )r   default_valuerC   r   Z	NoDefaultr   r   )r   r   r   defaultrC   r   r   r   _default_args  s   
z8FunctionModel.attr___kwdefaults__.<locals>._default_argsr   )r"   r   r   r   dictr   r   r   )r$   r   r   r   r   r   r   r   attr___kwdefaults__  s   
z!FunctionModel.attr___kwdefaults__c                 C      t | j  S r    r   r   r"   rootr}   r#   r   r   r   attr___module__  rg   zFunctionModel.attr___module__c                    s:   ddl m  | jG  fddd j}|| j| jdS )Nr   basesc                       s8   e Zd ZdZdd Zd	 fdd	ZefddZdS )
z9FunctionModel.attr___get__.<locals>.DescriptorBoundMethodzFBound method which knows how to understand calling descriptor binding.c                 S   s   dS Nr   r   r#   r   r   r   implicit_parameters!  s   zMFunctionModel.attr___get__.<locals>.DescriptorBoundMethod.implicit_parametersNc                 3   s    t |jdkrtjd| |dt|}t|jd j|d}|tj	u r-tjd| |dt
 jr8V  d S jjjjj|d}|jjjj  |} j||dV  d S )	Nr(   z(Invalid arguments for descriptor binding)rJ   r   r   r   zInvalid class inferred)rC   ri   lineno
col_offsetr   proxybound)r0   r   r   ZInferenceError
contextmodcopy_contextnextZinferrS   ZUninferable
isinstanceBoundMethod	__class__rC   ri   r   r   r   bodyZ
decoratorsr   ZUnboundMethod)r$   callerr   r@   new_funcr   r   r   r   r   infer_call_result&  s4   


zKFunctionModel.attr___get__.<locals>.DescriptorBoundMethod.infer_call_resultc                    sF    j j  }|tjdd tj j jd}|j|g g g g d |S )a  Overwrite the underlying args to match those of the underlying func

                Usually the underlying *func* is a function/method, as in:

                    def test(self):
                        pass

                This has only the *self* parameter but when we access test.__get__
                we get a new object which has two parameters, *self* and *type*.
                r.   r   r   )r   r   r   kw_defaultsr   )r   copyr5   rS   Z
AssignName	Argumentsr   r   )r$   params	arguments)r   r   r   r   L  s   z>FunctionModel.attr___get__.<locals>.DescriptorBoundMethod.argsr    )r/   rO   rP   __doc__r   r   rz   r   r   r   r   r   DescriptorBoundMethod  s    &r   r   )rS   r   r"   r   )r$   r   r   r   r   attr___get__  s   GzFunctionModel.attr___get__c                 C   rs   r    rt   r#   r   r   r   attr___ne__h  rY   zFunctionModel.attr___ne__N)$r/   rO   rP   rz   rf   rj   r~   r   r   ro   Zattr___globals__r   r   r   r   Zattr___subclasshook__Zattr___str__Zattr___sizeof__Zattr___setattr___Zattr___repr__Zattr___reduce__Zattr___reduce_ex__Zattr___new__Zattr___lt__Zattr___eq__Zattr___gt__Zattr___format__Zattr___delattr___Zattr___getattribute__Zattr___hash__Zattr___init__Zattr___dir__Zattr___call__attr___class__Zattr___closure__Zattr___code__r   r   r   r   r{      sV    



	
 



O
r{   c                   @   s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd ZdS )
ClassModelc                 C   r   r    r   r#   r   r   r   r     rg   zClassModel.attr___module__c                 C      t | jjS r    re   r#   r   r   r   rf     r   zClassModel.attr___name__c                 C   s   t | j S r    r|   r#   r   r   r   r~     rp   zClassModel.attr___qualname__c                 C   r   r    rh   r#   r   r   r   rj     r   zClassModel.attr___doc__c                 C   s>   | j jstj| j dd| j  }tj| j d}|| |S )N__mro__rI   r   )r"   newstyler   rM   mror   r   r   )r$   r   r   r   r   r   attr___mro__  s   

zClassModel.attr___mro__c                    sb   | j jstj| j ddddlm} |  G  fddd|j}| j  }|jd d }|||dS )Nr   rI   r   r   c                          e Zd Zd fdd	ZdS )z+ClassModel.attr_mro.<locals>.MroBoundMethodNc                 3   s     j V  d S r    )r   r$   r   r   Z
other_selfr   r   r     s   z=ClassModel.attr_mro.<locals>.MroBoundMethod.infer_call_resultr    r/   rO   rP   r   r   r   r   r   MroBoundMethod      r   r   )	r"   r   r   rM   rS   r   r   implicit_metaclasslocals)r$   r   r   r   Z
mro_methodr   r   r   attr_mro  s   
zClassModel.attr_mroc                 C   s0   t  }t }t| j|}|j|d |S )Nelts)r   r   r   ZInferenceContextr   r"   Z_inferred_basesr   )r$   r   r   r   r   r   r   attr___bases__  s
   zClassModel.attr___bases__c                 C      ddl m} || jS Nr   )helpersrS   r   Zobject_typer"   r$   r   r   r   r   r        zClassModel.attr___class__c                    s   ddl m} ddl m} jjstjjddj j }fdd|	|j
D }tjjd  | G  fd	d
d
|j}j }|jd d }|||dS )zGet the subclasses of the underlying class

        This looks only in the current module for retrieving the subclasses,
        thus it might miss a couple of them.
        r   r   )scoped_nodes__subclasses__rI   c                    s$   g | ]}|j kr| r|qS r   )r"   Zis_subtype_of)r   r@   )r}   r$   r   r   r     s    z2ClassModel.attr___subclasses__.<locals>.<listcomp>r   c                       r   )z=ClassModel.attr___subclasses__.<locals>.SubclassesBoundMethodNc                 3        V  d S r    r   r   r   r   r   r        
zOClassModel.attr___subclasses__.<locals>.SubclassesBoundMethod.infer_call_resultr    r   r   r   r   r   SubclassesBoundMethod  r   r   r   )rS   r   r   r"   r   r   rM   r}   r   Znodes_of_classClassDefr   r`   r   r   r   r   )r$   r   r   r   classesr   r   Zsubclasses_methodr   )r   r}   r$   r   attr___subclasses__  s"   




zClassModel.attr___subclasses__c                 C   r   r   r   r#   r   r   r   ro     r   zClassModel.attr___dict__N)r/   rO   rP   rz   r   rf   r~   rj   r   r   r   r   r   ro   r   r   r   r   r     s*    








!r   c                   @   <   e Zd Zedd Zedd Zedd Zedd Zd	S )

SuperModelc                 C      | j jS r    )r"   Zmro_pointerr#   r   r   r   attr___thisclass__  rY   zSuperModel.attr___thisclass__c                 C   r   r    )r"   Z_self_classr#   r   r   r   attr___self_class__  rY   zSuperModel.attr___self_class__c                 C   r   r    )r"   r.   r#   r   r   r   attr___self__  rY   zSuperModel.attr___self__c                 C   r   r    r"   _proxiedr#   r   r   r   r     rY   zSuperModel.attr___class__N)r/   rO   rP   rz   r   r   r   r   r   r   r   r   r         


r   c                   @   s<   e Zd Zedd Zedd Zedd ZeZeZeZ	dS )UnboundMethodModelc                 C   r   r   r   r   r   r   r   r     r   z!UnboundMethodModel.attr___class__c                 C   r   r    r   r#   r   r   r   attr___func__  rY   z UnboundMethodModel.attr___func__c                 C   s   t jd | jdS rd   )r   r   r"   r#   r   r   r   r     rp   z UnboundMethodModel.attr___self__N)
r/   rO   rP   rz   r   r   r   Zattr_im_funcZattr_im_classZattr_im_selfr   r   r   r   r     s    


r   c                   @   $   e Zd Zedd Zedd ZdS )BoundMethodModelc                 C   s
   | j jjS r    r   r#   r   r   r   r        
zBoundMethodModel.attr___func__c                 C   r   r    )r"   r   r#   r   r   r   r     rY   zBoundMethodModel.attr___self__N)r/   rO   rP   rz   r   r   r   r   r   r   r     
    
r   c                       s4   e Zd Z fddZedd Zedd Z  ZS )GeneratorModelc           	         sn   t t| j| g|R i |}tjjd }|j D ]\}}|d }|fdd}tt	|t
| t| q|S )N	generatorr   c                 S      |S r    r   r@   methr   r   r   <lambda>!      z(GeneratorModel.__new__.<locals>.<lambda>)superr   __new__rS   rT   rU   r   r   setattrr.   rG   rz   )	r@   r   kwargsretr   rC   r   methodpatchedr   r   r   r     s   zGeneratorModel.__new__c                 C      t j| jjj| jdS rd   )r   r   r"   r   rC   r#   r   r   r   rf   '     zGeneratorModel.attr___name__c                 C   r   rd   )r   r   r"   r   ri   r#   r   r   r   rj   -  r   zGeneratorModel.attr___doc__)r/   rO   rP   r   rz   rf   rj   __classcell__r   r   r   r   r     s    
r   c                       s   e Zd Z fddZ  ZS )AsyncGeneratorModelc           
         s   t  j| g|R i |}tjj}|d}|d u r|d}|j D ]\}}|d }|fdd}	tt	|t
| t|	 q$|S )Nasync_generatorr   r   c                 S   r   r    r   r   r   r   r   r   @  r   z-AsyncGeneratorModel.__new__.<locals>.<lambda>)r   r   rS   rT   rU   getr   r   r   r.   rG   rz   )
r@   r   r   r   Zastroid_builtinsr   rC   r   r   r   r   r   r   r   5  s   

zAsyncGeneratorModel.__new__)r/   rO   rP   r   r   r   r   r   r   r   4  s    r   c                   @   r   )
InstanceModelc                 C   r   r    r   r#   r   r   r   r   H  rY   zInstanceModel.attr___class__c                 C   r   r    r   r#   r   r   r   r   L  rg   zInstanceModel.attr___module__c                 C   r   r    rh   r#   r   r   r   rj   P  r   zInstanceModel.attr___doc__c                 C   rm   r    )r   r"   Zinstance_attrsr#   r   r   r   ro   T  rp   zInstanceModel.attr___dict__N)r/   rO   rP   rz   r   r   rj   ro   r   r   r   r   r  G  r   r  c                   @   r   )ExceptionInstanceModelc                 C   s(   t d}t j| jd}||f |S )Nrq   r   )r   r   r   r"   r   )r$   messager   r   r   r   	attr_args]  s   
z ExceptionInstanceModel.attr_argsc                 C   s   t jj}|tjj }| S r    )rS   rT   rU   typesTracebackTyper/   Zinstantiate_class)r$   rV   Ztraceback_typer   r   r   attr___traceback__d  s   z)ExceptionInstanceModel.attr___traceback__N)r/   rO   rP   rz   r  r  r   r   r   r   r  \  s
    
r  c                   @   s   e Zd Zedd ZdS )SyntaxErrorInstanceModelc                 C   
   t dS Nrq   r   r#   r   r   r   	attr_textl  r   z"SyntaxErrorInstanceModel.attr_textN)r/   rO   rP   rz   r  r   r   r   r   r	  k  s    r	  c                   @   s4   e Zd Zedd Zedd Zedd ZeZdS )OSErrorInstanceModelc                 C   r
  r  r   r#   r   r   r   attr_filenamer  r   z"OSErrorInstanceModel.attr_filenamec                 C   r
  r   r   r#   r   r   r   
attr_errnov  r   zOSErrorInstanceModel.attr_errnoc                 C   r
  r  r   r#   r   r   r   attr_strerrorz  r   z"OSErrorInstanceModel.attr_strerrorN)r/   rO   rP   rz   r  r  r  Zattr_filename2r   r   r   r   r  q  s    


r  c                   @   r   )ImportErrorInstanceModelc                 C   r
  r  r   r#   r   r   r   	attr_name  r   z"ImportErrorInstanceModel.attr_namec                 C   r
  r  r   r#   r   r   r   	attr_path  r   z"ImportErrorInstanceModel.attr_pathN)r/   rO   rP   rz   r  r  r   r   r   r   r    r   r  zbuiltins.SyntaxErrorzbuiltins.ImportErrorzbuiltins.OSErrorzbuiltins.BlockingIOErrorzbuiltins.BrokenPipeErrorzbuiltins.ChildProcessErrorzbuiltins.ConnectionAbortedErrorzbuiltins.ConnectionErrorzbuiltins.ConnectionRefusedErrorzbuiltins.ConnectionResetErrorzbuiltins.FileExistsErrorzbuiltins.FileNotFoundErrorzbuiltins.InterruptedErrorzbuiltins.IsADirectoryErrorzbuiltins.NotADirectoryErrorzbuiltins.PermissionErrorzbuiltins.ProcessLookupErrorzbuiltins.TimeoutErrorc                   @   sD   e Zd Zedd Zdd Zedd Zedd Zed	d
 ZdS )	DictModelc                 C   r   r    r   r#   r   r   r   r     rY   zDictModel.attr___class__c                    s6   G  fdddt j}t| jj|}||| jdS )z7Generate a bound method that can infer the given *obj*.c                       r   )z@DictModel._generic_dict_attribute.<locals>.DictMethodBoundMethodNc                 3   r   r    r   r   r   r   r   r     r   zRDictModel._generic_dict_attribute.<locals>.DictMethodBoundMethod.infer_call_resultr    r   r   r   r   r   DictMethodBoundMethod  r   r  r   )rS   r   r   r"   r   Zigetattr)r$   r   rC   r  r   r   r   r   _generic_dict_attribute  s   z!DictModel._generic_dict_attributec                 C   sv   g }t j| jd}| jjD ]\}}t j|d}|||f || q|j|d ddlm} |	|}| 
|dS )Nr   r   r   objectsr   )r   r`   r"   r   r   r   r5   rS   r  Z	DictItemsr  )r$   elemsr   r   r
   r   r  r   r   r   
attr_items  s   
zDictModel.attr_itemsc                 C   sN   dd | j jD }tj| j d}|j|d ddlm} ||}| |dS )Nc                 S   s   g | ]\}}|qS r   r   )r   r   _r   r   r   r     r   z'DictModel.attr_keys.<locals>.<listcomp>r   r   r   r  r   )	r"   r   r   r`   r   rS   r  ZDictKeysr  )r$   r   r   r  r   r   r   	attr_keys  s   
zDictModel.attr_keysc                 C   sL   dd | j jD }tj| j d}|| ddlm} ||}| |dS )Nc                 S   s   g | ]\}}|qS r   r   )r   r  r
   r   r   r   r     r   z)DictModel.attr_values.<locals>.<listcomp>r   r   r  r   )	r"   r   r   r`   r   rS   r  Z
DictValuesr  )r$   r   r   r  r   r   r   attr_values  s   

zDictModel.attr_valuesN)	r/   rO   rP   rz   r   r  r  r  r  r   r   r   r   r    s    



r  )r   r   r2   r\   r  	functoolsr   rS   r   r   r   r   rG   r   r   rQ   r{   r   r   r   r   r   r   r  r  r	  r  r  ZBUILTIN_EXCEPTIONSr  r   r   r   r   <module>   s   ;L Ag

	
