Skip to content
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

MyPy does not consider the setter signature when type-checking assignments of properties #16957

Closed
wjakob opened this issue Feb 28, 2024 · 1 comment
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes

Comments

@wjakob
Copy link

wjakob commented Feb 28, 2024

Bug Report
It is a relatively common design pattern that a property setter might be more accepting of various inputs and then perform a conversion internally to conform the input to the right type (in fact, this might be the very reason that a @property is used, as opposed to merely storing data in an attribute).

PyRight accepts the following code without warnings or errors. MyPy does not:

To Reproduce

class C:
    @property
    def x(self, /) -> int:
        return 1

    @x.setter
    def x(self, value: float | int, /) -> None:
        pass

c = C()
c.x = 3.0

Expected Behavior

Accepted without errors or warnings.

Actual Behavior

q.py:11: error: Incompatible types in assignment (expression has type "float", variable has type "int")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.8.0 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12.2
@wjakob wjakob added the bug mypy got something wrong label Feb 28, 2024
@AlexWaygood AlexWaygood added the topic-descriptors Properties, class vs. instance attributes label Feb 28, 2024
@AlexWaygood
Copy link
Member

Thanks! Closing as a duplicate of #3004

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes
Projects
None yet
Development

No branches or pull requests

2 participants