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 relative-beyond-top-level false positive #1186

Merged
merged 5 commits into from
Oct 5, 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 @@ -32,6 +32,10 @@ Release date: TBA

* Improve brain for ``typing.Callable`` and ``typing.Type``.

* Fix bug with importing namespace packages with relative imports

Closes PyCQA/pylint#5059


What's New in astroid 2.8.0?
============================
Expand Down
12 changes: 10 additions & 2 deletions astroid/nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import builtins
import io
import itertools
import os
import typing
from typing import List, Optional, TypeVar

Expand Down Expand Up @@ -732,10 +733,17 @@ def relative_to_absolute_name(self, modname, level):
if 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]
):
level = level - 1
package_name = ""
else:
package_name = self.name.rsplit(".", level)[0]
if level and self.name.count(".") < level:
raise TooManyLevelsError(level=level, name=self.name)

package_name = self.name.rsplit(".", level)[0]
elif self.package:
package_name = self.name
else:
Expand All @@ -744,7 +752,7 @@ def relative_to_absolute_name(self, modname, level):
if package_name:
if not modname:
return package_name
return f"{package_name}.{modname}"
return f"{package_name}.{modname.split('.')[0]}"
return modname

def wildcard_import_names(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from namespace_package import top_level_function

top_level_function.do_something()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from ..plugin_api import top_message


def plugin_message(msg):
return "plugin_message: %s" % top_message(msg)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def top_message(msg):
return "top_message: %s" % msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .lower_level.helper_function import plugin_message


def do_something():
return plugin_message("called by do_something")
5 changes: 5 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -6535,5 +6535,10 @@ def play():
assert next(node.infer()).pytype() == ".B"


def test_namespace_package() -> None:
"""check that a file using namespace packages and relative imports is parseable"""
resources.build_file("data/beyond_top_level/import_package.py")


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