Skip to content

Commit

Permalink
Make return type implicitly None for type checked __init__ and __init…
Browse files Browse the repository at this point in the history
…_subclass__ (python#5677)

Implements python#604 (comment).
  • Loading branch information
onlined authored and root committed Oct 4, 2018
1 parent 29152c1 commit 85c173b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
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__'):
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

0 comments on commit 85c173b

Please sign in to comment.