module documentation
(source)

this module contains a set of functions to handle inference on astroid trees
Function infer​_arguments Undocumented
Function infer​_assign infer a AssignName/AssignAttr: need to inspect the RHS part of the assign node
Function infer​_attribute infer an Attribute node by using getattr on the associated object
Function infer​_augassign Undocumented
Function infer​_binop Undocumented
Function infer​_call infer a Call node by trying to guess what the function returns
Function infer​_empty​_node Undocumented
Function infer​_end Inference's end for nodes that yield themselves on inference
Function infer​_functiondef Undocumented
Function infer​_global Undocumented
Function infer​_ifexp Support IfExp inference
Function infer​_import infer an Import node: return the imported module/object
Function infer​_import​_from infer a ImportFrom node: return the imported module/object
Function infer​_index Undocumented
Function infer​_map Undocumented
Function infer​_name infer a Name: use name lookup rules
Function infer​_sequence Undocumented
Function infer​_subscript Inference for subscripts
Function infer​_unaryop Infer what an UnaryOp should return when evaluated.
Constant COMPARE​_OPS Undocumented
Constant UNINFERABLE​_OPS Undocumented
Variable objects Undocumented
Function _aug​_op Get an inference callable for an augmented binary operation.
Function _bin​_op Get an inference callable for a normal binary operation.
Function _cached​_generator Undocumented
Function _do​_compare No summary
Function _filter​_operation​_errors Undocumented
Function _get​_aug​_flow Get the flow for augmented binary operations.
Function _get​_binop​_contexts Get contexts for binary operations.
Function _get​_binop​_flow Get the flow for binary operations.
Function _higher​_function​_scope No summary
Function _infer​_augassign Inference logic for augmented binary operations.
Function _infer​_binary​_operation Infer a binary operation between a left operand and a right operand
Function _infer​_binop Binary operation inference logic.
Function _infer​_boolop Infer a boolean operation (and / or / not).
Function _infer​_compare Chained comparison inference logic.
Function _infer​_map Infer all values based on Dict.items
Function _infer​_sequence​_helper Infer all values based on _BaseContainer.elts
Function _infer​_unaryop Infer what an UnaryOp should return when evaluated.
Function _invoke​_binop​_inference Invoke binary operation inference on the given instance.
Function _is​_not​_implemented Check if the given const node is NotImplemented.
Function _populate​_context​_lookup Undocumented
Function _same​_type Check if type1 is the same as type2.
Function _to​_literal Undocumented
Function _update​_with​_replacement Delete nodes that equate to duplicate keys
Constant _SUBSCRIPT​_SENTINEL Undocumented
@decorators.raise_if_nothing_inferred
def infer_arguments(self, context=None): (source)

Undocumented

infer a AssignName/AssignAttr: need to inspect the RHS part of the assign node
def infer_attribute(self, context=None): (source)
infer an Attribute node by using getattr on the associated object

Undocumented

Undocumented

infer a Call node by trying to guess what the function returns

Undocumented

def infer_end(self, context=None): (source)

Inference's end for nodes that yield themselves on inference

These are objects for which inference does not have any semantic, such as Module or Consts.

@_cached_generator
def infer_functiondef(self, context=None): (source)

Undocumented

Undocumented

@decorators.raise_if_nothing_inferred
def infer_ifexp(self, context=None): (source)

Support IfExp inference

If we can't infer the truthiness of the condition, we default to inferring both branches. Otherwise, we infer either branch depending on the condition.

@decorators.raise_if_nothing_inferred
@decorators.path_wrapper
def infer_import(self, context=None, asname=True): (source)
infer an Import node: return the imported module/object
@decorators.raise_if_nothing_inferred
@decorators.path_wrapper
def infer_import_from(self, context=None, asname=True): (source)
infer a ImportFrom node: return the imported module/object
@decorators.raise_if_nothing_inferred
def infer_index(self, context=None): (source)

Undocumented

def infer_map(self, context=None): (source)

Undocumented

def infer_name(self, context=None): (source)
infer a Name: use name lookup rules
@decorators.raise_if_nothing_inferred
def infer_sequence(self, context=None): (source)

Undocumented

def infer_subscript(self, context=None): (source)

Inference for subscripts

We're understanding if the index is a Const or a slice, passing the result of inference to the value's getitem method, which should handle each supported index type accordingly.

Infer what an UnaryOp should return when evaluated.
COMPARE_OPS: Dict[str, Callable[[Any, Any], bool]] = (source)

Undocumented

