-
Notifications
You must be signed in to change notification settings - Fork 258
Feature Request: Add a generic property class #758
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
Comments
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 |
In my case, I have a generic class with a property of that generic type. I can type-hint the from typing import TypeVar, Generic
T = TypeVar('T')
class C(Generic[T]):
def __init__(self, val: T):
self._val = val
@property
def val(self) -> T:
return self._val
@val.setter
def val(self, val: T):
self._val = val
c = C('abc')
x: float = c.val # Warning
c.val = 5 # No problem |
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. |
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. |
One thing we could improve is the types of |
I think this might be more appropriate than my initial suggestion. |
Closing this as a duplicate of #594 and python/typeshed#4731. |
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 thetyping
library to improve type hinting in our code.Here's my stab at an implementation:
This would allow us to do the following:
I'm not sure if I have all the subtleties of the implementation correct, however my own cursory tests on python 3.9 work.
The text was updated successfully, but these errors were encountered: