Skip to content

semanal: Treat __init_subclass__ as an implicit classmethod #7355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ class FuncBase(Node):
'unanalyzed_type',
'info',
'is_property',
'is_class', # Uses "@classmethod"
'is_class', # Uses "@classmethod" (explicit or implicit)
'is_static', # Uses "@staticmethod"
'is_final', # Uses "@final"
'_fullname',
Expand Down
4 changes: 3 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,12 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
if not func.arguments:
self.fail('Method must have at least one argument', func)
elif isinstance(functype, CallableType):
if func.name() == '__init_subclass__':
func.is_class = True
self_type = functype.arg_types[0]
if isinstance(self_type, AnyType):
leading_type = fill_typevars(info) # type: Type
if func.is_class or func.name() in ('__new__', '__init_subclass__'):
if func.is_class or func.name() == '__new__':
leading_type = self.class_type(leading_type)
func.type = replace_implicit_first_type(functype, leading_type)

Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-super.test
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ def g(b: B) -> None:
super(B, b).f('42')
super(B, b).f(42) # E: Argument 1 to "f" of "A" has incompatible type "int"; expected "str"

[case testSuperInInitSubclass]
class A:
def __init_subclass__(cls) -> None:
super().__init_subclass__()
[builtins fixtures/__init_subclass__.pyi]


-- Invalid uses of super()
-- -----------------------
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/fixtures/__init_subclass__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# builtins stub with object.__init_subclass__

class object:
def __init_subclass__(cls) -> None: pass

class type: pass

class int: pass
class bool: pass
class str: pass
class function: pass