Skip to main content

How to Add a Type Hint to a Variable in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<name>: <type> = <value>
<name>: <type>
PartWhat 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. uvx runs mypy without adding it to the project.

Step 1: Write the Script​

counter.py
count: int = 0
names: list[str] = []

names.append("Sam")
count = "three"

print(count, names)

The counter.py script in a macOS-style code window: count is annotated as int and names as a list of str, a name is appended, and count is then given the string three.

Line 5 breaks the hint on purpose: count was declared an int.

Step 2: Run It, Then Check It with mypy​

Terminal
uv run python counter.py
uvx mypy counter.py
Output
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.

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​

MistakeWhat happensDo this instead
Expecting Python to stop a wrong assignmentIt doesn't: three printedRun a type checker such as mypy
Declaring without a value, then reading itcount: int alone creates no variable: NameError: name 'count' is not defined. Did you mean: 'round'?Assign a value before reading it

Troubleshooting​

SymptomLikely causeFix
mypy complains about an empty listIt can't tell what an unannotated [] will holdAnnotate 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​