Skip to content

Commit 032b1be

Browse files
[3.14] gh-119180: Document the format parameter in typing.get_type_hints() (GH-143758) (#148901)
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. (cherry picked from commit 8bf99ae) Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
1 parent 0a63bb8 commit 032b1be

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
@@ -3346,13 +3346,13 @@ Functions and decorators
33463346
Introspection helpers
33473347
---------------------
33483348

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

33513351
Return a dictionary containing type hints for a function, method, module,
33523352
class object, or other callable object.
33533353

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

33573357
* Forward references encoded as string literals or :class:`ForwardRef`
33583358
objects are handled by evaluating them in *globalns*, *localns*, and
@@ -3366,29 +3366,28 @@ Introspection helpers
33663366
annotations from ``C``'s base classes with those on ``C`` directly. This
33673367
is done by traversing :attr:`C.__mro__ <type.__mro__>` and iteratively
33683368
combining
3369-
``__annotations__`` dictionaries. Annotations on classes appearing
3370-
earlier in the :term:`method resolution order` always take precedence over
3371-
annotations on classes appearing later in the method resolution order.
3369+
:term:`annotations <variable annotation>` of each base class. Annotations
3370+
on classes appearing earlier in the :term:`method resolution order` always
3371+
take precedence over annotations on classes appearing later in the method
3372+
resolution order.
33723373
* The function recursively replaces all occurrences of
33733374
``Annotated[T, ...]``, ``Required[T]``, ``NotRequired[T]``, and ``ReadOnly[T]``
33743375
with ``T``, unless *include_extras* is set to ``True`` (see
33753376
:class:`Annotated` for more information).
33763377

3377-
See also :func:`annotationlib.get_annotations`, a lower-level function that
3378-
returns annotations more directly.
3379-
33803378
.. caution::
33813379

33823380
This function may execute arbitrary code contained in annotations.
33833381
See :ref:`annotationlib-security` for more information.
33843382

33853383
.. note::
33863384

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

33933392
.. note::
33943393

@@ -3406,6 +3405,10 @@ Introspection helpers
34063405
if a default value equal to ``None`` was set.
34073406
Now the annotation is returned unchanged.
34083407

3408+
.. versionchanged:: 3.14
3409+
Added the ``format`` parameter. See the documentation on
3410+
:func:`annotationlib.get_annotations` for more information.
3411+
34093412
.. versionchanged:: 3.14
34103413
Calling :func:`get_type_hints` on instances is no longer supported.
34113414
Some instances were accepted in earlier versions as an undocumented

0 commit comments

Comments
 (0)