Skip to content

Commit 68b72f9

Browse files
[fix] AttributeError crash when a slice is used as a class decorator
Closes #10334
1 parent 8a733fc commit 68b72f9

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

doc/whatsnew/fragments/10334.bugfix

+3
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.
2+
3+
Closes #10334

pylint/checkers/deprecated.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def visit_decorators(self, node: nodes.Decorators) -> None:
138138
inferred = safe_infer(children[0])
139139
if not inferred:
140140
return
141-
qname = inferred.qname()
141+
qname = inferred.qname() if hasattr(inferred, "qname") else ""
142142
if qname in self.deprecated_decorators():
143143
self.add_message("deprecated-decorator", node=node, args=qname)
144144

@@ -217,7 +217,12 @@ def check_deprecated_attribute(self, node: astroid.Attribute) -> None:
217217
inferred_expr = safe_infer(node.expr)
218218
if not isinstance(inferred_expr, (nodes.ClassDef, Instance, nodes.Module)):
219219
return
220-
attribute_qname = ".".join((inferred_expr.qname(), node.attrname))
220+
attribute_qname = ".".join(
221+
(
222+
inferred_expr.qname() if hasattr(inferred_expr, "qname") else "",
223+
node.attrname,
224+
)
225+
)
221226
for deprecated_name in self.deprecated_attributes():
222227
if attribute_qname == deprecated_name:
223228
self.add_message(

pylint/checkers/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,8 @@ def decorated_with(
873873
decorator_node = decorator_node.func
874874
try:
875875
if any(
876-
i.name in qnames or i.qname() in qnames
876+
hasattr(i, "name")
877+
and (i.name in qnames or (hasattr(i, "qname") and i.qname() in qnames))
877878
for i in decorator_node.infer()
878879
if i is not None and not isinstance(i, util.UninferableBase)
879880
):
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."""

0 commit comments

Comments
 (0)