Skip to content

Commit 5aacaa4

Browse files
committed
gh-149574: Document that is_typeddict, is_protocol, is_dataclass, isclass return False for generic aliases
1 parent 354ef33 commit 5aacaa4

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

Doc/library/dataclasses.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,8 @@ Module contents
498498
.. function:: is_dataclass(obj)
499499

500500
Return ``True`` if its parameter is a dataclass (including subclasses of a
501-
dataclass) or an instance of one, otherwise return ``False``.
501+
dataclass, but not including :ref:`generic specializations <types-generic-aliases>`)
502+
or an instance of one, otherwise return ``False``.
502503

503504
If you need to know if a class is an instance of a dataclass (and
504505
not a dataclass itself), then add a further check for ``not

Doc/library/inspect.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
416416
Return ``True`` if the object is a class, whether built-in or created in Python
417417
code.
418418

419+
This function returns ``False`` for :ref:`generic specializations <types-generic-aliases>` of classes,
420+
such as ``list[int]``.
421+
419422

420423
.. function:: ismethod(object)
421424

Doc/library/stdtypes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5858,7 +5858,8 @@ type and the :class:`bytes` data type:
58585858

58595859
``GenericAlias`` objects are instances of the class
58605860
:class:`types.GenericAlias`, which can also be used to create ``GenericAlias``
5861-
objects directly.
5861+
objects directly. Specializations of user-defined :ref:`generic classes <generic-classes>`
5862+
may not be instances of :class:`types.GenericAlias`, but they provide similar functionality.
58625863

58635864
.. describe:: T[X, Y, ...]
58645865

Doc/library/typing.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,6 +3642,15 @@ Introspection helpers
36423642
is_protocol(P) # => True
36433643
is_protocol(int) # => False
36443644

3645+
This function only returns true for ``Protocol`` classes, not for
3646+
:ref:`generic specializations <types-generic-aliases>` of them::
3647+
3648+
class GenericP[T](Protocol):
3649+
def a(self) -> T: ...
3650+
b: int
3651+
3652+
assert not is_protocol(GenericP[int])
3653+
36453654
.. versionadded:: 3.13
36463655

36473656
.. function:: is_typeddict(tp)
@@ -3663,6 +3672,15 @@ Introspection helpers
36633672
# not a typed dict itself
36643673
assert not is_typeddict(TypedDict)
36653674

3675+
This function only returns true for ``TypedDict`` classes, not for
3676+
:ref:`generic specializations <types-generic-aliases>` of them::
3677+
3678+
class GenericFilm[T](TypedDict):
3679+
title: str
3680+
year: T
3681+
3682+
assert not is_typeddict(GenericFilm[int])
3683+
36663684
.. versionadded:: 3.10
36673685

36683686
.. class:: ForwardRef

0 commit comments

Comments
 (0)