-
Notifications
You must be signed in to change notification settings - Fork 258
Add typing.Property #594
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
Regarding a) this is not really about |
FYI: if you wind up here, you can do this for now:
|
@earonesty Thank you for putting me on the right path... from typing import Optional, Union
from inspect import getattr_static
import unittest
class FooBar:
@property
def foobar(self) -> Optional[str]:
pass
@foobar.setter
def foobar(self, foobar_: str) -> None:
pass
class AnnotationsTest(unittest.TestCase):
def setUp(self):
self.foobar = FooBar()
def test_access_getter_annotations_with_inspect(self):
"""==> object_.fget.__annotations__ should return expected annotations"""
property_object = getattr_static(self.foobar, 'foobar')
self.assertEqual(property_object.fget.__annotations__, {'return': Union[str, None]})
def test_access_setter_annotations_with_inspect(self):
"""==> object_.fset.__annotations__ should return expected annotations"""
property_object = getattr_static(self.foobar, 'foobar')
self.assertEqual(property_object.fset.__annotations__, {'foobar_': str, 'return': None})
def test_access_getter_annotations_with_type_getattribute(self):
"""==> object_.fget.__annotations__ should return expected annotations"""
property_object = type.__getattribute__(type(self.foobar), 'foobar')
self.assertEqual(property_object.fget.__annotations__, {'return': Union[str, None]})
def test_access_setter_annotations_with_type_getattribute(self):
"""==> object_.fset.__annotations__ should return expected annotations"""
property_object = type.__getattribute__(type(self.foobar), 'foobar')
self.assertEqual(property_object.fset.__annotations__, {'foobar_': str, 'return': None})
if __name__ == '__main__':
unittest.main(verbosity=2) |
Could we have a generic Property class added to from typing import Property
class C:
def __init__(self, prop: int):
self._prop = prop
@Property[int]
def prop(self):
return self._prop Here's the gist of the class I'm proposing (details will likely need to be refined): T_co = TypeVar('T_co', covariant=True)
class Property(property, Generic[T_co]):
def fget(self) -> T_co:
return cast(T_co, super().fget())
def fset(self, value: T_co) -> None:
super().fset(value)
def fdel(self) -> None:
super().fdel()
def getter(self, fget: Callable[[Any], T_co]) -> 'Property[T_co]':
return cast(Property[T_co], super().getter(fget))
def setter(self, fset: Callable[[Any, T_co], None]) -> 'Property[T_co]':
return cast(Property[T_co], super().setter(fset))
def deleter(self, fdel: Callable[[Any], None]) -> 'Property[T_co]':
return cast(Property[T_co], super().deleter(fdel)) |
I mean for me it would make sense the most to actually annotate the return type for the function the property calls, and maybe the property could automatically adapt to that? @property
def foo(...) -> int: to Union[property, int] or that a new type idea Property[int] Like as long as you annotate the function under the |
Here's a real-world example for where a generic property is required (screenshots included for easier reference).
Since in #985 it considered too difficult to make ![]() ![]() |
I don't think we will add |
Currently there is no type for the values @Property returns.
a) the returned value of
property
has notyping
equivalentb) There is no consistency in how properties in classes work in comparison to normal variables or functions:
The text was updated successfully, but these errors were encountered: