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 regression with import resolver #1211

Merged
merged 6 commits into from
Oct 17, 2021
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
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Release date: TBA
Closes PyCQA/pylint#2462
Closes #1208

* Fix regression with the import resolver

Closes PyCQA/pylint#5131

* Fix crash with invalid dataclass field call

Closes PyCQA/pylint#5153
Expand Down
13 changes: 7 additions & 6 deletions astroid/nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,19 +708,16 @@ def import_module(self, modname, relative_only=False, level=None):
raise
return AstroidManager().ast_from_module_name(modname)

def relative_to_absolute_name(self, modname, level):
def relative_to_absolute_name(self, modname: str, level: int) -> str:
"""Get the absolute module name for a relative import.

The relative import can be implicit or explicit.

:param modname: The module name to convert.
:type modname: str

:param level: The level of relative import.
:type level: int

:returns: The absolute module name.
:rtype: str

:raises TooManyLevelsError: When the relative import refers to a
module too far above this one.
Expand All @@ -734,8 +731,12 @@ def relative_to_absolute_name(self, modname, level):
if self.package:
level = level - 1
package_name = self.name.rsplit(".", level)[0]
elif not os.path.exists("__init__.py") and os.path.exists(
modname.split(".")[0]
elif (
self.path
and not os.path.exists(os.path.dirname(self.path[0]) + "/__init__.py")
and os.path.exists(
os.path.dirname(self.path[0]) + "/" + modname.split(".")[0]
)
):
level = level - 1
package_name = ""
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ...double_name import function
8 changes: 8 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -6547,5 +6547,13 @@ def test_namespace_package_same_name() -> None:
resources.build_file("data/beyond_top_level_two/a.py")


def test_relative_imports_init_package() -> None:
"""check that relative imports within a package that uses __init__.py
still works"""
resources.build_file(
"data/beyond_top_level_three/module/sub_module/sub_sub_module/main.py"
)


if __name__ == "__main__":
unittest.main()