Skip to content

Commit 97aac52

Browse files
[fix] Fix a crash when the root of a node is not a module but is unknown (pylint-dev#2739)
Closes pylint-dev#2672
1 parent 555a128 commit 97aac52

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ What's New in astroid 3.3.11?
3737
=============================
3838
Release date: TBA
3939

40+
* Fix a crash when the root of a node is not a module but is unknown.
41+
42+
Closes #2672
43+
4044
* Fix a crash when parsing an empty arbitrary expression with ``extract_node`` (``extract_node("__()")``).
4145

4246
Closes #2734

astroid/nodes/node_ng.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,13 @@ def scope(self) -> nodes.LocalsDictNodeNG:
309309
raise ParentMissingError(target=self)
310310
return self.parent.scope()
311311

312-
def root(self) -> nodes.Module:
312+
def root(self) -> nodes.Module | nodes.Unknown:
313313
"""Return the root node of the syntax tree.
314314
315315
:returns: The root node.
316316
"""
317317
if not (parent := self.parent):
318-
assert isinstance(self, nodes.Module)
318+
assert isinstance(self, (nodes.Module, nodes.Unknown))
319319
return self
320320

321321
while parent.parent:

tests/test_regrtest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,21 @@ def _get_option(self, option):
500500
assert node.inferred()[0].value == "mystr"
501501

502502

503+
def test_regression_root_is_not_a_module() -> None:
504+
"""Regression test for #2672."""
505+
node: nodes.Attribute = _extract_single_node(
506+
textwrap.dedent(
507+
"""
508+
a=eval.__get__(1).__gt__
509+
510+
@a
511+
class c: ...
512+
"""
513+
)
514+
)
515+
assert node.name == "c"
516+
517+
503518
def test_regression_no_crash_during_build() -> None:
504519
node: nodes.Attribute = extract_node("__()")
505520
assert node.args == []

0 commit comments

Comments
 (0)