Skip to content

Commit bd78ab0

Browse files
Harden get_module_part() against "." (#2202)
1 parent a6eb2b8 commit bd78ab0

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

ChangeLog

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,19 @@ Release date: TBA
148148
Refs pylint-dev/pylint#8598
149149

150150

151-
What's New in astroid 2.15.5?
151+
What's New in astroid 2.15.6?
152152
=============================
153153
Release date: TBA
154154

155+
* Harden ``get_module_part()`` against ``"."``.
156+
157+
Closes pylint-dev/pylint#8749
158+
159+
160+
What's New in astroid 2.15.5?
161+
=============================
162+
Release date: 2023-05-14
163+
155164
* Handle ``objects.Super`` in ``helpers.object_type()``.
156165

157166
Refs pylint-dev/pylint#8554

astroid/modutils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ def get_module_part(dotted_name: str, context_file: str | None = None) -> str:
430430
), "explicit relative import, but no context_file?"
431431
path = [] # prevent resolving the import non-relatively
432432
starti = 1
433-
while parts[starti] == "": # for all further dots: change context
433+
# for all further dots: change context
434+
while starti < len(parts) and parts[starti] == "":
434435
starti += 1
435436
assert (
436437
context_file is not None

tests/test_modutils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def test_get_module_part_exception(self) -> None:
147147
ImportError, modutils.get_module_part, "unknown.module", modutils.__file__
148148
)
149149

150+
def test_get_module_part_only_dot(self) -> None:
151+
self.assertEqual(modutils.get_module_part(".", modutils.__file__), ".")
152+
150153

151154
class ModPathFromFileTest(unittest.TestCase):
152155
"""Given an absolute file path return the python module's path as a list."""

0 commit comments

Comments
 (0)