Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,8 @@ def check___new___signature(self, fdef: FuncDef, typ: CallableType) -> None:
"but must return a subtype of",
)
elif not isinstance(
get_proper_type(bound_type.ret_type), (AnyType, Instance, TupleType, UninhabitedType)
get_proper_type(bound_type.ret_type),
(AnyType, Instance, TupleType, UninhabitedType, LiteralType),
):
self.fail(
message_registry.NON_INSTANCE_NEW_TYPE.format(
Expand Down
35 changes: 35 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,41 @@ class B(A):
def __new__(cls) -> B:
pass

[case testOverride__new__WithLiteralReturnPassing]
[file builtins.py]
from typing import Literal, Protocol, overload

class str: pass
class dict: pass
class float: pass

class _Truthy(Protocol):
def __bool__(self) -> Literal[True]: pass

class _Falsy(Protocol):
def __bool__(self) -> Literal[False]: pass

class bool(int):
@overload
def __new__(cls, __o: _Truthy) -> Literal[True]: pass
@overload
def __new__(cls, __o: _Falsy) -> Literal[False]: pass
def __new__(cls, __o: object):
pass

class int:
def __new__(cls) -> Literal[0]: pass
[typing fixtures/typing-medium.pyi]

[case testOverride__new__WithLiteralReturnFailing]
from typing import Literal

class Foo:
def __new__(cls) -> Literal[1]: pass # E: Incompatible return type for "__new__" (returns "Literal[1]", but must return a subtype of "Foo")

[builtins fixtures/__new__.pyi]
[typing fixtures/typing-medium.pyi]

[case testOverride__new__AndCallObject]
from typing import TypeVar, Generic

Expand Down
1 change: 1 addition & 0 deletions test-data/unit/fixtures/__new__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class object:
class type:
def __init__(self, x) -> None: pass

class float: pass
class int: pass
class bool: pass
class str: pass
Expand Down