Skip to content

Commit e1644dd

Browse files
authored
Python: Handle __class_getitem__ in py/not-named-self (#2825)
Fixes #2824
1 parent dcb41a1 commit e1644dd

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

python/ql/src/Functions/NonSelf.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ where
3232
not name = "__new__" and
3333
not name = "__metaclass__" and
3434
not name = "__init_subclass__" and
35+
not name = "__class_getitem__" and
3536
/* declared in scope */
3637
f.getScope() = cls.getScope()
3738
) and

python/ql/test/query-tests/Functions/general/parameter_names.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,22 @@ def meth(arg):
120120

121121

122122

123-
# The `__init_subclass__` method is a new method introduced into Python 3.6
124-
# which does not follow the normal conventions, and is in fact a class method
123+
# The `__init_subclass__` (introduced in Python 3.6)
124+
# and `__class_getitem__` (introduced in Python 3.7) methods are methods
125+
# which do not follow the normal conventions, and are in fact class methods
125126
# despite not being marked as such with other means. The name alone is what
126127
# makes it such. As a consequence, the query `py/not-named-self` and other
127128
# relevant queries need to account for this.
128129
#
129130
# This has come up in the wild via LGTM as a false positive. For example,
130-
# https://docs.python.org/3/reference/datamodel.html#customizing-class-creation
131+
# `__init_subclass__`:
132+
# https://docs.python.org/3/reference/datamodel.html#customizing-class-creation
133+
# `__class_getitem__`:
134+
# https://docs.python.org/3/reference/datamodel.html#emulating-generic-types
131135

132-
class InitSubclass(object):
136+
class SpecialMethodNames(object):
133137
def __init_subclass__(cls):
134138
pass
139+
140+
def __class_getitem__(cls):
141+
pass

0 commit comments

Comments
 (0)