Skip to content

__repr__ defined from helper function cannot be overridden #17562

Closed
@asottile-sentry

Description

@asottile-sentry

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions