Skip to content

Commit 555a128

Browse files
[fix] Prevent crash on slice decorator for 'six' decorated function (#2738)
Closes #2721
1 parent 59f36e7 commit 555a128

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
@@ -41,6 +41,11 @@ Release date: TBA
4141

4242
Closes #2734
4343

44+
* Fix a crash when parsing a slice called in a decorator on a function that is also decorated with
45+
a known ``six`` decorator.
46+
47+
Closes #2721
48+
4449
What's New in astroid 3.3.10?
4550
=============================
4651
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
@@ -504,3 +504,19 @@ def test_regression_no_crash_during_build() -> None:
504504
node: nodes.Attribute = extract_node("__()")
505505
assert node.args == []
506506
assert node.as_string() == "__()"
507+
508+
509+
def test_regression_no_crash_on_called_slice() -> None:
510+
"""Regression test for issue #2721."""
511+
node: nodes.Attribute = extract_node(
512+
textwrap.dedent(
513+
"""
514+
s = slice(-2)
515+
@s()
516+
@six.add_metaclass()
517+
class a: ...
518+
"""
519+
)
520+
)
521+
assert isinstance(node, nodes.ClassDef)
522+
assert node.name == "a"

0 commit comments

Comments
 (0)