a
    lc                      @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZmZ ddlmZmZmZ ddlmZ ddlmZ G dd dZdS )a
  
Type inference of Python code in |jedi| is based on three assumptions:

* The code uses as least side effects as possible. Jedi understands certain
  list/tuple/set modifications, but there's no guarantee that Jedi detects
  everything (list.append in different modules for example).
* No magic is being used:

  - metaclasses
  - ``setattr()`` / ``__import__()``
  - writing to ``globals()``, ``locals()``, ``object.__dict__``
* The programmer is not a total dick, e.g. like `this
  <https://github.com/davidhalter/jedi/issues/24>`_ :-)

The actual algorithm is based on a principle I call lazy type inference.  That
said, the typical entry point for static analysis is calling
``infer_expr_stmt``. There's separate logic for autocompletion in the API, the
inference_state is all about inferring an expression.

TODO this paragraph is not what jedi does anymore, it's similar, but not the
same.

Now you need to understand what follows after ``infer_expr_stmt``. Let's
make an example::

    import datetime
    datetime.date.toda# <-- cursor here

First of all, this module doesn't care about completion. It really just cares
about ``datetime.date``. At the end of the procedure ``infer_expr_stmt`` will
return the ``date`` class.

To *visualize* this (simplified):

- ``InferenceState.infer_expr_stmt`` doesn't do much, because there's no assignment.
- ``Context.infer_node`` cares for resolving the dotted path
- ``InferenceState.find_types`` searches for global definitions of datetime, which
  it finds in the definition of an import, by scanning the syntax tree.
- Using the import logic, the datetime module is found.
- Now ``find_types`` is called again by ``infer_node`` to find ``date``
  inside the datetime module.

Now what would happen if we wanted ``datetime.date.foo.bar``? Two more
calls to ``find_types``. However the second call would be ignored, because the
first one would return nothing (there's no foo attribute in ``date``).

What if the import would contain another ``ExprStmt`` like this::

    from foo import bar
    Date = bar.baz

Well... You get it. Just another ``infer_expr_stmt`` recursion. It's really
easy. Python can obviously get way more complicated then this. To understand
tuple assignments, list comprehensions and everything else, a lot more code had
to be written.

Jedi has been tested very well, so you can just start modifying code. It's best
to write your own test first for your "new" feature. Don't be scared of
breaking stuff. As long as the tests pass, you're most likely to be fine.

I need to mention now that lazy type inference is really good because it
only *inferes* what needs to be *inferred*. All the statements and modules
that are not used are just being ignored.
    N)FileIO)debug)settings)imports)	recursion)inference_state_function_cache)helpers)TreeNameDefinition)ContextualizedNodeValueSetiterate_values)
ClassValueFunctionValue)infer_expr_stmtcheck_tuple_assignmentstree_name_to_values)%follow_error_node_imports_if_possible)plugin_managerc                   @   s   e Zd ZdddZdddZee dd Ze	e
 d	d
 Ze	e
 dd Zdd Zdd Zdd ZdddZdd ZdS )InferenceStateNc                 C   s   |d u r|  }|| _|| _|| | _| | _tjdd| _	i | _
t | _i | _i | _i | _i | _g | _d| _d| _|| _i | _d| _d| _|   d S )Nz3.7)versionr   FT)get_environmentenvironmentscript_pathZget_inference_state_subprocessZcompiled_subprocessZget_grammargrammarparsoZload_grammarlatest_grammarZmemoize_cacher   ZModuleCacheZmodule_cacheZstub_module_cacheZcompiled_cacheZinferred_element_countsZmixed_cacheZanalysisZdynamic_params_depthZis_analysisprojectZaccess_cacheZallow_descriptor_getattrZflow_analysis_enabledreset_recursion_limitations)selfr   r   r    r   S/var/www/html/django/DPS/env/lib/python3.9/site-packages/jedi/inference/__init__.py__init__U   s*    

zInferenceState.__init__Tc                 C   s   t j| |||dS )N)prefer_stubs)r   Zimport_module_by_names)r   Zimport_namessys_pathr"   r   r   r    import_modulen   s    zInferenceState.import_modulec                 C   sT   t d| | t   | j|d}W d    n1 s80    Y  t d||  |S )Nzexecute: %s %s)	argumentszexecute result: %s in %s)r   ZdbgZincrease_indent_cmZ
py__call__)valuer%   Z	value_setr   r   r    executer   s
    
*zInferenceState.executec                 C   s   d}| j |fdd\}|S )Nbuiltinsr   )r#   r$   )r   module_namebuiltins_moduler   r   r    r+   |   s    zInferenceState.builtins_modulec                 C   s   |  d\}|S )N)typingr)   )r   typing_moduler   r   r    r-      s    zInferenceState.typing_modulec                 C   s   t  | _t | | _d S )N)r   ZRecursionDetectorZrecursion_detectorZExecutionRecursionDetectorZexecution_recursion_detector)r   r   r   r    r      s    
z*InferenceState.reset_recursion_limitationsc                 K   s   | j j| fi |S )zConvenience function)r   Z_get_sys_path)r   kwargsr   r   r    get_sys_path   s    zInferenceState.get_sys_pathc                 C   sD  |j dd}|d ur |j}|dk}|s0|dkr\|rDt| ||j}nt||j}t|gS |dkr|jjdv}|rt|||S |dkr||j	d }t
||j	d }	t||	}
t||}t||
S |d	v rt||S |d
krt| ||S |dkr
|j|j|jdS |dkr8||S nt||}|d ur8|S t||S )NT)Zimport_name_alwaysZclassdefZfuncdefZ	expr_stmt)powertrailerZfor_stmt   )Zimport_fromimport_nameZ	with_stmtparam)positionZnamedexpr_test)Zget_definitiontyper   parentr   Zfrom_contextr   r   Z
infer_nodechildrenr
   r   r	   r   r   Zinfer_importr   Zpy__getattribute__r&   end_posr   r   Zinfer_call_of_leaf)r   contextnameZdef_type_Zis_classdefcZis_simple_nameZcontainer_typesZcnZ	for_typesnresultr   r   r    infer   s>    








zInferenceState.inferFc                 K   sx   |d u r |d u rt |}| }tj|ddd}t|tjkrL|d tj }|rV| jn| j}|j	f |||d||fS )Nzutf-8replace)encodingerrors)codepathfile_io)
r   readr   Zpython_bytes_to_unicodelenr   Z_cropped_file_sizer   r   parse)r   rD   rE   Zuse_latest_grammarrF   r.   r   r   r   r    parse_and_get_code   s    z!InferenceState.parse_and_get_codec                 O   s   | j |i |d S )Nr   )rJ   )r   argsr.   r   r   r    rI      s    zInferenceState.parse)NN)NT)NNFN)__name__
__module____qualname__r!   r$   staticmethodr   Zdecorater'   propertyr   r+   r-   r   r/   r@   rJ   rI   r   r   r   r    r   T   s$   

%  
r   )__doc__r   Zjedi.file_ior   Zjedir   r   Zjedi.inferencer   r   Zjedi.inference.cacher   r   Zjedi.inference.namesr	   Zjedi.inference.base_valuer
   r   r   Zjedi.inference.valuer   r   Zjedi.inference.syntax_treer   r   r   Zjedi.inference.importsr   Zjedi.pluginsr   r   r   r   r   r    <module>   s   @