Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong exception raised in infer_import_from #1110

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
============================
Expand Down
7 changes: 5 additions & 2 deletions astroid/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down