Skip to main content

How to Add Type Hints to a Python Function

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A type hint writes down what kind of value a function expects and returns: a float here, an int there. Editors use the hints to warn you early, and readers see at a glance what to pass, but Python itself doesn't check them when the code runs.

Syntax​

Syntax
def <name>(<parameter>: <type>, <parameter>: <type>) -> <return type>:
<body>
PartWhat it does
<parameter>: <type>Says what type the parameter should be, such as price: float.
-> <return type>Says what type the function returns.
<type>A type such as int, float, str, bool or list[str].

The Python glossary defines a type hint as "An annotation that specifies the expected type for a variable, a class attribute, or a function parameter or return value", and adds: "Type hints are optional and are not enforced by Python but they are useful to static type checkers."

The Example​

The example writes a total_price function with hints for both parameters and the result, calls it correctly, and then calls it with a string to show what happens.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Hints​

Create a file named main.py:

main.py, which defines total_price(price: float, quantity: int) -> float returning price times quantity, and prints total_price(2.5, 4) and total_price("2.5", 4).

main.py
def total_price(price: float, quantity: int) -> float:
return price * quantity


print(total_price(2.5, 4))
print(total_price("2.5", 4))
  • price: float and quantity: int describe the two parameters.
  • -> float describes the result.
  • The second call passes the string "2.5", which breaks the hint on purpose.

Step 2: Run It​

Terminal
uv run python main.py
Output
10.0
2.52.52.52.5

A zsh terminal where uv run python main.py prints 10.0 and 2.52.52.52.5.

The first call worked as intended. The second ran too: Python ignored the hint, and a string times 4 repeats the string four times. That's exactly what the glossary means by "not enforced". The command is the same in zsh, bash and PowerShell.

Reading the Hints Back​

Python keeps the hints on the function, where tools can read them:

Hints at run time
print(total_price.__annotations__)
# {'price': <class 'float'>, 'quantity': <class 'int'>, 'return': <class 'float'>}

Common Mistakes​

MistakeWhat happensDo this instead
Expecting Python to reject a wrong typeThe call runs anyway, as 2.52.52.52.5 showsRun a static type checker over the code
Writing -> float after the colon, as in def f(x: int): -> floatSyntaxError: invalid syntaxPut -> type before the colon
Hinting float and returning a stringNothing at run time; only a checker noticesKeep the hint and the code in agreement

Troubleshooting​

SymptomLikely causeFix
A function gets a value of the wrong type with no errorHints aren't checked when the code runsCheck types with a static type checker, or convert the value in the function
Your editor underlines a callIt compared the call with the hintsPass a value of the hinted type

FAQ​

Do type hints make Python faster?​

No. Python doesn't enforce them, and the example ran the same with a wrong type; they're for readers, editors and type checkers.

What checks the hints, then?​

Static type checkers, such as mypy, and editors read the hints without running the code and report calls that don't match.

Conclusion​

parameter: type and -> type document what a function takes and returns. Python stores the hints but doesn't enforce them, so pair them with a type checker when you want mistakes caught before the code runs.

Additional Resources​

  • type hint, in the Python glossary.
  • typing, the module documentation that describes the hint types.