diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2018-10-02 09:43:56 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-10-02 09:43:56 +0200 |
commit | add5f7b8eba427de9d39caae864bbc6dc37ef980 (patch) | |
tree | 86bb7df77f389062879365e97df0c3d53d38a8a6 /astroid/inference.py | |
parent | bb46cd198b93f3715da94089d42f39fc910892ff (diff) | |
download | astroid-git-add5f7b8eba427de9d39caae864bbc6dc37ef980.tar.gz |
Initial formatting of astroid
Diffstat (limited to 'astroid/inference.py')
-rw-r--r-- | astroid/inference.py | 237 |
1 files changed, 143 insertions, 94 deletions
diff --git a/astroid/inference.py b/astroid/inference.py index b5403f5b..62c06e65 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -49,6 +49,8 @@ def infer_end(self, context=None): """ yield self + + nodes.Module._infer = infer_end nodes.ClassDef._infer = infer_end nodes.FunctionDef._infer = infer_end @@ -65,11 +67,9 @@ def _infer_sequence_helper(node, context=None): if isinstance(elt, nodes.Starred): starred = helpers.safe_infer(elt.value, context) if starred in (None, util.Uninferable): - raise exceptions.InferenceError(node=node, - context=context) - if not hasattr(starred, 'elts'): - raise exceptions.InferenceError(node=node, - context=context) + raise exceptions.InferenceError(node=node, context=context) + if not hasattr(starred, "elts"): + raise exceptions.InferenceError(node=node, context=context) values.extend(_infer_sequence_helper(starred)) else: values.append(elt) @@ -83,7 +83,9 @@ def infer_sequence(self, context=None): yield self else: values = _infer_sequence_helper(self, context) - new_seq = type(self)(lineno=self.lineno, col_offset=self.col_offset, parent=self.parent) + new_seq = type(self)( + lineno=self.lineno, col_offset=self.col_offset, parent=self.parent + ) new_seq.postinit(values) yield new_seq @@ -136,16 +138,14 @@ def _infer_map(node, context): if double_starred in (None, util.Uninferable): raise exceptions.InferenceError if not isinstance(double_starred, nodes.Dict): - raise exceptions.InferenceError(node=node, - context=context) + raise exceptions.InferenceError(node=node, context=context) unpack_items = _infer_map(double_starred, context) values = _update_with_replacement(values, unpack_items) else: key = helpers.safe_infer(name, context=context) value = helpers.safe_infer(value, context=context) if any(elem in (None, util.Uninferable) for elem in (key, value)): - raise exceptions.InferenceError(node=node, - context=context) + raise exceptions.InferenceError(node=node, context=context) values = _update_with_replacement(values, {key: value}) return values @@ -172,6 +172,7 @@ def _higher_function_scope(node): return current.parent return None + def infer_name(self, context=None): """infer a Name: use name lookup rules""" frame, stmts = self.lookup(self.name) @@ -183,18 +184,19 @@ def infer_name(self, context=None): _, stmts = parent_function.lookup(self.name) if not stmts: - raise exceptions.NameInferenceError(name=self.name, - scope=self.scope(), - context=context) + raise exceptions.NameInferenceError( + name=self.name, scope=self.scope(), context=context + ) context = context.clone() context.lookupname = self.name return bases._infer_stmts(stmts, context, frame) + # pylint: disable=no-value-for-parameter nodes.Name._infer = decorators.raise_if_nothing_inferred( decorators.path_wrapper(infer_name) ) -nodes.AssignName.infer_lhs = infer_name # won't work with a path wrapper +nodes.AssignName.infer_lhs = infer_name # won't work with a path wrapper @decorators.raise_if_nothing_inferred @@ -202,8 +204,9 @@ nodes.AssignName.infer_lhs = infer_name # won't work with a path wrapper def infer_call(self, context=None): """infer a Call node by trying to guess what the function returns""" callcontext = context.clone() - callcontext.callcontext = contextmod.CallContext(args=self.args, - keywords=self.keywords) + callcontext.callcontext = contextmod.CallContext( + args=self.args, keywords=self.keywords + ) callcontext.boundnode = None extra_context = {} if context is not None: @@ -214,14 +217,13 @@ def infer_call(self, context=None): yield callee continue try: - if hasattr(callee, 'infer_call_result'): - yield from callee.infer_call_result( - caller=self, - context=callcontext, - ) + if hasattr(callee, "infer_call_result"): + yield from callee.infer_call_result(caller=self, context=callcontext) except exceptions.InferenceError: continue return dict(node=self, context=context) + + nodes.Call._infer = infer_call @@ -239,10 +241,7 @@ def infer_import(self, context=None, asname=True): else: yield self.do_import_module(name) except exceptions.AstroidBuildingError as exc: - raise exceptions.InferenceError( - node=self, - context=context, - ) from exc + raise exceptions.InferenceError(node=self, context=context) from exc nodes.Import._infer = infer_import @@ -252,6 +251,8 @@ def infer_name_module(self, name): context = contextmod.InferenceContext() context.lookupname = name return self.infer(context, asname=False) + + nodes.Import.infer_name_module = infer_name_module @@ -268,10 +269,7 @@ def infer_import_from(self, context=None, asname=True): try: module = self.do_import_module() except exceptions.AstroidBuildingError as exc: - raise exceptions.InferenceError( - node=self, - context=context, - ) from exc + raise exceptions.InferenceError(node=self, context=context) from exc try: context = contextmod.copy_context(context) @@ -280,11 +278,10 @@ def infer_import_from(self, context=None, asname=True): return bases._infer_stmts(stmts, context) except exceptions.AttributeInferenceError as error: raise exceptions.InferenceError( - error.message, - target=self, - attribute=name, - context=context, + error.message, target=self, attribute=name, context=context ) from error + + nodes.ImportFrom._infer = infer_import_from @@ -300,11 +297,14 @@ def infer_attribute(self, context=None): # This handles the situation where the attribute is accessed through a subclass # of a base class and the attribute is defined at the base class's level, # by taking in consideration a redefinition in the subclass. - if (isinstance(owner, bases.Instance) - and isinstance(context.boundnode, bases.Instance)): + if isinstance(owner, bases.Instance) and isinstance( + context.boundnode, bases.Instance + ): try: - if helpers.is_subtype(helpers.object_type(context.boundnode), - helpers.object_type(owner)): + if helpers.is_subtype( + helpers.object_type(context.boundnode), + helpers.object_type(owner), + ): owner = context.boundnode except exceptions._NonDeducibleTypeHierarchy: # Can't determine anything useful. @@ -320,8 +320,10 @@ def infer_attribute(self, context=None): # XXX method / function context.boundnode = None return dict(node=self, context=context) + + nodes.Attribute._infer = decorators.path_wrapper(infer_attribute) -nodes.AssignAttr.infer_lhs = infer_attribute # # won't work with a path wrapper +nodes.AssignAttr.infer_lhs = infer_attribute # # won't work with a path wrapper @decorators.raise_if_nothing_inferred @@ -330,15 +332,13 @@ def infer_global(self, context=None): if context.lookupname is None: raise exceptions.InferenceError(node=self, context=context) try: - return bases._infer_stmts(self.root().getattr(context.lookupname), - context) + return bases._infer_stmts(self.root().getattr(context.lookupname), context) except exceptions.AttributeInferenceError as error: raise exceptions.InferenceError( - error.message, - target=self, - attribute=context.lookupname, - context=context, + error.message, target=self, attribute=context.lookupname, context=context ) from error + + nodes.Global._infer = infer_global @@ -387,10 +387,12 @@ def infer_subscript(self, context=None): try: assigned = value.getitem(index_value, context) - except (exceptions.AstroidTypeError, - exceptions.AstroidIndexError, - exceptions.AttributeInferenceError, - AttributeError) as exc: + except ( + exceptions.AstroidTypeError, + exceptions.AstroidIndexError, + exceptions.AttributeInferenceError, + AttributeError, + ) as exc: raise exceptions.InferenceError(node=self, context=context) from exc # Prevent inferring if the inferred subscript @@ -402,6 +404,7 @@ def infer_subscript(self, context=None): return dict(node=self, context=context) + nodes.Subscript._infer = decorators.path_wrapper(infer_subscript) nodes.Subscript.infer_lhs = infer_subscript @@ -416,7 +419,7 @@ def _infer_boolop(self, context=None): node. """ values = self.values - if self.op == 'or': + if self.op == "or": predicate = operator.truth else: predicate = operator.not_ @@ -458,11 +461,13 @@ def _infer_boolop(self, context=None): return dict(node=self, context=context) + nodes.BoolOp._infer = _infer_boolop # UnaryOp, BinOp and AugAssign inferences + def _filter_operation_errors(self, infer_callable, context, error): for result in infer_callable(self, context): if isinstance(result, error): @@ -532,10 +537,12 @@ def _infer_unaryop(self, context=None): @decorators.path_wrapper def infer_unaryop(self, context=None): """Infer what an UnaryOp should return when evaluated.""" - yield from _filter_operation_errors(self, _infer_unaryop, context, - util.BadUnaryOperationMessage) + yield from _filter_operation_errors( + self, _infer_unaryop, context, util.BadUnaryOperationMessage + ) return dict(node=self, context=context) + nodes.UnaryOp._infer_unaryop = _infer_unaryop nodes.UnaryOp._infer = infer_unaryop @@ -559,11 +566,15 @@ def _invoke_binop_inference(instance, opnode, op, other, context, method_name): def _aug_op(instance, opnode, op, other, context, reverse=False): """Get an inference callable for an augmented binary operation.""" method_name = protocols.AUGMENTED_OP_METHOD[op] - return functools.partial(_invoke_binop_inference, - instance=instance, - op=op, opnode=opnode, other=other, - context=context, - method_name=method_name) + return functools.partial( + _invoke_binop_inference, + instance=instance, + op=op, + opnode=opnode, + other=other, + context=context, + method_name=method_name, + ) def _bin_op(instance, opnode, op, other, context, reverse=False): @@ -575,11 +586,15 @@ def _bin_op(instance, opnode, op, other, context, reverse=False): method_name = protocols.REFLECTED_BIN_OP_METHOD[op] else: method_name = protocols.BIN_OP_METHOD[op] - return functools.partial(_invoke_binop_inference, - instance=instance, - op=op, opnode=opnode, other=other, - context=context, - method_name=method_name) + return functools.partial( + _invoke_binop_inference, + instance=instance, + op=op, + opnode=opnode, + other=other, + context=context, + method_name=method_name, + ) def _get_binop_contexts(context, left, right): @@ -603,8 +618,9 @@ def _same_type(type1, type2): return type1.qname() == type2.qname() -def _get_binop_flow(left, left_type, binary_opnode, right, right_type, - context, reverse_context): +def _get_binop_flow( + left, left_type, binary_opnode, right, right_type, context, reverse_context +): """Get the flow for binary operations. The rules are a bit messy: @@ -625,16 +641,21 @@ def _get_binop_flow(left, left_type, binary_opnode, right, right_type, elif helpers.is_subtype(left_type, right_type): methods = [_bin_op(left, binary_opnode, op, right, context)] elif helpers.is_supertype(left_type, right_type): - methods = [_bin_op(right, binary_opnode, op, left, reverse_context, reverse=True), - _bin_op(left, binary_opnode, op, right, context)] + methods = [ + _bin_op(right, binary_opnode, op, left, reverse_context, reverse=True), + _bin_op(left, binary_opnode, op, right, context), + ] else: - methods = [_bin_op(left, binary_opnode, op, right, context), - _bin_op(right, binary_opnode, op, left, reverse_context, reverse=True)] + methods = [ + _bin_op(left, binary_opnode, op, right, context), + _bin_op(right, binary_opnode, op, left, reverse_context, reverse=True), + ] return methods -def _get_aug_flow(left, left_type, aug_opnode, right, right_type, - context, reverse_context): +def _get_aug_flow( + left, left_type, aug_opnode, right, right_type, context, reverse_context +): """Get the flow for augmented binary operations. The rules are a bit messy: @@ -653,19 +674,27 @@ def _get_aug_flow(left, left_type, aug_opnode, right, right_type, bin_op = aug_opnode.op.strip("=") aug_op = aug_opnode.op if _same_type(left_type, right_type): - methods = [_aug_op(left, aug_opnode, aug_op, right, context), - _bin_op(left, aug_opnode, bin_op, right, context)] + methods = [ + _aug_op(left, aug_opnode, aug_op, right, context), + _bin_op(left, aug_opnode, bin_op, right, context), + ] elif helpers.is_subtype(left_type, right_type): - methods = [_aug_op(left, aug_opnode, aug_op, right, context), - _bin_op(left, aug_opnode, bin_op, right, context)] + methods = [ + _aug_op(left, aug_opnode, aug_op, right, context), + _bin_op(left, aug_opnode, bin_op, right, context), + ] elif helpers.is_supertype(left_type, right_type): - methods = [_aug_op(left, aug_opnode, aug_op, right, context), - _bin_op(right, aug_opnode, bin_op, left, reverse_context, reverse=True), - _bin_op(left, aug_opnode, bin_op, right, context)] + methods = [ + _aug_op(left, aug_opnode, aug_op, right, context), + _bin_op(right, aug_opnode, bin_op, left, reverse_context, reverse=True), + _bin_op(left, aug_opnode, bin_op, right, context), + ] else: - methods = [_aug_op(left, aug_opnode, aug_op, right, context), - _bin_op(left, aug_opnode, bin_op, right, context), - _bin_op(right, aug_opnode, bin_op, left, reverse_context, reverse=True)] + methods = [ + _aug_op(left, aug_opnode, aug_op, right, context), + _bin_op(left, aug_opnode, bin_op, right, context), + _bin_op(right, aug_opnode, bin_op, left, reverse_context, reverse=True), + ] return methods @@ -679,8 +708,9 @@ def _infer_binary_operation(left, right, binary_opnode, context, flow_factory): context, reverse_context = _get_binop_contexts(context, left, right) left_type = helpers.object_type(left) right_type = helpers.object_type(right) - methods = flow_factory(left, left_type, binary_opnode, right, right_type, - context, reverse_context) + methods = flow_factory( + left, left_type, binary_opnode, right, right_type, context, reverse_context + ) for method in methods: try: results = list(method()) @@ -698,8 +728,9 @@ def _infer_binary_operation(left, right, binary_opnode, context, flow_factory): if all(map(_is_not_implemented, results)): continue - not_implemented = sum(1 for result in results - if _is_not_implemented(result)) + not_implemented = sum( + 1 for result in results if _is_not_implemented(result) + ) if not_implemented and not_implemented != len(results): # Can't infer yet what this is. yield util.Uninferable @@ -739,7 +770,8 @@ def _infer_binop(self, context): try: yield from _infer_binary_operation( - lhs, rhs, self, context, _get_binop_flow) + lhs, rhs, self, context, _get_binop_flow + ) except exceptions._NonDeducibleTypeHierarchy: yield util.Uninferable @@ -747,8 +779,10 @@ def _infer_binop(self, context): @decorators.yes_if_nothing_inferred @decorators.path_wrapper def infer_binop(self, context=None): - return _filter_operation_errors(self, _infer_binop, context, - util.BadBinaryOperationMessage) + return _filter_operation_errors( + self, _infer_binop, context, util.BadBinaryOperationMessage + ) + nodes.BinOp._infer_binop = _infer_binop nodes.BinOp._infer = infer_binop @@ -773,7 +807,9 @@ def _infer_augassign(self, context=None): return try: - yield from _infer_binary_operation(lhs, rhs, self, context, _get_aug_flow) + yield from _infer_binary_operation( + lhs, rhs, self, context, _get_aug_flow + ) except exceptions._NonDeducibleTypeHierarchy: yield util.Uninferable @@ -781,8 +817,10 @@ def _infer_augassign(self, context=None): @decorators.raise_if_nothing_inferred @decorators.path_wrapper def infer_augassign(self, context=None): - return _filter_operation_errors(self, _infer_augassign, context, - util.BadBinaryOperationMessage) + return _filter_operation_errors( + self, _infer_augassign, context, util.BadBinaryOperationMessage + ) + nodes.AugAssign._infer_augassign = _infer_augassign nodes.AugAssign._infer = infer_augassign @@ -797,6 +835,8 @@ def infer_arguments(self, context=None): if name is None: raise exceptions.InferenceError(node=self, context=context) return protocols._arguments_infer_argname(self, name, context) + + nodes.Arguments._infer = infer_arguments @@ -812,12 +852,15 @@ def infer_assign(self, context=None): stmts = list(self.assigned_stmts(context=context)) return bases._infer_stmts(stmts, context) + + nodes.AssignName._infer = infer_assign nodes.AssignAttr._infer = infer_assign # no infer method on DelName and DelAttr (expected InferenceError) + @decorators.raise_if_nothing_inferred @decorators.path_wrapper def infer_empty_node(self, context=None): @@ -828,6 +871,8 @@ def infer_empty_node(self, context=None): yield from MANAGER.infer_ast_from_something(self.object, context=context) except exceptions.AstroidError: yield util.Uninferable + + nodes.EmptyNode._infer = infer_empty_node @@ -835,6 +880,8 @@ nodes.EmptyNode._infer = infer_empty_node @decorators.path_wrapper def infer_index(self, context=None): return self.value.infer(context) + + nodes.Index._infer = infer_index # TODO: move directly into bases.Instance when the dependency hell @@ -848,20 +895,22 @@ def instance_getitem(self, index, context=None): # Create a new callcontext for providing index as an argument. new_context.callcontext = contextmod.CallContext(args=[index]) - method = next(self.igetattr('__getitem__', context=context), None) + method = next(self.igetattr("__getitem__", context=context), None) if not isinstance(method, bases.BoundMethod): raise exceptions.InferenceError( - 'Could not find __getitem__ for {node!r}.', - node=self, context=context) + "Could not find __getitem__ for {node!r}.", node=self, context=context + ) try: return next(method.infer_call_result(self, new_context)) except StopIteration as exc: raise exceptions.InferenceError( - message='Inference for {node!r}[{index!s}] failed.', + message="Inference for {node!r}[{index!s}] failed.", node=self, index=index, - context=context) from exc + context=context, + ) from exc + bases.Instance.getitem = instance_getitem |