Skip to content

Commit 887b1b6

Browse files
Pierre-Sassoulascodegen-sh[bot]cdce8p
authored
[fix] AttributeError crash when a slice is used as a class decorator (#10350) (#10361)
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 6a02395 commit 887b1b6

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ def visit_decorators(self, node: nodes.Decorators) -> None:
136136
inf = safe_infer(children[0].func)
137137
else:
138138
inf = safe_infer(children[0])
139-
qname = inf.qname() if inf else None
139+
if not isinstance(inf, (nodes.ClassDef, nodes.FunctionDef)):
140+
return
141+
qname = inf.qname()
140142
if qname in self.deprecated_decorators():
141143
self.add_message("deprecated-decorator", node=node, args=qname)
142144

pylint/checkers/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ def decorated_with(
876876
if any(
877877
i.name in qnames or i.qname() in qnames
878878
for i in decorator_node.infer()
879-
if i is not None and not isinstance(i, util.UninferableBase)
879+
if isinstance(i, (nodes.ClassDef, nodes.FunctionDef))
880880
):
881881
return True
882882
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)