]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-149574: Document that is_typeddict, is_protocol, is_dataclass, isclass...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 13 May 2026 03:07:24 +0000 (05:07 +0200)
committerGitHub <noreply@github.com>
Wed, 13 May 2026 03:07:24 +0000 (03:07 +0000)
gh-149574: Document that is_typeddict, is_protocol, is_dataclass, isclass return False for generic aliases (GH-149604)
(cherry picked from commit a4e51c8dac9fdd49ae26ff8c6cd3c808fd8ba15e)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Doc/library/dataclasses.rst
Doc/library/inspect.rst
Doc/library/stdtypes.rst
Doc/library/typing.rst

index 43efb739a77c31379db7f6093bf8ea5db9f0ba47..c163516eae9963f7eb59745f1f786480f2195584 100644 (file)
@@ -492,7 +492,8 @@ Module contents
 .. function:: is_dataclass(obj)
 
    Return ``True`` if its parameter is a dataclass (including subclasses of a
-   dataclass) or an instance of one, otherwise return ``False``.
+   dataclass, but not including :ref:`generic aliases <types-genericalias>`)
+   or an instance of one, otherwise return ``False``.
 
    If you need to know if a class is an instance of a dataclass (and
    not a dataclass itself), then add a further check for ``not
index 8a7cdb04f5f269bcce9f3effbf1a00146d965a25..00e1d77980e080497f4114fbf419ec2d5633bffd 100644 (file)
@@ -365,6 +365,9 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
    Return ``True`` if the object is a class, whether built-in or created in Python
    code.
 
+   This function returns ``False`` for :ref:`generic aliases <types-genericalias>` of classes,
+   such as ``list[int]``.
+
 
 .. function:: ismethod(object)
 
index 13275dc760f815693a6b38a94b79b81f258d88da..20e021ec4842b86e852bbd80acad9d5bd2ee30cc 100644 (file)
@@ -5527,7 +5527,8 @@ type and the :class:`bytes` data type:
 
 ``GenericAlias`` objects are instances of the class
 :class:`types.GenericAlias`, which can also be used to create ``GenericAlias``
-objects directly.
+objects directly. Specializations of user-defined :ref:`generic classes <generic-classes>`
+may not be instances of :class:`types.GenericAlias`, but they provide similar functionality.
 
 .. describe:: T[X, Y, ...]
 
index 8569e2e4491f00a857ae3e5ca570c652a5e3602c..0dc71d479592eb47a3a56d88a30c56b86a2037d6 100644 (file)
@@ -3340,14 +3340,27 @@ Introspection helpers
 
    Determine if a type is a :class:`Protocol`.
 
-   For example::
+   For example:
+
+   .. testcode::
 
       class P(Protocol):
           def a(self) -> str: ...
           b: int
 
-      is_protocol(P)    # => True
-      is_protocol(int)  # => False
+      assert is_protocol(P)
+      assert not is_protocol(int)
+
+   This function only returns true for ``Protocol`` classes, not for
+   :ref:`generic aliases <types-genericalias>` of them:
+
+   .. testcode::
+
+      class GenericP[T](Protocol):
+          def a(self) -> T: ...
+          b: int
+
+      assert not is_protocol(GenericP[int])
 
    .. versionadded:: 3.13
 
@@ -3370,6 +3383,17 @@ Introspection helpers
       # not a typed dict itself
       assert not is_typeddict(TypedDict)
 
+   This function only returns true for ``TypedDict`` classes, not for
+   :ref:`generic aliases <types-genericalias>` of them:
+
+   .. testcode::
+
+      class GenericFilm[T](TypedDict):
+          title: str
+          year: T
+
+      assert not is_typeddict(GenericFilm[int])
+
    .. versionadded:: 3.10
 
 .. class:: ForwardRef