You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
__new__ return types are not respected (reported as error and overwritten by the current class) unless the return type is a subclass of the current class #19624
fromtypingimportcastclassA:
def__new__(cls) ->int:
return1classB:
def__new__(cls) ->"C":
returncast(C, None)
classC(B):
def__new__(cls) ->B:
returncast(B, None)
reveal_type(A()) # Expect int (builtin.int)reveal_type(B()) # Expect C (new.C)reveal_type(C()) # Expect B (new.B)
Run
mypy new.py --config-file=
(The --config-file= ignores configuration files)
Expected:
new.py:15: note: Revealed type is "builtin.int"
new.py:16: note: Revealed type is "new.C"
new.py:17: note: Revealed type is "new.B"
Actual:
new.py:4: error: Incompatible return type for "__new__" (returns "int", but must return a subtype of "A") [misc]
new.py:12: error: Incompatible return type for "__new__" (returns "B", but must return a subtype of "C") [misc]
new.py:15: note: Revealed type is "new.A"
new.py:16: note: Revealed type is "new.C"
new.py:17: note: Revealed type is "new.C"
Found 2 errors in 1 file (checked 1 source file)
(Note: Some of these issues are noted as no longer reproducable in the comments. I didn't check their reproducability, but I am able to reproduce this one, which might mean a regression.)