> ## 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 type Statement

The `type` statement is a soft keyword introduced in Python 3.12 (PEP 695) used to declare type aliases. It provides a dedicated, native syntax for defining both generic and non-generic type aliases, replacing the older `typing.TypeAlias` assignment method while introducing lazy evaluation, implicit variance inference, and strict scoping.

## Syntax

The statement follows this general grammar:

```python theme={"dark"}
type AliasName = TypeExpression
type GenericAliasName[TypeParameters] = TypeExpression
```

## Technical Mechanics

**1. Underlying Object (`typing.TypeAliasType`)**
When the Python interpreter executes a `type` statement, it does not simply assign the right-hand side to the left-hand side. Instead, it creates an instance of `typing.TypeAliasType` and binds it to the `AliasName`.

```python theme={"dark"}
type Point = tuple[float, float]

print(type(Point))  # <class 'typing.TypeAliasType'>
```

**2. Lazy Evaluation**
The `TypeExpression` on the right-hand side is lazily evaluated. The Python compiler wraps the expression in an implicit function. This allows the alias to reference names that have not yet been defined (forward references) without raising a `NameError` at runtime, eliminating the need for string literals.

The actual value is only evaluated when explicitly requested via the `__value__` attribute.

```python theme={"dark"}
type NodeList = list[Node]  # 'Node' is not yet defined

class Node:
    pass


# Evaluation happens here
print(NodeList.__value__)  # list[__main__.Node]
```

**3. Type Parameter Scoping**
When defining generic type aliases, the `type` statement introduces an **annotation scope**. Type parameters defined within the square brackets `[...]` are implicitly created and are strictly scoped to the `TypeExpression`. They do not leak into the surrounding module namespace.

```python theme={"dark"}
type Mapping[K, V] = dict[K, V]


# K and V are valid within the statement, but undefined outside

# print(K) -> NameError: name 'K' is not defined
```

**4. Implicit Variance**
Unlike the legacy `typing.TypeVar` instantiation which required explicit `covariant=True` or `contravariant=True` flags, type parameters declared via the `type` statement automatically infer their variance based on how they are used within the `TypeExpression`.

## Generic Type Parameter Syntax

The square bracket syntax supports standard `TypeVar` declarations, bound types, constrained types, `TypeVarTuple` (variadic generics), and `ParamSpec` (callable signatures).

* **Standard Type Variables:**

```python theme={"dark"}
type Vector[T] = list[T]
```

* **Bound Type Variables** (using `:` with a single type):

  Restricts the type variable to the specified type and its subclasses.

```python theme={"dark"}
type FloatList[T: float] = list[T]
```

* **Constrained Type Variables** (using `:` with a tuple of types):

  Restricts the type variable to exactly the specified types (no subclasses).

```python theme={"dark"}
type NumberList[T: (int, float)] = list[T]
```

* **TypeVarTuple** (using `*`):

```python theme={"dark"}
type Shape[*Ts] = tuple[*Ts]
```

* **ParamSpec** (using `**`):

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

type Callback[**P, R] = typing.Callable[P, R]
```

## Runtime Introspection Attributes

Instances created by the `type` statement expose specific dunder attributes for runtime introspection:

* `__name__`: The string name of the type alias.
* `__value__`: The evaluated type expression (triggers lazy evaluation).
* `__type_params__`: A tuple of the implicit type parameter objects (e.g., `TypeVar` instances) declared in the generic signature.

```python theme={"dark"}
type Matrix[T] = list[list[T]]

print(Matrix.__name__)         # Matrix
print(Matrix.__type_params__)  # (T,)
print(Matrix.__value__)        # list[list[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>
