-
-
Notifications
You must be signed in to change notification settings - Fork 280
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
Don't rely on the module cache when "importing self" #1747
Conversation
b8637f0
to
e5e09c3
Compare
|
||
# Infer the 'import math' statement | ||
stdlib_math = next(module.body[1].value.args[0].infer()) | ||
assert self.manager.astroid_cache["math"] != stdlib_math |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the old math
stays in place in the cache. This is how we handle all cache clashes so I think this is also good in this case?
Pull Request Test Coverage Report for Build 2911196102
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had some time to do a quick first pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice MR, I like the typing update too :)
tests/resources.py
Outdated
@@ -20,6 +20,10 @@ def find(name: str) -> str: | |||
return os.path.normpath(os.path.join(os.path.dirname(__file__), DATA_DIR, name)) | |||
|
|||
|
|||
def find_in_testdata(name: str) -> Path: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def find_in_testdata(name: str) -> Path: | |
def construct_absolute_path_in_testdata(name: str) -> Path: |
For me the verb "find" imply searching for it by walking the directory tree. I would expect the give "foo.py" and get back the file in testdata/bar/
called foo.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to use get_testdata_path
. The other one is a bit long.
tests/unittest_manager.py
Outdated
data_dir = resources.find_in_testdata("import_conflicting_names") | ||
math_file = data_dir / "math.py" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data_dir = resources.find_in_testdata("import_conflicting_names") | |
math_file = data_dir / "math.py" | |
math_file = resources.construct_absolute_path_in_testdata("import_conflicting_names/math.py") |
I think this would work ?
Ready for review again! @Pierre-Sassoulas @jacobtylerwalls 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test passes for me on main
. Would you have the opportunity to create a test that fails before the fix?
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Are you sure? diff --git a/tests/unittest_manager.py b/tests/unittest_manager.py
index 9cf02aff8..fe1f22642 100644
--- a/tests/unittest_manager.py
+++ b/tests/unittest_manager.py
@@ -325,6 +325,22 @@ class AstroidManagerTest(
with self.assertRaises(AstroidBuildingError):
self.manager.ast_from_module_name("foo.bar.baz")
+ def test_same_name_import_module(self) -> None:
+ """Test inference of an import statement with the same name as the module.
+ See https://github.com/PyCQA/pylint/issues/5151.
+ """
+ math_file = resources.find("data/import_conflicting_names/math.py")
+ module = self.manager.ast_from_file(math_file)
+
+ # Change the cache key and module name to mimic importing the test file
+ # from the root/top level. This creates a clash between math.py and stdlib math.
+ self.manager.astroid_cache["math"] = self.manager.astroid_cache.pop(module.name)
+ module.name = "math"
+
+ # Infer the 'import math' statement
+ stdlib_math = next(module.body[1].value.args[0].infer())
+ assert self.manager.astroid_cache["math"] != stdlib_math
+
class BorgAstroidManagerTC(unittest.TestCase):
def test_borg(self) -> None:
together with the Fails for me with > assert self.manager.astroid_cache["math"] != stdlib_math
E assert <Module.math l.0 at 0x110670210> != <Module.math l.0 at 0x110670210> |
Oh never mind. That doesn't actually fail. You just can't Edit: @jacobtylerwalls I should have taken my coffee. That assertion fails correctly. On |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I did something horribly wrong when testing it, I guess. Sorry!
EDIT: |
Steps
Description
Relevant historic commits are:
6d59ad0
9684d0f
Basically, by not relying on the cache we trigger a new import system and check and rightfully end up with the
stdlib
math
module instead of the current module. I tested withpylint
and test suite there still passes.Refs: pylint-dev/pylint#7289 and pylint-dev/pylint#5151, but mostly the latter.
Type of Changes