python / python/cpython

Document that `inspect.isclass` returns False for `GenericAlias` instances

Abierto
#132,467 9 comentarios 0 reacciones 1 asignado Ver en GitHub

@AbduazizZiyodov ya está trabajando en esto.

Desde el 13/4/2025.

docs easy
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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
  • gh-133504
  • gh-150854
  • gh-151116
  • gh-151117

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.