Skip to content

__repr__ defined from helper function cannot be overridden #17562

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

Closed
asottile-sentry opened this issue Jul 22, 2024 · 1 comment
Closed

__repr__ defined from helper function cannot be overridden #17562

asottile-sentry opened this issue Jul 22, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@asottile-sentry
Copy link

Bug Report

I expect to be able to define __repr__ via a function which returns a function -- this should act the same as if the function were defined inline. I then want to be able to subclass this and override it but I seemingly cannot

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=90291dba6f20ad59d33d32c420103fc3

from collections.abc import Callable


def makes_repr() -> Callable[[object], str]:
    return lambda self: f'{type(self)}(...)'


class Base:
    __repr__ = makes_repr()


class Sub(Base):
    def __repr__(self) -> str:
        return 'Sub(...)'

Expected Behavior

it should accept this program -- maybe....

Actual Behavior

$ mypy t.py
t.py:13: error: Signature of "__repr__" incompatible with supertype "Base"  [override]
t.py:13: note:      Superclass:
t.py:13: note:          def (object, /) -> str
t.py:13: note:      Subclass:
t.py:13: note:          def __repr__(self) -> str
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.2
@asottile-sentry asottile-sentry added the bug mypy got something wrong label Jul 22, 2024
@asottile-sentry
Copy link
Author

this seems to have gotten fixed by mypy 1.16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant