Repository navigation
Closed as not planned
Labels
topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations
Description
Currently there is no type for the values @Property returns.
a) the returned value of property has no typing equivalent
def create_property() -> ???:
some_really_bad_code = True
def setter(value):
some_really_bad_code = value
def getter():
return some_really_bad_code
def deller():
some_really_bad_code = None
return property(getter, setter, deller)b) There is no consistency in how properties in classes work in comparison to normal variables or functions:
class Example:
foo: str
def bar(self, value: str) -> str:
return value
# end def
@property
def batz(self) -> str:
return 'test2'
# end def
@batz.setter
def batz(self, value: str):
pass
# end def
# end class
# variable
>>> typing.get_type_hints(Example)
{'foo': <class 'str'>}
>>> Example.__annotations__
{'foo': <class 'str'>}
# function
>>> typing.get_type_hints(Example.bar)
{'value': <class 'str'>, 'return': <class 'str'>}
>>> Example.bar.__annotations__
{'value': <class 'str'>, 'return': <class 'str'>}
# property
# not in the class's hints
>>> 'batz' in typing.get_type_hints(Example)
False
>>> typing.get_type_hints(Example.batz)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/typing.py", line 1527, in get_type_hints
'or function.'.format(obj))
TypeError: <property object at 0x10e81b868> is not a module, class, method, or function.
>>> Example.batz.__annotations__
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'property' object has no attribute '__annotations__'Metadata
Metadata
Assignees
Labels
topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations
Activity
ilevkivskyi commentedon Nov 20, 2018
Regarding a) this is not really about
typing, technically property is a class, so you can annotate your decorator as just-> property. Some type checkers may be not happy about this, but then it should be discussed per type checker.About b) we can add some special casing for
propertybut this is a more general problem, unless decorator copies the__annotations__attribute, we can't do much.[-]@property support[/-][+]Can't access __annotations__ on a @property[/+]earonesty commentedon May 8, 2020
FYI: if you wind up here, you can do this for now:
TangoMan75 commentedon May 13, 2020
@earonesty Thank you for putting me on the right path...
I was looking for this trick for so long, I think I should share it here.
Before reaching
fgetorfsetattributes from a property, we need to access descriptor statically either frominspect.getattr_static()ortype.__getattribute__(), otherwise Python would raiseAttributeError: 'FooBar' object has no attribute "fget"since it is trying to access 'fget' on property return value instead of the object itself.jp-larose commentedon Sep 3, 2020
Could we have a generic Property class added to
typing? PEP 614 would allow something like this:Here's the gist of the class I'm proposing (details will likely need to be refined):
luckydonald commentedon Sep 3, 2020
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?
to
or that a new type idea
Like as long as you annotate the function under the
@propertythat could automatically be used?[-]Can't access __annotations__ on a @property[/-][+]Add typing.Property[/+]jace commentedon Jun 3, 2024
Here's a real-world example for where a generic property is required (screenshots included for easier reference).
Response.varyisproperty[HeaderSet], but without the ability to provide a generic type, static type checkers interpret it asAny. When I useresponse.vary.add('Accept')in my code, there's no type check for theaddmethod:Since in #985 it considered too difficult to make
builtins.propertygeneric, a substitutetyping.Propertywill be most excellent.https://github.com/pallets/werkzeug/blob/3.0.3/src/werkzeug/sansio/response.py#L40-L61
srittau commentedon Jun 3, 2024
I don't think we will add
typing.propertyifbuiltins.propertyexists. There has been a trend towards deprecating this kind of duplication, i.e. making built-in types generic. This would be the correct course of action forpropertyas well, even if it proves difficult.