Debian Codegraph @

doc/inference.rst

raw
1 .. _inference:
2
3 Inference Introduction
4 ======================
5
6 What/where is 'inference' ?
7 ---------------------------
8
9
10 The inference is a mechanism through which *astroid* tries to interpret
11 statically your Python code.
12
13 How does it work ?
14 ------------------
15
16 The magic is handled by :meth:`~astroid.nodes.NodeNG.infer` method.
17 *astroid* usually provides inference support for various Python primitives,
18 such as protocols and statements, but it can also be enriched
19 via `inference transforms`.
20
21 In both cases the :meth:`~astroid.nodes.NodeNG.infer` must return a *generator* which iterates
22 through the various *values* the node could take.
23
24 In some case the value yielded will not be a node found in the AST of the node
25 but an instance of a special inference class such as :obj:`~astroid.Uninferable`,
26 or :class:`~astroid.Instance`.
27
28 Namely, the special singleton :obj:`~astroid.Uninferable` is yielded when the inference reaches
29 a point where it can't follow the code and is so unable to guess a value; and
30 instances of the :class:`~astroid.Instance` class are yielded when the current node is
31 inferred to be an instance of some known class.
32
33
34 Crash course into astroid's inference
35 --------------------------------------
36
37 Let's see some examples on how the inference might work in ``astroid``.
38
39 First we'll need to do a detour through some of the ``astroid``'s APIs.
40
41 ``astroid`` offers a relatively similar API to the builtin ``ast`` module,
42 that is, you can do ``astroid.parse(string)`` to get an AST out of the given
43 string::
44
45 >>> tree = astroid.parse('a + b')
46 >>> tree
47 >>> <Module l.0 at 0x...>
48
49 >>> print(tree.repr_tree())
50 Module(
51 name='',
52 doc=None,
53 file='<?>',
54 path=['<?>'],
55 package=False,
56 pure_python=True,
57 future_imports=set(),
58 body=[Expr(value=BinOp(
59 op='+',
60 left=Name(name='a'),
61 right=Name(name='b')))])
62
63
64 The :meth:`~astroid.nodes.NodeNG.repr_tree` is super useful to inspect how a tree actually looks.
65 Most of the time you can access the same fields as those represented
66 in the output of :meth:`~astroid.nodes.NodeNG.repr_tree` so you can do ``tree.body[0].value.left``
67 to get the left hand side operand of the addition operation.
68
69 Another useful function that you can use is :func:`astroid.extract_node`,
70 which given a string, tries to extract one or more nodes from the given string::
71
72 >>> node = astroid.extract_node('''
73 ... a = 1
74 ... b = 2
75 ... c
76 ... ''')
77
78 In that example, the node that is going to be returned is the last node
79 from the tree, so it will be the ``Name(c)`` node.
80 You can also use :func:`astroid.extract_node` to extract multiple nodes::
81
82 >>> nodes = astroid.extract_node('''
83 ... a = 1 #@
84 ... b = 2 #@
85 ... c
86 ... ''')
87
88 You can use ``#@`` comment to annotate the lines for which you want the
89 corresponding nodes to be extracted. In that example, what we're going to
90 extract is two ``Expr`` nodes, which is in astroid's parlance, two statements,
91 but you can access their underlying ``Assign`` nodes using the ``.value`` attribute.
92
93 Now let's see how can we use ``astroid`` to infer what's going on with your code.
94
95 The main method that you can use is :meth:`~astroid.nodes.NodeNG.infer`. It returns a generator
96 with all the potential values that ``astroid`` can extract for a piece of code::
97
98 >>> name_node = astroid.extract_node('''
99 ... a = 1
100 ... b = 2
101 ... c = a + b
102 ... c
103 ... ''')
104 >>> inferred = next(name_node.infer())
105 >>> inferred
106 <Const.int l.None at 0x...>
107 >>> inferred.value
108 3
109
110 From this example you can see that ``astroid`` is capable of *inferring* what ``c``
111 might hold, which is a constant value with the number 3.