Skip to main content

How to Type Hint a Python Function That Returns Nothing with -> None

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some functions do a job and hand nothing back: they print, save a file or send a message. In a type hint, you say so with -> None after the parameters. This guide writes one such function on Python 3.15.2 and looks at what Python does with the hint.

Syntax​

Syntax
def <name>(<parameters>) -> None:
<body with no return value>
PartWhat it means
<name>(<parameters>)The function and its parameters, with their own hints if you like, such as name: str
-> NoneSays the function returns no value
<body with no return value>Code that does its job without return <value>. A bare return, or none at all, returns None

The typing specification says: "When used in a type hint, the expression None is considered equivalent to type(None)." A function that ends without returning a value gives back None, so -> None describes it exactly.

The Example​

greet() prints a greeting and returns nothing. The script calls it, prints what the call returned, and prints the hints Python stored on the function.

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​

greet.py
def greet(name: str) -> None:
print(f"Hello, {name}!")


result = greet("Ana")
print(result)
print(greet.__annotations__)

The greet.py script in a macOS-style code window: def greet(name: str) -> None prints a greeting; the script stores greet("Ana") in result, prints result, and prints greet.annotations.

  • -> None comes after the closing parenthesis and before the colon.
  • result catches what the call returns.
  • greet.__annotations__ is the dictionary of hints Python keeps on the function.

Step 2: Run It​

Terminal
uv run python greet.py
Output
Hello, Ana!
None
{'name': <class 'str'>, 'return': None}

The terminal after uv run python greet.py: Hello, Ana!, then None, then the annotations dictionary with name as class str and return as None.

The function printed its greeting, the call returned None, and Python stored the hint under the key 'return'. The hint changes nothing when the program runs: it tells readers, editors and type checkers what to expect. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Writing none in lowercase: -> noneThe function still runs, but reading its hints fails with NameError: name 'none' is not defined. Did you mean: 'None'?Write None with a capital N
Returning a value from a -> None functionPython doesn't check: return f"Hello, {name}!" under -> None runs, and the caller gets the textFix the hint to the real type, such as -> str, or stop returning the value
Leaving the hint out of the other functions in a file that uses hintsReaders and tools can't tell "returns nothing" from "not annotated yet"Write -> None on every function that returns nothing

Troubleshooting​

SymptomLikely causeFix
NameError only when something reads the hintsA misspelled name in a hint. Since Python 3.14, hints are evaluated when they're read, not when the function is definedCorrect the spelling
The function returns None when you expected a valueIt has no return <value>Add the return, and change the hint to match

FAQ​

Do I have to write -> None?​

No. Hints are optional, and the function works the same without it. Write it when the rest of your code has hints, so "returns nothing" is said rather than guessed.

Why did the misspelled none not fail straight away?​

Since Python 3.14, annotations are evaluated lazily. The language reference says "they are not evaluated when the code containing the annotation is evaluated." In the run above, greet("Ana") printed Hello, Ana!, and the NameError came only when the script read greet.__annotations__.

Conclusion​

def greet(name: str) -> None: said the function returns nothing. The call returned None, and Python stored the hint as 'return': None without checking it. Spell it with a capital N, and keep it true.

Additional Resources​