Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self as return type annotation in overloads raises error #14641

Closed
uriyyo opened this issue Feb 7, 2023 · 0 comments · Fixed by #17388
Closed

Self as return type annotation in overloads raises error #14641

uriyyo opened this issue Feb 7, 2023 · 0 comments · Fixed by #17388
Labels
bug mypy got something wrong topic-overloads topic-self-types Types for self

Comments

@uriyyo
Copy link
Member

uriyyo commented Feb 7, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-overloads topic-self-types Types for self
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants