Skip to content

Commit 054348e

Browse files
[3.14] gh-149574: Document that is_typeddict, is_protocol, is_dataclass, isclass return False for generic aliases (GH-149604) (#149751)
gh-149574: Document that is_typeddict, is_protocol, is_dataclass, isclass return False for generic aliases (GH-149604) (cherry picked from commit a4e51c8) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent d6b6fd7 commit 054348e

4 files changed

Lines changed: 34 additions & 5 deletions

File tree

Doc/library/dataclasses.rst

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

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

502503
If you need to know if a class is an instance of a dataclass (and
503504
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
@@ -399,6 +399,9 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
399399
Return ``True`` if the object is a class, whether built-in or created in Python
400400
code.
401401

402+
This function returns ``False`` for :ref:`generic aliases <types-genericalias>` of classes,
403+
such as ``list[int]``.
404+
402405

403406
.. function:: ismethod(object)
404407

Doc/library/stdtypes.rst

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

57845784
``GenericAlias`` objects are instances of the class
57855785
:class:`types.GenericAlias`, which can also be used to create ``GenericAlias``
5786-
objects directly.
5786+
objects directly. Specializations of user-defined :ref:`generic classes <generic-classes>`
5787+
may not be instances of :class:`types.GenericAlias`, but they provide similar functionality.
57875788

57885789
.. describe:: T[X, Y, ...]
57895790

Doc/library/typing.rst

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,14 +3480,27 @@ Introspection helpers
34803480

34813481
Determine if a type is a :class:`Protocol`.
34823482

3483-
For example::
3483+
For example:
3484+
3485+
.. testcode::
34843486

34853487
class P(Protocol):
34863488
def a(self) -> str: ...
34873489
b: int
34883490

3489-
is_protocol(P) # => True
3490-
is_protocol(int) # => False
3491+
assert is_protocol(P)
3492+
assert not is_protocol(int)
3493+
3494+
This function only returns true for ``Protocol`` classes, not for
3495+
:ref:`generic aliases <types-genericalias>` of them:
3496+
3497+
.. testcode::
3498+
3499+
class GenericP[T](Protocol):
3500+
def a(self) -> T: ...
3501+
b: int
3502+
3503+
assert not is_protocol(GenericP[int])
34913504

34923505
.. versionadded:: 3.13
34933506

@@ -3510,6 +3523,17 @@ Introspection helpers
35103523
# not a typed dict itself
35113524
assert not is_typeddict(TypedDict)
35123525

3526+
This function only returns true for ``TypedDict`` classes, not for
3527+
:ref:`generic aliases <types-genericalias>` of them:
3528+
3529+
.. testcode::
3530+
3531+
class GenericFilm[T](TypedDict):
3532+
title: str
3533+
year: T
3534+
3535+
assert not is_typeddict(GenericFilm[int])
3536+
35133537
.. versionadded:: 3.10
35143538

35153539
.. class:: ForwardRef

0 commit comments

Comments
 (0)