Skip to content

Document that inspect.isclass returns False for GenericAlias instances #132467

Closed

Description

@RobertoPrevato

Bug report

Bug description:

Hi
I am not sure if this is a bug, but I use the bug template because I couldn't find a better one to submit questions like this one.

The documentation says, "User-defined generic classes can be instantiated.".

import inspect
from typing import TypeVar, Generic

T = TypeVar("T")


class Node(Generic[T]):
    x: T  # Instance attribute (see below)

    def __init__(self, label: T | None = None) -> None:
        self.label = label


x = Node("")  # Inferred type is Node[str]
y = Node(0)  # Inferred type is Node[int]
z = Node()  # Inferred type is Node[Any]


# Also OK:
y = Node[int](0)


print(inspect.isclass(Node))  # True
print(inspect.isclass(Node[int]))  # False  (shouldn't this be True?)
print(inspect.isclass(Node[str]))  # False  (shouldn't this be True?)

In the example above, shouldn't calls such as inspect.isclass(Node[int]) return True, since they can be instantiated and used like normal classes?

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Activity

  1. AbduazizZiyodov commented on Apr 13, 2025

    @AbduazizZiyodov
    Contributor

    @sobolevn Can you assign me to this issue ? I will work on it

  2. sobolevn commented on Apr 13, 2025

    @sobolevn
    Member

    @AbduazizZiyodov sure, please go ahead :)

  3. AbduazizZiyodov commented on May 3, 2025

    @AbduazizZiyodov
    Contributor

    @sobolevn: I also agree that changing anything in inspect.is* can break a lot of code. And it is easier to adapt any checks in your own code: inspect.isclass(obj) or isinstance(obj, GenericAlias) - for example.

    isinstance(Node[int], GenericAlias) is also returning False:

    # Definition of generic Node is same
    >>> inspect.isclass(Node[int])
    False # ok
    >>> from types import GenericAlias
    >>> inspect.isclass(Node[int]) or isinstance(Node[int], GenericAlias)
    False # ?
    >>> isinstance(Node[int], GenericAlias)
    False # ?

    Version: Python 3.14.0a7+, is that expected behaviour?

  4. AbduazizZiyodov commented on May 3, 2025

    @AbduazizZiyodov
    Contributor

    But this behaves as expected:

    >>> from typing import _GenericAlias as GenericAlias
    >>> isinstance(Node[int], GenericAlias)
    True

    I see that, there was discussion about types.GenericAlias VS typing._GenericAlias. As I understand this is documented in code, should we document in online docs ?

  5. AbduazizZiyodov commented on May 6, 2025

    @AbduazizZiyodov
    Contributor
  6. added 2 commits that reference this issue on Jun 3, 2026
  7. added a commit that references this issue on Jun 8, 2026
  8. added a commit that references this issue on Jun 9, 2026
  9. added a commit that references this issue on Jun 12, 2026
  10. added a commit that references this issue on Jun 17, 2026
  11. added a commit that references this issue on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

docsDocumentation in the Doc direasy

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions