Skip to content

Commit 847f7be

Browse files
More tests for imported modules (#1399)
Co-authored-by: Jacob Walls <[email protected]>
1 parent 0bf711f commit 847f7be

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/unittest_inference.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6627,6 +6627,42 @@ def test_inference_of_items_on_module_dict() -> None:
66276627
builder.file_build(str(DATA_DIR / "module_dict_items_call" / "test.py"), "models")
66286628

66296629

6630+
class ImportedModuleTests(resources.AstroidCacheSetupMixin):
6631+
def test_imported_module_var_inferable(self) -> None:
6632+
"""
6633+
Module variables can be imported and inferred successfully as part of binary operators.
6634+
"""
6635+
mod1 = parse(("from top.mod import v as z\n" "w = [1] + z"), module_name="top")
6636+
parse("v = [2]", module_name="top.mod")
6637+
w_val = mod1.body[-1].value
6638+
i_w_val = next(w_val.infer())
6639+
assert i_w_val != util.Uninferable
6640+
assert i_w_val.as_string() == "[1, 2]"
6641+
6642+
def test_imported_module_var_inferable2(self) -> None:
6643+
"""Version list of strings."""
6644+
mod1 = parse(
6645+
("from top.mod import v as z\n" "w = ['1'] + z"), module_name="top"
6646+
)
6647+
parse("v = ['2']", module_name="top.mod")
6648+
w_val = mod1.body[-1].value
6649+
i_w_val = next(w_val.infer())
6650+
assert i_w_val != util.Uninferable
6651+
assert i_w_val.as_string() == "['1', '2']"
6652+
6653+
def test_imported_module_var_inferable3(self) -> None:
6654+
"""Version list of strings with a __dunder__ name."""
6655+
mod1 = parse(
6656+
("from top.mod import __dunder_var__ as v\n" "__dunder_var__ = ['w'] + v"),
6657+
module_name="top",
6658+
)
6659+
parse("__dunder_var__ = ['v']", module_name="top.mod")
6660+
w_val = mod1.body[-1].value
6661+
i_w_val = next(w_val.infer())
6662+
assert i_w_val != util.Uninferable
6663+
assert i_w_val.as_string() == "['w', 'v']"
6664+
6665+
66306666
def test_recursion_on_inference_tip() -> None:
66316667
"""Regression test for recursion in inference tip.
66326668

0 commit comments

Comments
 (0)