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 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
6 changes: 5 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +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 (
Expand Down Expand Up @@ -407,6 +407,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
113 changes: 111 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,61 @@ import typing
class A:
def __init__(self, x: int): pass
[out]
main:3: error: The return type of "__init__" must be None

[case testDecoratedConstructorWithImplicitReturnValueType]
import typing
from typing import Callable

def deco(fn: Callable) -> Callable:
return fn

class A:
@deco
def __init__(self, x: int): pass
[out]

[case testOverloadedConstructorWithImplicitReturnValueType]
from foo import *
[file foo.pyi]
from typing import overload
class Foo:
@overload
def __init__(self, a: int):
pass

@overload
def __init__(self, a: str):
pass

[case testConstructorWithAnyReturnValueType]
import typing
from typing import Any
class A:
def __init__(self) -> Any: pass # E: The return type of "__init__" must be None

[case testDecoratedConstructorWithAnyReturnValueType]
import typing
from typing import Callable, Any

def deco(fn: Callable) -> Callable:
return fn

class A:
@deco
def __init__(self) -> Any: pass # E: The return type of "__init__" must be None

[case testOverloadedConstructorWithAnyReturnValueType]
from foo import *
[file foo.pyi]
from typing import overload, Any
class Foo:
@overload
def __init__(self, a: int) -> Any: # E: The return type of "__init__" must be None
pass

@overload
def __init__(self, a: str) -> Any: # E: The return type of "__init__" must be None
pass

[case testInitSubclassWithReturnValueType]
import typing
Expand All @@ -591,7 +645,62 @@ 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 testDecoratedInitSubclassWithImplicitReturnValueType]
import typing
from typing import Callable

def deco(fn: Callable) -> Callable:
return fn

class A:
@deco
def __init_subclass__(cls, x: int=1): pass
[out]

[case testOverloadedInitSubclassWithImplicitReturnValueType]
from foo import *
[file foo.pyi]
from typing import overload
class Foo:
@overload
def __init_subclass__(cls, a: int):
pass

@overload
def __init_subclass__(cls, a: str):
pass

[case testInitSubclassWithAnyReturnValueType]
import typing
from typing import Any
class A:
def __init_subclass__(cls) -> Any: pass # E: The return type of "__init_subclass__" must be None

[case testDecoratedInitSubclassWithAnyReturnValueType]
import typing
from typing import Callable, Any

def deco(fn: Callable) -> Callable:
return fn

class A:
@deco
def __init_subclass__(cls) -> Any: pass # E: The return type of "__init_subclass__" must be None
[out]

[case testOverloadedInitSubclassWithAnyReturnValueType]
from foo import *
[file foo.pyi]
from typing import overload, Any
class Foo:
@overload
def __init_subclass__(cls, a: int) -> Any: # E: The return type of "__init_subclass__" must be None
pass

@overload
def __init_subclass__(cls, a: str) -> Any: # E: The return type of "__init_subclass__" must be None
pass

[case testGlobalFunctionInitWithReturnType]
import typing
Expand Down