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 two incorrect/broken tests in tests/checkers/unittest_imports.py #9911

Merged
merged 4 commits into from
Sep 20, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions tests/checkers/unittest_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from pylint.checkers import imports
from pylint.interfaces import UNDEFINED
from pylint.lint import augmented_sys_path, discover_package_path
from pylint.testutils import CheckerTestCase, MessageTest
from pylint.testutils._run import _Run as Run

Expand Down Expand Up @@ -92,28 +93,33 @@ def test_relative_beyond_top_level_four(capsys: CaptureFixture[str]) -> None:
assert errors == ""

def test_wildcard_import_init(self) -> None:
module = astroid.MANAGER.ast_from_module_name("init_wildcard", REGR_DATA)
import_from = module.body[0]
context_file = os.path.join(REGR_DATA, "dummy_wildcard.py")
Copy link
Member

Choose a reason for hiding this comment

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

Why is the context file necessary and doesn't it change what we're testing ?

Copy link
Contributor Author

@akamat10 akamat10 Sep 4, 2024

Choose a reason for hiding this comment

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

Context file would be used to specify that the file where the import statement for the module. I was trying to understand the intent of the code and the test and trying to guess what we are trying to achieve. The existing test only works if the search directory is in the sys.path. If we want this test to succeed without changing sys.path, then I think we need to change (fix?) the find_spec in astroid.interpreter._import.spec module. It is not considering the directory we are passing to it. Shall I attempt to push a fix and see if it works?

I also looked at the tests in test_manager.py in the astroid repo. There is no test for ast_from_module when the second argument is specified.


with self.assertNoMessages():
self.checker.visit_importfrom(import_from)
with augmented_sys_path([discover_package_path(context_file, [])]):
module = astroid.MANAGER.ast_from_module_name("init_wildcard", context_file)
import_from = module.body[0]

with self.assertNoMessages():
self.checker.visit_importfrom(import_from)

def test_wildcard_import_non_init(self) -> None:
module = astroid.MANAGER.ast_from_module_name("wildcard", REGR_DATA)
import_from = module.body[0]
context_file = os.path.join(REGR_DATA, "dummy_wildcard.py")

msg = MessageTest(
msg_id="wildcard-import",
node=import_from,
args="empty",
confidence=UNDEFINED,
line=1,
col_offset=0,
end_line=1,
end_col_offset=19,
)
with self.assertAddsMessages(msg):
self.checker.visit_importfrom(import_from)
with augmented_sys_path([discover_package_path(context_file, [])]):
module = astroid.MANAGER.ast_from_module_name("wildcard", context_file)
import_from = module.body[0]
msg = MessageTest(
msg_id="wildcard-import",
node=import_from,
args="empty",
confidence=UNDEFINED,
line=1,
col_offset=0,
end_line=1,
end_col_offset=19,
)
with self.assertAddsMessages(msg):
self.checker.visit_importfrom(import_from)

@staticmethod
def test_preferred_module(capsys: CaptureFixture[str]) -> None:
Expand Down
Empty file.
Loading