Skip to content

Commit b28067e

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Fix a crash when _filter_stmts encounters an EmptyNode (#1534)
1 parent 19734a1 commit b28067e

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Release date: TBA
1414

1515
* Fix ``col_offset`` attribute for nodes involving ``with`` on ``PyPy``.
1616

17+
* Fixed a crash when ``_filter_stmts`` encounters an ``EmptyNode``.
18+
19+
Closes PyCQA/pylint#6438
20+
1721

1822
What's New in astroid 2.11.3?
1923
=============================

astroid/filter_statements.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ def _filter_stmts(base_node: nodes.NodeNG, stmts, frame, offset):
112112
# Fixes issue #375
113113
if mystmt is stmt and _is_from_decorator(base_node):
114114
continue
115-
assert hasattr(node, "assign_type"), (
116-
node,
117-
node.scope(),
118-
node.scope().locals,
119-
)
120-
assign_type = node.assign_type()
121115
if node.has_base(base_node):
122116
break
123117

118+
if isinstance(node, nodes.EmptyNode):
119+
# EmptyNode does not have assign_type(), so just add it and move on
120+
_stmts.append(node)
121+
continue
122+
123+
assign_type = node.assign_type()
124124
_stmts, done = assign_type._get_filtered_stmts(base_node, node, _stmts, mystmt)
125125
if done:
126126
break

tests/unittest_filter_statements.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2+
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
3+
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
4+
5+
from astroid.builder import extract_node
6+
from astroid.filter_statements import _filter_stmts
7+
from astroid.nodes import EmptyNode
8+
9+
10+
def test_empty_node() -> None:
11+
enum_mod = extract_node("import enum")
12+
empty = EmptyNode(parent=enum_mod)
13+
empty.is_statement = True
14+
filtered_statements = _filter_stmts(
15+
empty, [empty.statement(future=True)], empty.frame(future=True), 0
16+
)
17+
assert filtered_statements[0] is empty

0 commit comments

Comments
 (0)