summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--astroid/inference.py2
-rw-r--r--tests/testdata/python3/data/module_dict_items_call/models.py5
-rw-r--r--tests/testdata/python3/data/module_dict_items_call/test.py7
-rw-r--r--tests/unittest_inference.py13
5 files changed, 30 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index e2d68654..66606218 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -31,6 +31,11 @@ Release date: TBA
Closes #1282
Ref #1103
+* Fixed crash when trying to infer ``items()`` on the ``__dict__``
+ attribute of an imported module.
+
+ Closes #1085
+
What's New in astroid 2.9.4?
============================
Release date: TBA
diff --git a/astroid/inference.py b/astroid/inference.py
index 74f04dd9..f1b50a87 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -317,6 +317,8 @@ def infer_attribute(self, context=None):
if not context:
context = InferenceContext()
+ else:
+ context = copy_context(context)
old_boundnode = context.boundnode
try:
diff --git a/tests/testdata/python3/data/module_dict_items_call/models.py b/tests/testdata/python3/data/module_dict_items_call/models.py
new file mode 100644
index 00000000..212bc011
--- /dev/null
+++ b/tests/testdata/python3/data/module_dict_items_call/models.py
@@ -0,0 +1,5 @@
+import re
+
+
+class MyModel:
+ class_attribute = 1
diff --git a/tests/testdata/python3/data/module_dict_items_call/test.py b/tests/testdata/python3/data/module_dict_items_call/test.py
new file mode 100644
index 00000000..4a52b18e
--- /dev/null
+++ b/tests/testdata/python3/data/module_dict_items_call/test.py
@@ -0,0 +1,7 @@
+import models
+
+
+def func():
+ for _, value in models.__dict__.items():
+ if isinstance(value, type):
+ value.class_attribute += 1
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index 0133c787..d2de2b5b 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -48,6 +48,7 @@ import textwrap
import unittest
from abc import ABCMeta
from functools import partial
+from pathlib import Path
from typing import Any, Callable, Dict, List, Tuple, Union
from unittest.mock import patch
@@ -88,6 +89,7 @@ builder = AstroidBuilder()
EXC_MODULE = "builtins"
BOOL_SPECIAL_METHOD = "__bool__"
+DATA_DIR = Path(__file__).parent / "testdata" / "python3" / "data"
class InferenceUtilsTest(unittest.TestCase):
@@ -1732,8 +1734,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
"""
ast = extract_node(code, __name__)
expr = ast.func.expr
- with pytest.raises(InferenceError):
- next(expr.infer())
+ self.assertIs(next(expr.infer()), util.Uninferable)
def test_tuple_builtin_inference(self) -> None:
code = """
@@ -6584,5 +6585,13 @@ def test_relative_imports_init_package() -> None:
)
+def test_inference_of_items_on_module_dict() -> None:
+ """Crash test for the inference of items() on a module's dict attribute.
+
+ Originally reported in https://github.com/PyCQA/astroid/issues/1085
+ """
+ builder.file_build(str(DATA_DIR / "module_dict_items_call" / "test.py"), "models")
+
+
if __name__ == "__main__":
unittest.main()