You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classA:
definit(self) ->None:
self._a: int|None=None@propertydefa(self) ->int:
ifself._aisNone:
raiseValueErrorreturnself._a@a.setterdefa(self, val: int|None) ->None:
self._a=vala=A()
a.a=None# Incompatible types in assignment (expression has type "None", variable has type "int") [assignment]
In my scenario I would like it to work like this but if it is bad practice that the setter allows different types than the getter it should warn in the definition of the setter…
I would argue that it is not necessarily a problem in itself. We can use this to implement a "smart setter" that takes as type to build on this example Union[int, str], and tries to convert a str to an int.
What I see as a problem is rather that when you then try to set foo in a way compatible with the declaration and implementation (ie. assign a string to it), then despite the code being correct, mypy will ignore the type declared in the setter, and validate against the getter type instead - see #3004 where the consensus is that this is a valid use case.
Even if #3004 gets implemented, there are still several examples here of invalid setters that should be errors (incorrect number of arguments) so I'm leaving this issue open regardless
Activity
DetachHead commentedon May 28, 2022
also no error when there's no second argument:
[-]no error when setter has too many arguments[/-][+]signatures of setters are not checked[/+]DetachHead commentedon Jul 20, 2022
and also no error when the setter type is different to the getter type
however you get an error when attempting to use the setter:
q-wertz commentedon Feb 3, 2023
Similar example:
In my scenario I would like it to work like this but if it is bad practice that the setter allows different types than the getter it should warn in the definition of the setter…
ydirson commentedon May 12, 2023
I would argue that it is not necessarily a problem in itself. We can use this to implement a "smart setter" that takes as type to build on this example
Union[int, str], and tries to convert astrto anint.What I see as a problem is rather that when you then try to set
fooin a way compatible with the declaration and implementation (ie. assign a string to it), then despite the code being correct,mypywill ignore the type declared in the setter, and validate against the getter type instead - see #3004 where the consensus is that this is a valid use case.Maybe close this ticket?
DetachHead commentedon May 12, 2023
Even if #3004 gets implemented, there are still several examples here of invalid setters that should be errors (incorrect number of arguments) so I'm leaving this issue open regardless
Support properties with setter type different from getter type (pytho…
Support properties with setter type different from getter type (pytho…
DetachHead commentedon Mar 26, 2025
none of these examples seem to be fixed as of 1.15.0
JukkaL commentedon Mar 27, 2025
The fix will be in the next feature release. Have you tried master?