diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-07-20 22:08:53 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-07-21 08:44:57 +0200 |
commit | ff6f9e791e0d72b1dba610365e03b848952beea3 (patch) | |
tree | 954684288ef43e4f5960db8725eda5d6c1951ecb | |
parent | 51df6eef85c5e20e2f9414b83a98528f913e224b (diff) | |
download | astroid-git-ff6f9e791e0d72b1dba610365e03b848952beea3.tar.gz |
Fix wrong exception raised in infer_import_from
Fix #4692
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | astroid/inference.py | 7 | ||||
-rw-r--r-- | tests/unittest_inference.py | 14 |
3 files changed, 24 insertions, 2 deletions
@@ -16,6 +16,11 @@ Release date: TBA Closes PyCQA/pylint#4439 +* Fix a crash when a AttributeInferenceError was raised when + failing to find the real name in infer_import_from. + + Closes PyCQA/pylint#4692 + What's New in astroid 2.6.4? ============================ diff --git a/astroid/inference.py b/astroid/inference.py index 44145876..994e2aae 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -273,8 +273,11 @@ def infer_import_from(self, context=None, asname=True): if name is None: raise InferenceError(node=self, context=context) if asname: - name = self.real_name(name) - + try: + name = self.real_name(name) + except AttributeInferenceError as exc: + # See https://github.com/PyCQA/pylint/issues/4692 + raise InferenceError(node=self, context=context) from exc try: module = self.do_import_module() except AstroidBuildingError as exc: diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index a84ee3cf..591696f9 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -6155,6 +6155,20 @@ def test_issue926_binop_referencing_same_name_is_not_uninferable(): assert inferred[0].value == 3 +def test_pylint_issue_4692_attribute_inference_error_in_infer_import_from(): + """https://github.com/PyCQA/pylint/issues/4692""" + code = """ +import click + + +for name, item in click.__dict__.items(): + _ = isinstance(item, click.Command) and item != 'foo' + """ + node = extract_node(code) + with pytest.raises(InferenceError): + list(node.infer()) + + def test_issue_1090_infer_yield_type_base_class(): code = """ import contextlib |