How to Add a Type Hint for a Dictionary in Python
A type hint says what a variable or a function argument is meant to hold. For a dictionary, "a dict" alone leaves out the useful part: what the keys are and what the values are. dict[str, int] says both. This guide adds that hint to a variable and a function on Python 3.15.2.
Syntax
<name>: dict[<key type>, <value type>] = {...}
def <function>(<parameter>: dict[<key type>, <value type>]) -> <return type>:
| Part | What it means |
|---|---|
dict[...] | A dictionary, written with the built-in dict and square brackets |
<key type> | The type of every key, such as str |
<value type> | The type of every value, such as int |
-> <return type> | What the function returns, which can be a dict[...] too |
The typing documentation starts with the rule to remember: "The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc."
The Example
A stock list maps fruit names to how many are left. A function adds up the stock, and both carry the hint dict[str, int].
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
stock: dict[str, int] = {"apples": 4, "pears": 7}
def total(items: dict[str, int]) -> int:
return sum(items.values())
print(total(stock))
![The stock.py script in a macOS-style code window: stock annotated as dict[str, int] with 4 apples and 7 pears, a function total whose items parameter is a dict[str, int] and which returns an int, sum(items.values()), and a print of total(stock).](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-dict-type-hint-01-code.webp)
stock: dict[str, int] reads "stock is a dictionary whose keys are strings and whose values are integers". The function says it takes such a dictionary and returns an int.
Step 2: Run It
uv run python stock.py
11

The 4 apples and 7 pears add up to 11. The hints changed nothing about how the code ran: they're there for the people and the tools that read it. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting Python to reject a wrong value | stock["plums"] = "many" ran without a complaint, and the dictionary printed as {'apples': 4, 'plums': 'many'}. The error came only later, in sum(): TypeError: unsupported operand type(s) for +: 'int' and 'str' | Check your code with a type checker, which reads the hints and reports the wrong value before the program runs |
Importing Dict from typing | It still works, but the documentation calls it a "Deprecated alias to dict" | Use the built-in dict[...] |
FAQ
How do I hint a dictionary whose values can be different types?
Join the types with |: dict[str, int | str] allows integers and strings as values.
Do I need a hint on every variable?
No. Hints help most on function parameters and return values, where a reader can't see the values. Python works the same with or without them.
Conclusion
dict[str, int] described the stock dictionary's keys and values, on the variable and on the function. Python ran the code the same as without hints, and it didn't stop a wrong value either: hints are for readers and checking tools.
Additional Resources
- typing — type hints in Python.
- Generic alias type — how
dict[str, int]works.
