How to Add a Type Hint to a Variable in Python
Function parameters aren't the only things that take type hints. A variable can say what it should hold, which helps most with empty containers: [] alone doesn't tell a reader, or a type checker, what goes in it. This guide annotates two variables and breaks one hint on purpose, with real output from Python 3.15.2 and mypy 2.4.1.
Syntax
<name>: <type> = <value>
<name>: <type>
| Part | What it means |
|---|---|
<name>: <type> | Says what the variable should hold, such as int or list[str] |
= <value> | Optional: assigns a value in the same statement |
self.<attribute>: <type> = <value> | The same, for an attribute inside a class |
Python's language reference: "Annotation assignment is the combination, in a single statement, of a variable or attribute annotation and an optional assignment statement". As the typing documentation puts it, "The Python runtime does not enforce function and variable type annotations."
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.uvxruns mypy without adding it to the project.
Step 1: Write the Script
count: int = 0
names: list[str] = []
names.append("Sam")
count = "three"
print(count, names)

Line 5 breaks the hint on purpose: count was declared an int.
Step 2: Run It, Then Check It with mypy
uv run python counter.py
uvx mypy counter.py
three ['Sam']
counter.py:5: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
Found 1 error in 1 file (checked 1 source file)
![The output in a terminal: Python prints three ['Sam'], then mypy reports line 5's incompatible types in assignment, a str given to a variable of type int.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-variable-hints-02-output.webp)
Python ran the script and printed three: hints don't change what runs. mypy read them and reported line 5. The list[str] hint also told mypy what names holds, so names.append("Sam") passed.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting Python to stop a wrong assignment | It doesn't: three printed | Run a type checker such as mypy |
| Declaring without a value, then reading it | count: int alone creates no variable: NameError: name 'count' is not defined. Did you mean: 'round'? | Assign a value before reading it |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| mypy complains about an empty list | It can't tell what an unannotated [] will hold | Annotate it, as in names: list[str] = [] |
FAQ
Do I need to annotate every variable?
No. Type checkers work out most types from the value, such as count = 0. Annotate where the value doesn't tell, such as empty containers or attributes set to None first.
Does a hint make the code slower?
No measurable difference in normal code: Python stores the hint and runs the assignment as usual.
Conclusion
count: int = 0 and names: list[str] = [] recorded what each variable should hold, Python ran a wrong assignment anyway, and mypy reported it on line 5.
Additional Resources
- Annotated assignment statements — variable and attribute annotations.
- typing — the types you can use in hints.
