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
I propose allowing the use of @overload on __new__ to specify the return type. E.g.:
T = TypeVar('T')
class MyClass(Generic[T]):
@overload
def __new__(cls,val: str) -> 'MyClass[int]':
pass
@overload
def __new__(cls,val: int) -> int:
pass
def __new__(cls,val):
if isinstance(val,int):
return val
r = super().__init__(cls)
if isinstance(val,str):
r.val = int(val)
r.val = val
return r
This is useful on its own, but if @overload is allowed to have return types that only differ by the generic type argument (e.g. MyClass[int] vs MyClass[str]), then this would also supersede issue #307 and allow more complicated type argument deduction.
The text was updated successfully, but these errors were encountered:
This sounds like something to be filed in the mypy tracker. What's a realistic use case for allowing __new__ to return something that's not an instance of the containing class?
What's a realistic use case for allowing __new__ to return something that's not an instance of the containing class?
To be fair typing uses this few times :-) but I agree this is quite esoteric, and this feature proposal belongs to https://github.com/python/mypy/issues
I propose allowing the use of
@overload
on__new__
to specify the return type. E.g.:This is useful on its own, but if
@overload
is allowed to have return types that only differ by the generic type argument (e.g.MyClass[int]
vsMyClass[str]
), then this would also supersede issue #307 and allow more complicated type argument deduction.The text was updated successfully, but these errors were encountered: