From 59749a34e4d2459ea0e26be45513f8cc525cb62e Mon Sep 17 00:00:00 2001 From: Alex Kautz Date: Tue, 20 May 2025 16:07:45 -0400 Subject: [PATCH 1/3] Added clarification on instances annotations Instances of classes cannot have annotations, however sometimes they will erroneously have the __annotations__ attribute --- Doc/glossary.rst | 2 ++ Doc/howto/annotations.rst | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index c5c7994f1262a9..3d3f5db1a44fca 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -52,6 +52,8 @@ Glossary can be retrieved by calling :func:`annotationlib.get_annotations` on modules, classes, and functions, respectively. + Note that an *instance* of a :term:`class` cannot be annotated. + See :term:`variable annotation`, :term:`function annotation`, :pep:`484`, :pep:`526`, and :pep:`649`, which describe this functionality. Also see :ref:`annotations-howto` diff --git a/Doc/howto/annotations.rst b/Doc/howto/annotations.rst index 78f3704ba5d000..e44acdacd32d09 100644 --- a/Doc/howto/annotations.rst +++ b/Doc/howto/annotations.rst @@ -249,3 +249,8 @@ quirks by using :func:`annotationlib.get_annotations` on Python 3.14+ or Python, you can avoid these bugs by accessing the annotations from the class's :attr:`~type.__dict__` (e.g., ``cls.__dict__.get('__annotations__', None)``). + +In some versions of Python, instances of classes may have an ``__annotations__`` +attribute. This is not supported functionality, so the best practice is to always +interact with the ``__annotations__`` attribute of an instance's type. For example +``type(myinstance).__annotations__``. From 8b94a31f34538ce75a7913fd17b8a4519822e75f Mon Sep 17 00:00:00 2001 From: Alex Kautz Date: Tue, 20 May 2025 23:03:43 -0400 Subject: [PATCH 2/3] Removed unneeded confusing line --- Doc/glossary.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index 3d3f5db1a44fca..c5c7994f1262a9 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -52,8 +52,6 @@ Glossary can be retrieved by calling :func:`annotationlib.get_annotations` on modules, classes, and functions, respectively. - Note that an *instance* of a :term:`class` cannot be annotated. - See :term:`variable annotation`, :term:`function annotation`, :pep:`484`, :pep:`526`, and :pep:`649`, which describe this functionality. Also see :ref:`annotations-howto` From 91144a4c7f28b04f74fc6863c994875fadb6938d Mon Sep 17 00:00:00 2001 From: Alex Kautz Date: Tue, 20 May 2025 23:38:56 -0400 Subject: [PATCH 3/3] Updated note about Instances --- Doc/howto/annotations.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/howto/annotations.rst b/Doc/howto/annotations.rst index e44acdacd32d09..d7deb6c6bc1768 100644 --- a/Doc/howto/annotations.rst +++ b/Doc/howto/annotations.rst @@ -248,9 +248,9 @@ quirks by using :func:`annotationlib.get_annotations` on Python 3.14+ or :func:`inspect.get_annotations` on Python 3.10+. On earlier versions of Python, you can avoid these bugs by accessing the annotations from the class's :attr:`~type.__dict__` -(e.g., ``cls.__dict__.get('__annotations__', None)``). +(for example, ``cls.__dict__.get('__annotations__', None)``). In some versions of Python, instances of classes may have an ``__annotations__`` -attribute. This is not supported functionality, so the best practice is to always -interact with the ``__annotations__`` attribute of an instance's type. For example -``type(myinstance).__annotations__``. +attribute. However, this is not supported functionality. If you need the +annotations of an instance, you can use :func:`type` to access its class +(for example, ``annotationlib.get_annotations(type(myinstance))`` on Python 3.14+).