reportInvalidTypeArguments.md
April 8, 2026 · View on GitHub
Overview
reportInvalidTypeArguments is a diagnostic that flags incorrect or missing type arguments in generic classes, functions, or type aliases. This helps ensure that generics are used properly and that type safety is maintained throughout your code.
Representative Issues
- #7536: When subclassing a generic class, include type variables to preserve generics.
- #8112: Ensure generic type arguments are correctly constrained, especially with unions.
- #8942: Prefer class-based syntax for TypedDict with generics.
- #9641: Avoid circular dependencies between type aliases and class definitions.
Examples
Error:
from typing import Generic, TypeVar
T = TypeVar("T")
class Box(Generic[T]):
def __init__(self, item: T) -> None:
self.item = item
b: Box[int, str] = Box(1) # Too many type arguments (expected 1)
Fix — supply the correct number of type arguments:
b: Box[int] = Box(1)
Or for constrained type variables:
T = TypeVar("T", int, str)
class Box(Generic[T]):
pass
b: Box[float] = Box() # 'float' not in constraints [int, str]
# Fix: use an allowed type
b: Box[int] = Box()
Common Fixes & Workarounds
- Always specify the correct type arguments when using generics.
- Use class-based syntax for generic TypedDicts.
- Refactor code to avoid circular dependencies between type aliases and classes.
- Review and update type constraints for generic parameters.
- See the Pyright configuration documentation for details on configuring this rule.
See Also
python.analysis.diagnosticSeverityOverrides— adjust or suppress this diagnosticpython.analysis.typeCheckingMode— controls which diagnostics are enabled by default