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

My 1.5.1 gets confused when a class variable references a method from another class that returns Self. #15934

Closed
benwah opened this issue Aug 22, 2023 · 1 comment · Fixed by #15945
Labels
bug mypy got something wrong topic-self-types Types for self

Comments

@benwah
Copy link

benwah commented Aug 22, 2023

Bug Report

When you define a class variable on a class that references a method that returns Self from another class, MyPy thinks that Self is the class where the method is referenced/

To Reproduce

from typing import Self


class Foo:
    def foo(self) -> Self:
        return self


class Ben:
    MY_MAP = {
        "foo": Foo.foo,
    }

    @classmethod
    def doit(cls) -> Foo:
        foo_method = cls.MY_MAP["foo"]
        return foo_method(Foo())


assert isinstance(Ben.doit(), Foo)

Expected Behavior

Mypy should not detect any errors.

Actual Behavior

ben.py:17: error: Incompatible return value type (got "Ben", expected "Foo")  [return-value]
ben.py:17: error: Argument 1 has incompatible type "Foo"; expected "Ben"  [arg-type]

Your Environment

  • Mypy version used: 1.5.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
plugins = [
  "pydantic.mypy"
]
explicit_package_bases = true
python_version = "3.11"
check_untyped_defs = true
exclude = '''
(?x)(
    ^.*flycheck.*$
    | ^.*#.*$
  )
'''
  • Python version used: 3.11
@benwah benwah added the bug mypy got something wrong label Aug 22, 2023
@AlexWaygood AlexWaygood added the topic-self-types Types for self label Aug 22, 2023
@hauntsaninja
Copy link
Collaborator

Regression is related to #15541 cc @ilevkivskyi

ilevkivskyi added a commit that referenced this issue Aug 24, 2023
Fixes #15934

I think this is a right thing to do, it may even fix some other rare
accidental `TypeVar` clashes not involving self-types. This causes a bit
of churn in tests, but not too much.
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-self-types Types for self
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants