8000 gh-132426: Add get_annotate_from_class_namespace replacing get_annotate_function by JelleZijlstra · Pull Request #132490 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132426: Add get_annotate_from_class_namespace replacing get_annotate_function #132490

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

Merged
merged 4 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update more docs
  • Loading branch information
JelleZijlstra committed Apr 14, 2025
commit cddee44a8005343817a89d9ffe2ec543b4551ec2
11 changes: 5 additions & 6 deletions Doc/library/annotationlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,9 @@ Functions
.. function:: get_annotate_from_class_namespace(namespace)

Retrieve the :term:`annotate function` from a class namespace dictionary *namespace*.
Return :const:`!None` if the namespace does not contain an annotate function. *obj* may be a class, function,
module, or a namespace dictionary for a class. The last case is useful during
class creation, e.g. in the ``__new__`` method of a metaclass.

This is usually equivalent to accessing the :attr:`~object.__annotate__`
attribute of *obj*, but access through this public function is preferred.
Return :const:`!None` if the namespace does not contain an annotate function.
This is useful in :ref:`metaclasses <metaclasses>` to retrieve the annotate function;
see :ref:`below <annotationlib-metaclass>` for an example.

.. versionadded:: 3.14

Expand Down Expand Up @@ -411,6 +408,8 @@ Functions
Recipes
-------

.. _annotationlib-metaclass:

Using annotations in a metaclass
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
8 changes: 3 additions & 5 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,10 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):


def get_annotate_from_class_namespace(obj):
"""Get the __annotate__ function for an object.
"""Retrieve the annotate function from a class namespace dictionary.

obj may be a function, class, or module, or a user-defined type with
an `__annotate__` 5B5C attribute.

Returns the __annotate__ function or None.
Return None if the namespace does not contain an annotate function.
This is useful in metaclass ``__new__`` methods to retrieve the annotate function.
"""
try:
return obj["__annotate__"]
Expand Down
Loading
0