Closed
Description
Bug Report
Self
as return type annotation in overloads
raises error.
To Reproduce
from typing import Generic, TypeVar, Any, overload, Literal, Self
T = TypeVar("T", bound=Any)
class MyMapping(Generic[T]):
@overload
def get(self, obj: Literal[None]) -> Self:
pass
@overload
def get(self, obj: Any) -> T:
pass
def get(self, obj: Any | None) -> T | Self:
return self
Expected Behavior
No error.
Actual Behavior
error: Overloaded function signatures 1 and 2 overlap with incompatible return types
pyright
doesn't raise an error.
Old-style Self
approach works as expected:
from __future__ import annotations
from typing import Generic, TypeVar, Any, overload, Literal
T = TypeVar("T", bound=Any)
_Self = TypeVar("_Self", bound="MyMapping[Any]")
class MyMapping(Generic[T]):
@overload
def get(self: _Self, obj: Literal[None]) -> _Self:
pass
@overload
def get(self, obj: Any) -> T:
pass
def get(self, obj: Any | None) -> T | MyMapping[T]:
return self
Your Environment
- Mypy version used: 1.0.0
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini
(and other config files): - Python version used: 3.11