python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.5k stars 2.83k forks source link

no error when overriding property with incompatible return type when the overridden property has another decorator #17393

Open DetachHead opened 5 months ago

DetachHead commented 5 months ago
from contextlib import contextmanager
from typing import Iterator

@contextmanager
def foo() -> Iterator[None]:
    yield

class Bar:
    @property
    def asdf(self) -> int:
        return 2

class Baz(Bar):
    @property
    def asdf(self) -> str: # no error
        return ""

    @asdf.setter
    @foo()
    def asdf(self, new_value): ...

playground

removing the @foo() decorator makes the error appear:

main.py:16: error: Signature of "asdf" incompatible with supertype "Bar"  [override]
main.py:16: note:      Superclass:
main.py:16: note:          int
main.py:16: note:      Subclass:
main.py:16: note:          str
Found 1 error in 1 file (checked 1 source file)
DetachHead commented 2 weeks ago

actually it's not just contextmanager, but any other decorator:

from typing import Callable

def foo[**P, T](fn: Callable[P, T]) -> Callable[P, T]:
    return fn

class Bar:
    @property
    def asdf(self) -> int:
        return 2

class Baz(Bar):
    @property
    def asdf(self) -> str: # no error
        return ""

    @asdf.setter
    @foo
    def asdf(self, new_value: str) -> None: ...