> ## 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 TypedDict

`TypedDict` is a type hint construct from the `typing` module used to specify the expected types of values associated with specific string keys in a dictionary. It provides structural type information to static type checkers (such as `mypy` or `pyright`) without altering the runtime behavior of the dictionary. At runtime, an instance of a `TypedDict` is a standard Python `dict`, and no type validation or enforcement occurs.

## Syntax

`TypedDict` can be defined using two syntaxes: the class-based syntax (preferred for readability) and the alternative assignment syntax (required when dictionary keys are not valid Python identifiers or are reserved keywords).

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


# Class-based syntax
class Point2D(TypedDict):
    x: int
    y: int
    label: str


# Alternative assignment syntax
Point3D = TypedDict('Point3D', {'x': int, 'y': int, 'z': int, 'in-bounds': bool})
```

## Runtime Behavior

`TypedDict` is strictly a static typing tool subject to type erasure. Instantiating a `TypedDict` creates a standard dictionary. It does not enforce types, restrict extra keys, or provide default values during execution.

```python theme={"dark"}

# Static type checkers will flag the incorrect type for 'y' and the missing 'label'.

# However, the Python interpreter will execute this without raising an exception.
p: Point2D = {'x': 10, 'y': 'twenty'} 

print(type(p))  # Output: <class 'dict'>
```

## Totality and Key Presence

By default, a `TypedDict` is **total**, meaning every key defined in the type definition must be present in the dictionary instance. You can modify this behavior at the class level using the `total` parameter, or at the granular key level using `Required` and `NotRequired` (introduced in Python 3.11 via PEP 655).

### Class-Level Totality

Setting `total=False` makes all keys in the dictionary optional.

```python theme={"dark"}
class Config(TypedDict, total=False):
    timeout: int
    retries: int


# Valid: Neither key is strictly required by the type checker
c: Config = {}
```

### Key-Level Totality (Python 3.11+)

`NotRequired` marks specific keys as optional within a total `TypedDict`. Conversely, `Required` marks specific keys as mandatory within a non-total (`total=False`) `TypedDict`.

```python theme={"dark"}
from typing import TypedDict, NotRequired, Required

class User(TypedDict):
    username: str
    email: str
    bio: NotRequired[str]  # Optional key

class PartialUpdate(TypedDict, total=False):
    username: str
    email: str
    id: Required[int]      # Mandatory key
```

## Inheritance

`TypedDict` supports multiple inheritance, allowing the composition of complex dictionary types from simpler ones. A subclass inherits all key-type pairs from its base classes. If multiple base classes define the same key, the type annotations for that key must match exactly, otherwise static type checkers will report a conflict.

```python theme={"dark"}
class BaseItem(TypedDict):
    id: int
    name: str

class PhysicalItem(BaseItem):
    weight_kg: float


# PhysicalItem structurally expects: {'id': int, 'name': str, 'weight_kg': float}
item: PhysicalItem = {'id': 101, 'name': 'Anvil', 'weight_kg': 45.5}
```

<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>
