Skip to content

More tests for imported modules #1399

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

Merged
merged 11 commits into from
May 11, 2022
Merged
36 changes: 36 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -6627,6 +6627,42 @@ def test_inference_of_items_on_module_dict() -> None:
builder.file_build(str(DATA_DIR / "module_dict_items_call" / "test.py"), "models")


class ImportedModuleTests(resources.AstroidCacheSetupMixin):
def test_imported_module_var_inferable(self) -> None:
"""
Module variables can be imported and inferred successfully as part of binary operators.
"""
mod1 = parse(("from top.mod import v as z\n" "w = [1] + z"), module_name="top")
parse("v = [2]", module_name="top.mod")
w_val = mod1.body[-1].value
i_w_val = next(w_val.infer())
assert i_w_val != util.Uninferable
assert i_w_val.as_string() == "[1, 2]"

def test_imported_module_var_inferable2(self) -> None:
"""Version list of strings."""
mod1 = parse(
("from top.mod import v as z\n" "w = ['1'] + z"), module_name="top"
)
parse("v = ['2']", module_name="top.mod")
w_val = mod1.body[-1].value
i_w_val = next(w_val.infer())
assert i_w_val != util.Uninferable
assert i_w_val.as_string() == "['1', '2']"

def test_imported_module_var_inferable3(self) -> None:
"""Version list of strings with a __dunder__ name."""
mod1 = parse(
("from top.mod import __dunder_var__ as v\n" "__dunder_var__ = ['w'] + v"),
module_name="top",
)
parse("__dunder_var__ = ['v']", module_name="top.mod")
w_val = mod1.body[-1].value
i_w_val = next(w_val.infer())
assert i_w_val != util.Uninferable
assert i_w_val.as_string() == "['w', 'v']"


def test_recursion_on_inference_tip() -> None:
"""Regression test for recursion in inference tip.

Expand Down