Skip to main content

How to Add a Type Hint for a Dictionary in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<name>: dict[<key type>, <value type>] = {...}

def <function>(<parameter>: dict[<key type>, <value type>]) -> <return type>:
PartWhat 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.py
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).

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​

Terminal
uv run python stock.py
Output
11

The terminal after 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​

MistakeWhat happensDo this instead
Expecting Python to reject a wrong valuestock["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 typingIt 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​