diff options
author | HoverHell <hoverhell@gmail.com> | 2018-04-24 13:10:26 +0300 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-04-24 12:10:26 +0200 |
commit | ceeee09743079d07ffc3918969ab203f766b29e0 (patch) | |
tree | 1276a95208f7294b5c2eafadfb148ad39ccc2293 /astroid/inference.py | |
parent | 8575ac1c0bd247bc314f6752355d1ea647dec911 (diff) | |
download | astroid-git-ceeee09743079d07ffc3918969ab203f766b29e0.tar.gz |
Fix StopIteration raising for python3.7 (#534)
Because we don't support Python 2 any longer in the master branch, we can
return values from generators to signal that we want to throw a StopIteration,
without actually raising the StopIteration itself.
Diffstat (limited to 'astroid/inference.py')
-rw-r--r-- | astroid/inference.py | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/astroid/inference.py b/astroid/inference.py index a0a933eb..5467736e 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -202,7 +202,7 @@ def infer_call(self, context=None): continue # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. - raise StopIteration(dict(node=self, context=context)) + return dict(node=self, context=context) nodes.Call._infer = infer_call @@ -292,7 +292,7 @@ def infer_attribute(self, context=None): context.boundnode = None # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. - raise StopIteration(dict(node=self, context=context)) + 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 @@ -324,12 +324,18 @@ def infer_subscript(self, context=None): handle each supported index type accordingly. """ - value = next(self.value.infer(context)) + try: + value = next(self.value.infer(context)) + except StopIteration: + return if value is util.Uninferable: yield util.Uninferable return - index = next(self.slice.infer(context)) + try: + index = next(self.slice.infer(context)) + except StopIteration: + return if index is util.Uninferable: yield util.Uninferable return @@ -367,7 +373,7 @@ def infer_subscript(self, context=None): # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. - raise StopIteration(dict(node=self, context=context)) + return dict(node=self, context=context) nodes.Subscript._infer = decorators.path_wrapper(infer_subscript) nodes.Subscript.infer_lhs = infer_subscript @@ -425,7 +431,7 @@ def _infer_boolop(self, context=None): # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. - raise StopIteration(dict(node=self, context=context)) + return dict(node=self, context=context) nodes.BoolOp._infer = _infer_boolop @@ -506,7 +512,7 @@ def infer_unaryop(self, context=None): yield inferred # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. - raise StopIteration(dict(node=self, context=context)) + return dict(node=self, context=context) nodes.UnaryOp._infer_unaryop = _infer_unaryop nodes.UnaryOp._infer = infer_unaryop @@ -822,7 +828,7 @@ def instance_getitem(self, index, context=None): new_context.callcontext = contextmod.CallContext(args=[index]) new_context.boundnode = self - method = next(self.igetattr('__getitem__', context=context)) + method = next(self.igetattr('__getitem__', context=context), None) if not isinstance(method, bases.BoundMethod): raise exceptions.InferenceError( 'Could not find __getitem__ for {node!r}.', |