Skip to content

Commit 8bf99ae

Browse files
authored
gh-119180: Document the format parameter in typing.get_type_hints() (#143758)
Do not mention `__annotations__` dictionaries, as this is slightly outdated since 3.14. Rewrite the note about possible exceptions for clarity. Also do not mention imported type aliases, as since 3.12 aliases with the `type` statement do not suffer from this limitation anymore.
1 parent 75ff1af commit 8bf99ae

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

Doc/library/typing.rst

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3453,13 +3453,13 @@ Functions and decorators
34533453
Introspection helpers
34543454
---------------------
34553455

3456-
.. function:: get_type_hints(obj, globalns=None, localns=None, include_extras=False)
3456+
.. function:: get_type_hints(obj, globalns=None, localns=None, include_extras=False, *, format=Format.VALUE)
34573457

34583458
Return a dictionary containing type hints for a function, method, module,
34593459
class object, or other callable object.
34603460

3461-
This is often the same as ``obj.__annotations__``, but this function makes
3462-
the following changes to the annotations dictionary:
3461+
This is often the same as :func:`annotationlib.get_annotations`, but this
3462+
function makes the following changes to the annotations dictionary:
34633463

34643464
* Forward references encoded as string literals or :class:`ForwardRef`
34653465
objects are handled by evaluating them in *globalns*, *localns*, and
@@ -3473,29 +3473,28 @@ Introspection helpers
34733473
annotations from ``C``'s base classes with those on ``C`` directly. This
34743474
is done by traversing :attr:`C.__mro__ <type.__mro__>` and iteratively
34753475
combining
3476-
``__annotations__`` dictionaries. Annotations on classes appearing
3477-
earlier in the :term:`method resolution order` always take precedence over
3478-
annotations on classes appearing later in the method resolution order.
3476+
:term:`annotations <variable annotation>` of each base class. Annotations
3477+
on classes appearing earlier in the :term:`method resolution order` always
3478+
take precedence over annotations on classes appearing later in the method
3479+
resolution order.
34793480
* The function recursively replaces all occurrences of
34803481
``Annotated[T, ...]``, ``Required[T]``, ``NotRequired[T]``, and ``ReadOnly[T]``
34813482
with ``T``, unless *include_extras* is set to ``True`` (see
34823483
:class:`Annotated` for more information).
34833484

3484-
See also :func:`annotationlib.get_annotations`, a lower-level function that
3485-
returns annotations more directly.
3486-
34873485
.. caution::
34883486

34893487
This function may execute arbitrary code contained in annotations.
34903488
See :ref:`annotationlib-security` for more information.
34913489

34923490
.. note::
34933491

3494-
If any forward references in the annotations of *obj* are not resolvable
3495-
or are not valid Python code, this function will raise an exception
3496-
such as :exc:`NameError`. For example, this can happen with imported
3497-
:ref:`type aliases <type-aliases>` that include forward references,
3498-
or with names imported under :data:`if TYPE_CHECKING <TYPE_CHECKING>`.
3492+
If :attr:`Format.VALUE <annotationlib.Format.VALUE>` is used and any
3493+
forward references in the annotations of *obj* are not resolvable, a
3494+
:exc:`NameError` exception is raised. For example, this can happen
3495+
with names imported under :data:`if TYPE_CHECKING <TYPE_CHECKING>`.
3496+
More generally, any kind of exception can be raised if an annotation
3497+
contains invalid Python code.
34993498

35003499
.. note::
35013500

@@ -3513,6 +3512,10 @@ Introspection helpers
35133512
if a default value equal to ``None`` was set.
35143513
Now the annotation is returned unchanged.
35153514

3515+
.. versionchanged:: 3.14
3516+
Added the ``format`` parameter. See the documentation on
3517+
:func:`annotationlib.get_annotations` for more information.
3518+
35163519
.. versionchanged:: 3.14
35173520
Calling :func:`get_type_hints` on instances is no longer supported.
35183521
Some instances were accepted in earlier versions as an undocumented

0 commit comments

Comments
 (0)