Value
{'==': operator.eq,
 '!=': operator.ne,
 '<': operator.lt,
 '<=': operator.le,
 '>': operator.gt,
 '>=': operator.ge,
 'in': (lambda a, b: a in b),
...
UNINFERABLE_OPS: set[str] = (source)

Undocumented

Value
set(['is', 'is not'])
objects = (source)

Undocumented

def _aug_op(instance, opnode, op, other, context, reverse=False): (source)
Get an inference callable for an augmented binary operation.
def _bin_op(instance, opnode, op, other, context, reverse=False): (source)

Get an inference callable for a normal binary operation.

If reverse is True, then the reflected method will be used instead.

@wrapt.decorator
def _cached_generator(func, instance, args, kwargs, _cache={}): (source)

Undocumented

def _do_compare(left_iter, op, right_iter): (source)

If all possible combinations are either True or False, return that: >>> _do_compare([1, 2], '<=', [3, 4]) True >>> _do_compare([1, 2], '==', [3, 4]) False

If any item is uninferable, or if some combinations are True and some are False, return Uninferable: >>> _do_compare([1, 3], '<=', [2, 4]) util.Uninferable

Parameters
left​_iter:Iterable[nodes.NodeNG]Undocumented
op:strUndocumented
right​_iter:Iterable[nodes.NodeNG]Undocumented
Returns
bool|type[util.Uninferable]Undocumented
def _filter_operation_errors(self, infer_callable, context, error): (source)

Undocumented

def _get_aug_flow(left, left_type, aug_opnode, right, right_type, context, reverse_context): (source)

Get the flow for augmented binary operations.

The rules are a bit messy:

  • if left and right have the same type, then left.__augop__(right) is first tried and then left.__op__(right).
  • if left and right are unrelated typewise, then left.__augop__(right) is tried, then left.__op__(right) is tried and then right.__rop__(left) is tried.
  • if left is a subtype of right, then left.__augop__(right) is tried and then left.__op__(right).
  • if left is a supertype of right, then left.__augop__(right) is tried, then right.__rop__(left) and then left.__op__(right)
def _get_binop_contexts(context, left, right): (source)

Get contexts for binary operations.

This will return two inference contexts, the first one for x.__op__(y), the other one for y.__rop__(x), where only the arguments are inversed.

def _get_binop_flow(left, left_type, binary_opnode, right, right_type, context, reverse_context): (source)

Get the flow for binary operations.

The rules are a bit messy:

  • if left and right have the same type, then only one method will be called, left.__op__(right)
  • if left and right are unrelated typewise, then first left.__op__(right) is tried and if this does not exist or returns NotImplemented, then right.__rop__(left) is tried.
  • if left is a subtype of right, then only left.__op__(right) is tried.
  • if left is a supertype of right, then right.__rop__(left) is first tried and then left.__op__(right)
def _higher_function_scope(node): (source)
Search for the first function which encloses the given scope. This can be used for looking up in that function's scope, in case looking up in a lower scope for a particular name fails.
Parameters
nodeA scope node.
Returns
None, if no parent function scope was found, otherwise an instance of astroid.nodes.scoped_nodes.Function, which encloses the given node.
def _infer_augassign(self, context=None): (source)
Inference logic for augmented binary operations.
def _infer_binary_operation(left, right, binary_opnode, context, flow_factory): (source)

Infer a binary operation between a left operand and a right operand

This is used by both normal binary operations and augmented binary operations, the only difference is the flow factory used.

def _infer_binop(self, context): (source)
Binary operation inference logic.

Infer a boolean operation (and / or / not).

The function will calculate the boolean operation for all pairs generated through inference for each component node.

def _infer_compare(self, context=None): (source)
Chained comparison inference logic.
Parameters
context:Optional[InferenceContext]Undocumented
Returns
AnyUndocumented
def _infer_map(node, context): (source)
Infer all values based on Dict.items
def _infer_sequence_helper(node, context=None): (source)
Infer all values based on _BaseContainer.elts
def _infer_unaryop(self, context=None): (source)
Infer what an UnaryOp should return when evaluated.
def _invoke_binop_inference(instance, opnode, op, other, context, method_name): (source)
Invoke binary operation inference on the given instance.
def _is_not_implemented(const): (source)
Check if the given const node is NotImplemented.
def _populate_context_lookup(call, context): (source)

Undocumented

def _same_type(type1, type2): (source)
Check if type1 is the same as type2.
def _to_literal(node): (source)

Undocumented

Parameters
node:nodes.NodeNGUndocumented
Returns
AnyUndocumented
def _update_with_replacement(lhs_dict, rhs_dict): (source)

Delete nodes that equate to duplicate keys

Since an astroid node doesn't 'equal' another node with the same value, this function uses the as_string method to make sure duplicate keys don't get through

Note that both the key and the value are astroid nodes

Fixes issue with DictUnpack causing duplicte keys in inferred Dict items

Parameters
lhs​_dictUndocumented
rhs​_dictUndocumented
dict(nodes​.​Node​NG, nodes​.​Node​NG) lhs_dictDictionary to 'merge' nodes into
dict(nodes​.​Node​NG, nodes​.​Node​NG) rhs_dictDictionary with nodes to pull from
Returns
merged dictionary of nodes
_SUBSCRIPT_SENTINEL = (source)

Undocumented

Value
object()