python / python/mypy

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

Open
#17,393 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-descriptors topic-inheritance
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied Python reproducer and compare it with the version where @foo() is removed. Trace how decorated property setters are handled during property override checking; done means mypy reports the incompatible int-to-str override in the decorated case as well.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.