Skip to content

Commit 77481a6

Browse files
Pierre-Sassoulascodegen-sh[bot]cdce8p
authored
[fix] AttributeError crash when a slice is used as a class decorator (#10350)
Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
1 parent c3f7efe commit 77481a6

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed

doc/whatsnew/fragments/10334.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Using a slice as a class decorator now raise a 'not-callable' message instead of crashing pylint. A lot of checks that dealt with decorators (too many to list) are now shortcut if the decorator can't immediately be inferred to a function or class definition.
2+
3+
Closes #10334

pylint/checkers/deprecated.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def visit_decorators(self, node: nodes.Decorators) -> None:
136136
inferred = safe_infer(children[0].func)
137137
else:
138138
inferred = safe_infer(children[0])
139-
if not inferred:
139+
if not isinstance(inferred, (nodes.ClassDef, nodes.FunctionDef)):
140140
return
141141
qname = inferred.qname()
142142
if qname in self.deprecated_decorators():

pylint/checkers/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ def decorated_with(
875875
if any(
876876
i.name in qnames or i.qname() in qnames
877877
for i in decorator_node.infer()
878-
if i is not None and not isinstance(i, util.UninferableBase)
878+
if isinstance(i, (nodes.ClassDef, nodes.FunctionDef))
879879
):
880880
return True
881881
except astroid.InferenceError:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Test for slice object used as a decorator."""
2+
# pylint: disable=too-few-public-methods
3+
s = slice(-2)
4+
@s() # [not-callable]
5+
class A:
6+
"""Class with a slice decorator."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
not-callable:4:1:4:4:A:s is not callable:UNDEFINED

0 commit comments

Comments
 (0)