[issue40606] Copy property return annotations to __annotations__

Eric Wieser report at bugs.python.org
Tue May 12 05:11:47 EDT 2020


New submission from Eric Wieser <wieser.eric at gmail.com>:

Consider a class like

    class MyClass:
        x: int
        y: int

Which has

    >>> MyClass.__annotations__
    {'x': int, 'y': int}

In future, it might change to

    class MyClass:
        @property
        def x(self) -> int:
            ...
        @functools.cached_property
        def x(self) -> int:
            ...

Most code won't be able to tell the difference, as properties are already a mostly-transparent replacement for attributes - but any code looking at `__annotations__` will find it is now absent.

It would be handy if `property.__set_name__` and `cachedproperty.__set_name__` could populate the `__annotations__` dict from their return type annotation.

This isn't just hypothetically useful - `sphinx` master as of https://github.com/sphinx-doc/sphinx/pull/7564 is able to retrieve the type of any descriptor with this behavior.

----------
components: Library (Lib)
messages: 368710
nosy: Eric Wieser
priority: normal
severity: normal
status: open
title: Copy property return annotations to __annotations__
type: enhancement
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40606>
_______________________________________


More information about the Python-bugs-list mailing list