On this page
  1. typing.Generic
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

typing.Generic

Prev: typing.Generator | Up: typing | Next: typing.get_args
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.Generic
Tags: #public #python3 #stdlib #typing


Import

import typing
from typing import Generic

Signature & Overview

typing.Generic()

Abstract base class for generic types.

Usage

from typing import Generic, TypeVar

T = TypeVar("T")

class Box(Generic[T]):
    def __init__(self, value: T) -> None:
        self.value = value

b: Box[int] = Box(42)
assert b.value == 42

Gotchas

REPL Inspect

from typing import Generic
help(Generic)

Linked from

Related notes