Skip to content

Commit 0aaf213

Browse files
[fix] Prevent crash on slice decorator for 'six' decorated function (#2738) (#2740)
Closes #2721 (cherry picked from commit 555a128) Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
1 parent c8bd28a commit 0aaf213

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ Release date: TBA
1717

1818
Closes #2734
1919

20+
* Fix a crash when parsing a slice called in a decorator on a function that is also decorated with
21+
a known ``six`` decorator.
22+
23+
Closes #2721
24+
2025
What's New in astroid 3.3.10?
2126
=============================
2227
Release date: 2025-05-10

astroid/brain/brain_six.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ def transform_six_add_metaclass(node): # pylint: disable=inconsistent-return-st
182182
func = next(decorator.func.infer())
183183
except (InferenceError, StopIteration):
184184
continue
185-
if func.qname() == SIX_ADD_METACLASS and decorator.args:
185+
if (
186+
isinstance(func, (nodes.FunctionDef, nodes.ClassDef))
187+
and func.qname() == SIX_ADD_METACLASS
188+
and decorator.args
189+
):
186190
metaclass = decorator.args[0]
187191
node._metaclass = metaclass
188192
return node

tests/test_regrtest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,19 @@ def test_regression_no_crash_during_build() -> None:
503503
node: nodes.Attribute = extract_node("__()")
504504
assert node.args == []
505505
assert node.as_string() == "__()"
506+
507+
508+
def test_regression_no_crash_on_called_slice() -> None:
509+
"""Regression test for issue #2721."""
510+
node: nodes.Attribute = extract_node(
511+
textwrap.dedent(
512+
"""
513+
s = slice(-2)
514+
@s()
515+
@six.add_metaclass()
516+
class a: ...
517+
"""
518+
)
519+
)
520+
assert isinstance(node, nodes.ClassDef)
521+
assert node.name == "a"

0 commit comments

Comments
 (0)