Format: application/x-python-code
1 o
2
3 �h9! � @ s� d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm Z ddl
4 mZ dd lmZ dd
5 l
6 mZ ddlmZmZmZ ddlmZmZ dd
7 lmZmZmZ ddlmZ ddlmZ G dd� 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� e Zd Zddd�Zddd�Zee�� dd� ��Ze e
74 � d d
75 � ��Ze e
76 � dd� ��Zd
77 d� Z
78 dd� Zdd� Z ddd�Zdd� ZdS )�InferenceStateNc C s� |d u r|� � }|| _|| _|�| �| _|�� | _tjdd�| _ i | _
79 t�� | _
80 i | _i | _i | _i | _g | _d| _tj| _d| _|| _i | _d| _d| _| �� d S )Nz3.13)�versionr FT)�get_environment�environment�script_pathZget_inference_state_subprocessZcompiled_subprocessZget_grammar�grammar�parsoZload_grammar�latest_grammarZ
81 memoize_cacher ZModuleCacheZmodule_cacheZstub_module_cacheZcompiled_cacheZinferred_element_countsZmixed_cacheZanalysisZdynamic_params_depthr Zdynamic_paramsZdo_dynamic_params_searchZis_analysis�projectZaccess_cacheZallow_unsafe_executionsZflow_analysis_enabled�reset_recursion_limitations)�selfr r r � r �Mc:\Users\shubh\Videos\agent\nova\Lib\site-packages\jedi/inference/__init__.py�__init__U s,
82
83 zInferenceState.__init__Tc C s t j| |||d�S )N)�prefer_stubs)r Zimport_module_by_names)r Zimport_names�sys_pathr"