Skip to content

Fix ImportedModuleTests #1552

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 1 commit into from
May 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 54 additions & 32 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -6628,38 +6628,60 @@ 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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, you don't need to remove test classes. Even with pytest, they can be helpful to group test cases. I believe the only important things to remember are the classname must begin with Test... and you can use @staticmethod if you don't need self.

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\nw = [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\nw = ['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_imported_module_var_inferable() -> None:
"""
Module variables can be imported and inferred successfully as part of binary operators.
"""
mod1 = parse(
textwrap.dedent(
"""
from top1.mod import v as z
w = [1] + z
"""
),
module_name="top1",
)
parse("v = [2]", module_name="top1.mod")
w_val = mod1.body[-1].value
i_w_val = next(w_val.infer())
assert i_w_val is not util.Uninferable
assert i_w_val.as_string() == "[1, 2]"


def test_imported_module_var_inferable2() -> None:
"""Version list of strings."""
mod2 = parse(
textwrap.dedent(
"""
from top2.mod import v as z
w = ['1'] + z
"""
),
module_name="top2",
)
parse("v = ['2']", module_name="top2.mod")
w_val = mod2.body[-1].value
i_w_val = next(w_val.infer())
assert i_w_val is not util.Uninferable
assert i_w_val.as_string() == "['1', '2']"


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


def test_recursion_on_inference_tip() -> None:
Expand Down