Skip to main content

How to Accept Any Type in a Python Type Hint with typing.Any

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Type hints say what each value should be: name: str, count: int. Some values really can be anything, like the values of a settings dictionary that mixes text, numbers and flags. typing.Any is the hint for that. This guide hints a function and a dictionary with Any, with real output from Python 3.15.2.

Syntax​

Syntax
from typing import Any

def <function>(<parameter>: Any) -> <return type>: ...
<variable>: dict[str, Any] = {...}
PartWhat it means
from typing import AnyBrings in Any. It isn't a built-in name
<parameter>: AnyThe parameter may receive a value of any type
dict[str, Any]A dictionary with string keys and values of any type

Python's typing documentation: "Special type indicating an unconstrained type. Every type is assignable to Any. Any is assignable to every type."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

typing_any.py
from typing import Any


def describe(value: Any) -> str:
return f"{value!r} ({type(value).__name__})"


settings: dict[str, Any] = {"theme": "dark", "retries": 3, "beta": True}
for key, value in settings.items():
print(key, "->", describe(value))

The typing_any.py script in a macOS-style code window: a describe function whose value parameter is hinted Any, and a settings dictionary hinted dict of str to Any holding a theme string, a retries number and a beta flag, each described in a loop.

  • describe(value: Any) says the function accepts a value of any type, and returns a str.
  • settings: dict[str, Any] says the keys are strings and the values can be anything.
  • The loop passes a str, an int and a bool to the same function.

Step 2: Run It​

Terminal
uv run python typing_any.py
Output
theme -> 'dark' (str)
retries -> 3 (int)
beta -> True (bool)

The output in a terminal: theme is the str dark, retries the int 3 and beta the bool True.

One function described three different types, and the hints said so honestly: Any for the parameter, dict[str, Any] for the settings. A reader, and a type checker, knows not to expect one particular type there.

Common Mistakes​

MistakeWhat happensDo this instead
Forgetting from typing import AnyThe def still runs, because Python 3.14 and later evaluate hints only when something reads them. Then reading them fails: NameError: name 'Any' is not defined. Did you mean: 'any'?Import it
Expecting the hint to check values when the code runsPython doesn't enforce hints: double("ab") on a function hinted x: int returned 'abab'Check values yourself where it matters
Using Any where one type is knownType checkers stop checking that valueUse the real type, and keep Any for values that can truly be anything

Troubleshooting​

SymptomLikely causeFix
NameError: name 'Any' is not defined when a library reads your hintsAny wasn't importedAdd from typing import Any

FAQ​

Is any the same as Any?​

No. Lowercase any() is a built-in function that checks whether any item in a list is true. The type hint is Any, from typing.

Can I hint a value that's one of a few types instead?​

Yes, with |: int | str accepts an int or a str, which says more than Any.

Conclusion​

value: Any and dict[str, Any] described honestly what describe() and the settings hold: values of any type, here a str, an int and a bool.

Additional Resources​