Can you add a generic counterpart to the property decorator class to allow for proper typing of object properties. This would align with many other new classes in the typing library to improve type hinting in our code.
I'm not sure what this give beyond the standard property. Is there anything in your example program whose type gets inferred incorrectly in your example if you just use @property? Or is there anything which mypy incorrectly disallows when using that?
I'm not sure what this give beyond the standard property. Is there anything in your example program whose type gets inferred incorrectly in your example if you just use @property? Or is there anything which mypy incorrectly disallows when using that?
In my case, I have a generic class with a property of that generic type. I can type-hint the @property annotated function and PyCharm would understand it. However, if I type-hint @foo.setter function, PyCharm has no idea what type the setter expects.
fromtypingimportTypeVar, GenericT=TypeVar('T')
classC(Generic[T]):
def__init__(self, val: T):
self._val=val@propertydefval(self) ->T:
returnself._val@val.setterdefval(self, val: T):
self._val=valc=C('abc')
x: float=c.val# Warningc.val=5# No problem
This honestly sounds like a bug/missing feature in PyCharm, although I think mypy has similar issues. The type of a property is the same as the return type of the getter function. (It's a bit more complicated if it has getter and setter methods accepting different types, but the principle is the same.) I don't think a generic would make any difference in the provided code samples.
Activity
gvanrossum commented on Sep 30, 2020
I'm not sure what this give beyond the standard property. Is there anything in your example program whose type gets inferred incorrectly in your example if you just use @property? Or is there anything which mypy incorrectly disallows when using that?
bubblefoil commented on May 5, 2021
In my case, I have a generic class with a property of that generic type. I can type-hint the @property annotated function and PyCharm would understand it. However, if I type-hint @foo.setter function, PyCharm has no idea what type the setter expects.
jp-larose commented on May 5, 2021
Sorry, I never came back to this until now...
The point for me was to ensure/validate that the getter and setter for a given property had a matching type.
srittau commented on May 13, 2021
This honestly sounds like a bug/missing feature in PyCharm, although I think mypy has similar issues. The type of a property is the same as the return type of the getter function. (It's a bit more complicated if it has getter and setter methods accepting different types, but the principle is the same.) I don't think a generic would make any difference in the provided code samples.
srittau commented on May 13, 2021
One thing we could improve is the types of property in typeshed, though. Making it generic there sounds like a good idea to me.
jp-larose commented on May 18, 2021
I think this might be more appropriate than my initial suggestion.
srittau commented on Nov 4, 2021
Closing this as a duplicate of #594 and python/typeshed#4731.