Skip to content

Commit

Permalink
Fix a crash when _filter_stmts encounters an EmptyNode (#1534)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored and Pierre-Sassoulas committed May 2, 2022
1 parent 19734a1 commit b28067e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Release date: TBA

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

* Fixed a crash when ``_filter_stmts`` encounters an ``EmptyNode``.

Closes PyCQA/pylint#6438


What's New in astroid 2.11.3?
=============================
Expand Down
12 changes: 6 additions & 6 deletions astroid/filter_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ def _filter_stmts(base_node: nodes.NodeNG, stmts, frame, offset):
# Fixes issue #375
if mystmt is stmt and _is_from_decorator(base_node):
continue
assert hasattr(node, "assign_type"), (
node,
node.scope(),
node.scope().locals,
)
assign_type = node.assign_type()
if node.has_base(base_node):
break

if isinstance(node, nodes.EmptyNode):
# EmptyNode does not have assign_type(), so just add it and move on
_stmts.append(node)
continue

assign_type = node.assign_type()
_stmts, done = assign_type._get_filtered_stmts(base_node, node, _stmts, mystmt)
if done:
break
Expand Down
17 changes: 17 additions & 0 deletions tests/unittest_filter_statements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt

from astroid.builder import extract_node
from astroid.filter_statements import _filter_stmts
from astroid.nodes import EmptyNode


def test_empty_node() -> None:
enum_mod = extract_node("import enum")
empty = EmptyNode(parent=enum_mod)
empty.is_statement = True
filtered_statements = _filter_stmts(
empty, [empty.statement(future=True)], empty.frame(future=True), 0
)
assert filtered_statements[0] is empty

0 comments on commit b28067e

Please sign in to comment.