> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python TypeVar

`typing.TypeVar` is a class used to instantiate type variables for generic type hinting in Python. It acts as a placeholder for a specific, yet-to-be-determined type that must remain consistent across multiple evaluations within a given static typing scope, such as a function signature or a generic class definition.

```python theme={"dark"}
from typing import TypeVar

T = TypeVar('T')
```

## Signature and Parameters

The `TypeVar` constructor accepts a mandatory name and several optional arguments to restrict type resolution and define variance.

```python theme={"dark"}
TypeVar(name, *constraints, bound=None, covariant=False, contravariant=False, infer_variance=False)
```

**`name` (str)**
A string literal that must exactly match the name of the variable to which the `TypeVar` is assigned. This is required by the Python runtime for introspection, `__name__` resolution, and pickling.

**`*constraints` (type)**
Positional arguments that restrict the type variable to a discrete, closed set of allowed types. When constraints are provided, the static type checker forces the resolved type to be exactly one of the specified types, not a subclass.

```python theme={"dark"}

# Can only resolve to exactly `str` or exactly `bytes`
AnyStr = TypeVar('AnyStr', str, bytes) 
```

**`bound` (type)**
A keyword argument that establishes an upper bound for the type variable. The resolved type must be the specified bounding type or a subclass of that bounding type. Unlike `*constraints`, `bound` allows for continuous subtype resolution.

```python theme={"dark"}
class Shape: pass


# Can resolve to Shape or any subclass of Shape
TShape = TypeVar('TShape', bound=Shape) 
```

**`covariant` (bool)**
A keyword argument that marks the type variable as covariant. Covariance indicates that a generic type `Generic[T]` preserves the subtyping relationship of its type arguments. If `Derived` is a subtype of `Base`, then `Generic[Derived]` is recognized as a subtype of `Generic[Base]`.

```python theme={"dark"}
T_co = TypeVar('T_co', covariant=True)
```

**`contravariant` (bool)**
A keyword argument that marks the type variable as contravariant. Contravariance reverses the subtyping relationship. If `Derived` is a subtype of `Base`, then `Generic[Base]` is recognized as a subtype of `Generic[Derived]`.

```python theme={"dark"}
T_contra = TypeVar('T_contra', contravariant=True)
```

**`infer_variance` (bool)**
Introduced in Python 3.12, this keyword argument instructs static type checkers (like Mypy or Pyright) to automatically infer the variance (invariant, covariant, or contravariant) based on how the type variable is structurally utilized within a generic class.

```python theme={"dark"}
T_infer = TypeVar('T_infer', infer_variance=True)
```

## Python 3.12+ Type Parameter Syntax (PEP 695)

As of Python 3.12, explicit instantiation of `TypeVar` via the `typing` module is largely superseded by the native type parameter syntax. This syntax automatically creates the underlying `TypeVar` objects at runtime and implicitly handles variance inference.

```python theme={"dark"}

# Implicit TypeVar 'T'
def process[T](item: T) -> T: ...


# Implicit TypeVar 'T' with a bound
class Container[T: Shape]: ...


# Implicit TypeVar 'T' with constraints
def concatenate[T: (str, bytes)](a: T, b: T) -> T: ...
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
