python / python/mypy

Error when overriding a property created via decorators

Open
#14,684 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When a parent class has a property, created via decorators with a getter and setter. If you attempt to override the fset of the parent by using decorators in the subclass, mypy throws an error.

To Reproduce

class A:
    def __init__(self, x: int):
        self._x = x

    @property
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, new_value: int):
        self._x = new_value


class B(A):
    def __init__(self):
        super().__init__(100)

    @A.x.setter
    def x(self, new_value: int):
        A.x.fset(self, new_value)

        print("Do something else ontop of the parent setter")

Expected Behavior

Mypy should produce no errors

Actual Behavior

Mypy produces the errors:

overloaded function has no attribute "setter" [attr-defined] on the decorator line in the subclass.
overloaded function has no attribute "fset" [attr-defined] on the call to fset in the subclass.

If instead of using a decorator, the property is created in the parent class via the property() function like below, then mypy does not produce any errors.

class A:
    def __init__(self, x: int):
        self._x = x

    def _get_x(self) -> int:
        return self._x

    def _set_x(self, new_value: int):
        self._x = new_value

    x = property(_get_x, _set_x)

Your Environment

  • Mypy version used: 1.0.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): Default configuration
  • Python version used: 3.10.5

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

Start by reproducing the reported errors with the provided Python examples under mypy 1.0.0 and default configuration. Locate the type-checking entry point for decorated properties and add a regression test covering subclass setter overrides and parent fset access. Done means both examples type-check without attr-defined errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.