Format: application/x-python-code
⬇ Download File (7.6 KB)
   1  o
   2  
   3  �h9!�@s�dZddlZddlmZddlmZddlmZddlmZddlm	Z	ddl
   4  mZdd	lmZdd
   5  l
   6  mZddlmZmZmZddlmZmZdd
   7  lmZmZmZddlmZddlmZGdd�d�ZdS)a�
   8  
   9  Type inference of Python code in |jedi| is based on three assumptions:
  10  
  11  * The code uses as least side effects as possible. Jedi understands certain
  12    list/tuple/set modifications, but there's no guarantee that Jedi detects
  13    everything (list.append in different modules for example).
  14  * No magic is being used:
  15  
  16    - metaclasses
  17    - ``setattr()`` / ``__import__()``
  18    - writing to ``globals()``, ``locals()``, ``object.__dict__``
  19  * The programmer is not a total dick, e.g. like `this
  20    <https://github.com/davidhalter/jedi/issues/24>`_ :-)
  21  
  22  The actual algorithm is based on a principle I call lazy type inference.  That
  23  said, the typical entry point for static analysis is calling
  24  ``infer_expr_stmt``. There's separate logic for autocompletion in the API, the
  25  inference_state is all about inferring an expression.
  26  
  27  TODO this paragraph is not what jedi does anymore, it's similar, but not the
  28  same.
  29  
  30  Now you need to understand what follows after ``infer_expr_stmt``. Let's
  31  make an example::
  32  
  33      import datetime
  34      datetime.date.toda# <-- cursor here
  35  
  36  First of all, this module doesn't care about completion. It really just cares
  37  about ``datetime.date``. At the end of the procedure ``infer_expr_stmt`` will
  38  return the ``date`` class.
  39  
  40  To *visualize* this (simplified):
  41  
  42  - ``InferenceState.infer_expr_stmt`` doesn't do much, because there's no assignment.
  43  - ``Context.infer_node`` cares for resolving the dotted path
  44  - ``InferenceState.find_types`` searches for global definitions of datetime, which
  45    it finds in the definition of an import, by scanning the syntax tree.
  46  - Using the import logic, the datetime module is found.
  47  - Now ``find_types`` is called again by ``infer_node`` to find ``date``
  48    inside the datetime module.
  49  
  50  Now what would happen if we wanted ``datetime.date.foo.bar``? Two more
  51  calls to ``find_types``. However the second call would be ignored, because the
  52  first one would return nothing (there's no foo attribute in ``date``).
  53  
  54  What if the import would contain another ``ExprStmt`` like this::
  55  
  56      from foo import bar
  57      Date = bar.baz
  58  
  59  Well... You get it. Just another ``infer_expr_stmt`` recursion. It's really
  60  easy. Python can obviously get way more complicated then this. To understand
  61  tuple assignments, list comprehensions and everything else, a lot more code had
  62  to be written.
  63  
  64  Jedi has been tested very well, so you can just start modifying code. It's best
  65  to write your own test first for your "new" feature. Don't be scared of
  66  breaking stuff. As long as the tests pass, you're most likely to be fine.
  67  
  68  I need to mention now that lazy type inference is really good because it
  69  only *inferes* what needs to be *inferred*. All the statements and modules
  70  that are not used are just being ignored.
  71  �N)�FileIO)�debug)�settings)�imports)�	recursion)�inference_state_function_cache)�helpers)�TreeNameDefinition)�ContextualizedNode�ValueSet�iterate_values)�
  72  ClassValue�
  73  FunctionValue)�infer_expr_stmt�check_tuple_assignments�tree_name_to_values)�%follow_error_node_imports_if_possible)�plugin_managerc@s�eZdZddd�Zddd�Zee��dd���Ze	e
  74  �d	d
  75  ���Ze	e
  76  �dd���Zd
  77  d�Z
  78  dd�Zdd�Z		ddd�Zdd�ZdS)�InferenceStateNcCs�|dur|��}||_||_|�|�|_|��|_tjdd�|_	i|_
  79  t��|_
  80  i|_i|_i|_i|_g|_d|_tj|_d|_||_i|_d|_d|_|��dS)Nz3.13)�versionrFT)�get_environment�environment�script_pathZget_inference_state_subprocessZcompiled_subprocessZget_grammar�grammar�parsoZload_grammar�latest_grammarZ
  81  memoize_cacherZModuleCacheZmodule_cacheZstub_module_cacheZcompiled_cacheZinferred_element_countsZmixed_cacheZanalysisZdynamic_params_depthrZdynamic_paramsZdo_dynamic_params_searchZis_analysis�projectZaccess_cacheZallow_unsafe_executionsZflow_analysis_enabled�reset_recursion_limitations)�selfrrr�r�Mc:\Users\shubh\Videos\agent\nova\Lib\site-packages\jedi/inference/__init__.py�__init__Us,
  82  
  83  zInferenceState.__init__TcCstj||||d�S)N)�prefer_stubs)rZimport_module_by_names)rZimport_names�sys_pathr"rrr �
  84  import_moduleos�zInferenceState.import_modulecCsTt�d||�t���|j|d�}Wd�n1swYt�d||�|S)Nzexecute: %s %s)�	argumentszexecute result: %s in %s)rZdbgZincrease_indent_cmZ
  85  py__call__)�valuer%Z	value_setrrr �executess
  86  �zInferenceState.executecCsd}|j|fgd�\}|S)N�builtins)r#�r$)r�module_name�builtins_modulerrr r+}szInferenceState.builtins_modulecCs|�d�\}|S)N)�typingr))r�
  87  typing_modulerrr r-�szInferenceState.typing_modulecCst��|_t�|�|_dS)N)rZRecursionDetectorZrecursion_detectorZExecutionRecursionDetectorZexecution_recursion_detector)rrrr r�s
  88  z*InferenceState.reset_recursion_limitationscKs|jj|fi|��S)zConvenience function)rZ
  89  _get_sys_path)r�kwargsrrr �get_sys_path�szInferenceState.get_sys_pathc
  90  Cs<|jdd�}|dur�|j}|dk}|s|dkr-|r!t|||j�}nt�||j�}t|g�S|dkr?|jjdv}|r?t|||�S|dkrb|�|j	d�}t
  91  ||j	d�}	t||	�}
  92  t||�}t
  93  ||
  94  �S|d	vrlt�||�S|d
  95  krvt|||�S|dkr�|j|j|jd�S|d
  96  kr�|�|�Snt||�}|dur�|St�||�S)NT)Zimport_name_alwaysZclassdefZfuncdefZ	expr_stmt)�power�trailerZfor_stmt�)Zimport_from�import_nameZ	with_stmt�param)�positionZnamedexpr_test)Zget_definition�typer
  97  �parentrZfrom_contextrrZ
  98  infer_node�childrenr
  99  rr	rrZinfer_importrZpy__getattribute__r&�end_posrrZinfer_call_of_leaf)
 100  r�context�nameZdef_�type_Zis_classdef�cZis_simple_nameZcontainer_typesZcnZ	for_types�n�resultrrr �infer�s@
 101  
 102  
 103  
 104  
 105  �
 106  zInferenceState.inferFcKsx|dur|durt|�}|��}tj|ddd�}t|�tjkr&|dtj�}|r+|jn|j}|j	d|||d�|��|fS)Nzutf-8�replace)�encoding�errors)�code�path�file_ior)
 107  r�readrZpython_bytes_to_unicode�lenrZ_cropped_file_sizerr�parse)rrDrEZuse_latest_grammarrFr.rrrr �parse_and_get_code�sz!InferenceState.parse_and_get_codecOs|j|i|��dS)Nr)rJ)r�argsr.rrr rI�szInferenceState.parse)NN)NT)NNFN)�__name__�
 108  __module__�__qualname__r!r$�staticmethodrZdecorater'�propertyrr+r-rr/r@rJrIrrrr rTs&
 109  
 110  %
 111  �r)�__doc__rZjedi.file_iorZjedirrZjedi.inferencerrZjedi.inference.cacherrZjedi.inference.namesr	Zjedi.inference.base_valuer
 112  rrZjedi.inference.valuer
 113  rZjedi.inference.syntax_treerrrZjedi.inference.importsrZjedi.pluginsrrrrrr �<module>s @