Skip to content
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

Make return type implicitly None for type checked __init__ and __init_subclass__ #5677

Merged
merged 8 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 5 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@
from mypy.types import (
FunctionLike, UnboundType, TypeVarDef, TupleType, UnionType, StarType, function_type,
CallableType, Overloaded, Instance, Type, AnyType,
TypeTranslator, TypeOfAny, TypeType,
)
TypeTranslator, TypeOfAny, TypeType, NoneTyp)
from mypy.nodes import implicit_module_attrs
from mypy.typeanal import (
TypeAnalyser, analyze_type_alias, no_subscript_builtin_alias,
Expand Down Expand Up @@ -407,6 +406,10 @@ def _visit_func_def(self, defn: FuncDef) -> None:
add_symbol = False
if add_symbol:
self.type.names[defn.name()] = SymbolTableNode(MDEF, defn)
if defn.type is not None and defn.name() in ('__init__', '__init_subclass__'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this works with overloads and decorated methods? Please add tests to verify that.

Also perhaps it should only work inside a class definition?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one needs similar check at least in visit_overloaded_func_def.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gvanrossum It works only inside a class definition.
@gvanrossum @ilevkivskyi You are right, I will find a way to extend behavior.

assert isinstance(defn.type, CallableType)
if isinstance(defn.type.ret_type, AnyType):
defn.type = defn.type.copy_modified(ret_type=NoneTyp())
self.prepare_method_signature(defn, self.type)
elif self.is_func_scope():
# Nested function
Expand Down
2 changes: 0 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ import typing
class A:
def __init__(self, x: int): pass
[out]
main:3: error: The return type of "__init__" must be None

[case testInitSubclassWithReturnValueType]
import typing
Expand All @@ -591,7 +590,6 @@ import typing
class A:
def __init_subclass__(cls, x: int=1): pass
[out]
main:3: error: The return type of "__init_subclass__" must be None

[case testGlobalFunctionInitWithReturnType]
import typing
Expand Down