Skip to content

Commit e234662

Browse files
authored
pythongh-144190: Clarify get_type_hints() instance behavior in docs (python#144831)
1 parent aa2c0c3 commit e234662

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Doc/library/typing.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,8 +3353,8 @@ Introspection helpers
33533353

33543354
.. function:: get_type_hints(obj, globalns=None, localns=None, include_extras=False)
33553355

3356-
Return a dictionary containing type hints for a function, method, module
3357-
or class object.
3356+
Return a dictionary containing type hints for a function, method, module,
3357+
class object, or other callable object.
33583358

33593359
This is often the same as ``obj.__annotations__``, but this function makes
33603360
the following changes to the annotations dictionary:
@@ -3395,6 +3395,13 @@ Introspection helpers
33953395
:ref:`type aliases <type-aliases>` that include forward references,
33963396
or with names imported under :data:`if TYPE_CHECKING <TYPE_CHECKING>`.
33973397

3398+
.. note::
3399+
3400+
Calling :func:`get_type_hints` on an instance is not supported.
3401+
To retrieve annotations for an instance, call
3402+
:func:`get_type_hints` on the instance's class instead
3403+
(for example, ``get_type_hints(type(obj))``).
3404+
33983405
.. versionchanged:: 3.9
33993406
Added ``include_extras`` parameter as part of :pep:`593`.
34003407
See the documentation on :data:`Annotated` for more information.
@@ -3404,6 +3411,11 @@ Introspection helpers
34043411
if a default value equal to ``None`` was set.
34053412
Now the annotation is returned unchanged.
34063413

3414+
.. versionchanged:: 3.14
3415+
Calling :func:`get_type_hints` on instances is no longer supported.
3416+
Some instances were accepted in earlier versions as an undocumented
3417+
implementation detail.
3418+
34073419
.. function:: get_origin(tp)
34083420

34093421
Get the unsubscripted version of a type: for a typing object of the form

0 commit comments

Comments
 (0)