Skip to content

Commit a820438

Browse files
BekaValentinetausbn
authored andcommitted
Adds fix for __init_subclass__ bug. (#2390)
* Adds fix for __init_subclass__ bug. * Adds test case. * Move test on name. I think it makes more sense here, alongside the other "special" method names.
1 parent 5c3c8eb commit a820438

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

python/ql/src/Functions/NonSelf.ql

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

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,18 @@ def meth(arg):
117117
pass
118118

119119
Z().meth(0)
120+
121+
122+
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
125+
# despite not being marked as such with other means. The name alone is what
126+
# makes it such. As a consequence, the query `py/not-named-self` and other
127+
# relevant queries need to account for this.
128+
#
129+
# 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+
132+
class InitSubclass(object):
133+
def __init_subclass__(cls):
134+
pass

0 commit comments

Comments
 (0)