Excellent question! Yes, that is precisely the correct and idiomatic way to do it in Python.
Your syntax class Super(Base[X], Generic[Y]): correctly accomplishes two things simultaneously:
Base[X]: It signifies that Super inherits from Base and propagates the generic type X from the parent. This means Super is still generic over whatever X was in Base.
Generic[Y]: It introduces a new generic type, Y, that is specific to the Super subclass.
Complete Example
Let's build a full, working example to demonstrate this. We'll create a Base class that is generic over a type X (representing a primary value) and a Super class that inherits X and adds its own generic type Y (representing some extra data).
# 1. Import necessary tools from the typing module
from typing import TypeVar, Generic, Type
# 2. Define the TypeVars
# This will be the type for the Base class
X = TypeVar("X")
# This will be the new type for the Super subclass
Y = TypeVar("Y")
# 3. Define the generic base class
class Base(Generic[X]):
def __init__(self, value: X):
self.value: X = value
def get_value(self) -> X:
return self.value
def get_value_type(self) -> Type[X]:
return type(self.value)
# 4. Define the subclass using your proposed syntax
# It inherits from Base[X] and adds a new generic, Generic[Y]
class Super(Base[X], Generic[Y]):
def __init__(self, value: X, extra_data: Y):
# Initialize the parent class part
super().__init__(value)
# Initialize the new part for this subclass
self.extra_data: Y = extra_data
def get_extra_data(self) -> Y:
return self.extra_data
# --- Usage Example ---
# Instantiate the Super class with concrete types: X=int, Y=str
instance = Super[int, str](value=101, extra_data="status_ok")
# --- Verification ---
# Access the value inherited from Base
# Type checkers know this is an int
val: int = instance.get_value()
print(f"Value: {val} (Type: {type(val)})")
# Access the extra data from Super
# Type checkers know this is a str
extra: str = instance.get_extra_data()
print(f"Extra Data: {extra} (Type: {type(extra)})")
# Let's check the type hints with a static type checker like mypy.
# If you run `mypy your_script.py`, it will pass with no errors.
# --- What would a type error look like? ---
# This line would cause a mypy error because "hello" is not an int
# error: Argument "value" to "Super" has incompatible type "str"; expected "int"
# bad_instance = Super[int, str](value="hello", extra_data="status_fail")
# This line would also cause a mypy error because 123 is not a str
# error: Argument "extra_data" to "Super" has incompatible type "int"; expected "str"
# another_bad_instance = Super[int, str](value=200, extra_data=123)
Output of the Correct Code
Value: 101 (Type: <class 'int'>)
Extra Data: status_ok (Type: <class 'str'>)
Why this Works
This pattern is essentially multiple inheritance. You are inheriting from two "parent" classes:
- A specialized version of your actual parent class (
Base[X]).
- The special
typing.Generic class, which is used to introduce new type variables to the class definition.
The Python type system is designed to recognize this specific pattern and combine the type variables from all generic bases, resulting in Super being correctly understood as Super[X, Y].