summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Shachnev <mitya57@users.noreply.github.com>2021-11-21 19:24:08 +0300
committerGitHub <noreply@github.com>2021-11-21 17:24:08 +0100
commitce5cbce5ba11cdc2f8139ade66feea1e181a7944 (patch)
tree7a455f71ca44af7672e8219075a2857e8bb6d867
parent935ff12e0d65899d3d48d1c7be9cbeeb1d9e6089 (diff)
downloadastroid-git-ce5cbce5ba11cdc2f8139ade66feea1e181a7944.tar.gz
Always treat __class_getitem__ as a classmethod (#1266)
* Always treat __class_getitem__ as a classmethod Consider this code: class Foo: def __class_getitem__(cls, *args, **kwargs): return cls Without this change, astroid would treat cls as an instance of Foo, and thus it will think of Foo[bar] also as an instance, while really it is the same as Foo. Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
-rw-r--r--ChangeLog1
-rw-r--r--astroid/nodes/scoped_nodes.py2
-rw-r--r--tests/unittest_inference.py14
3 files changed, 17 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index a8dd154a..0f047d39 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,7 @@ What's New in astroid 2.9.0?
============================
Release date: TBA
+* Always treat ``__class_getitem__`` as a classmethod.
What's New in astroid 2.8.6?
diff --git a/astroid/nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes.py
index f8c60131..cfc14d72 100644
--- a/astroid/nodes/scoped_nodes.py
+++ b/astroid/nodes/scoped_nodes.py
@@ -1570,6 +1570,8 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda):
return "classmethod"
if self.name == "__init_subclass__":
return "classmethod"
+ if self.name == "__class_getitem__":
+ return "classmethod"
type_name = "method"
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index 26926292..d6e5e4bf 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -4605,6 +4605,20 @@ class TestBool(unittest.TestCase):
inferred = next(node.infer())
self.assertEqual(inferred, util.Uninferable)
+ def test_class_subscript(self) -> None:
+ node = extract_node(
+ """
+ class Foo:
+ def __class_getitem__(cls, *args, **kwargs):
+ return cls
+
+ Foo[int]
+ """
+ )
+ inferred = next(node.infer())
+ self.assertIsInstance(inferred, nodes.ClassDef)
+ self.assertEqual(inferred.name, "Foo")
+
class TestType(unittest.TestCase):
def test_type(self) -> None: