The typing module in Python provides support for type hints, enabling developers to annotate code with expected types for variables, function parameters, and return values. Introduced in Python 3.5 (PEP 484), it allows for static type checking using tools like mypy, Pyright, or Pylance, improving code readability, maintainability, and early bug detection—without affecting runtime behavior.
Core Concepts
Type Hints: Use annotations like
def func(x: int, y: str) -> bool:to specify types.Built-in Types: In Python 3.9+, you can use
list,dict,set, andtupledirectly in type hints instead of importing fromtyping. For example:def process(items: list[str]) -> dict[str, int]:.Union Types: Combine multiple types using
|(Python 3.10+) orUnion(older versions). Example:x: int | strmeansxcan be an integer or a string.Optional: Use
str | NoneorOptional[str]to indicate a value may beNone.Generic Types: Use
List[T],Dict[K, V],Tuple[T, ...], etc., for container types with specific element types.Protocols: Define structural interfaces using
Protocolto specify required methods/attributes without inheritance. Useful for duck typing.Type Aliases: Simplify complex types with
type Alias = SomeType. Example:Vector = list[float].
Best Practices
Prefer
Iterable,Sequence,Mappingover concrete types likelistordictwhen only iteration or indexing is needed.Use
objectinstead ofAnyfor arguments that accept any object (e.g.,def print_obj(o: object)).Avoid
Anyunless necessary; it disables type checking.Use shorthand syntax (
str | int) overUnion[str, int]for cleaner code.Use
NewTypeto create distinct types (e.g.,UserID = NewType('UserID', int)) to prevent mixing similar types.
Tools & Ecosystem
Type Checkers: mypy, Pyright, Pylance (VS Code), and others enforce type safety.
IDE Support: Full autocompletion, error highlighting, and refactoring assistance.
Modern Python: With Python 3.10+, PEP 604 (union operator) and PEP 585 (generic built-ins) have simplified type hinting significantly.
Note: Type hints are optional and purely for tooling and documentation. They do not change how Python executes code. However, they are widely adopted in professional Python development for robust, scalable codebases.
Python is not as statically typed language but we can specify the type of a variable.
Do you use this feature and if it's the case why and how ?
python - When/why use types from typing module for type hints - Stack Overflow
Python Type Hints and why you should use them.
Pycharm SQLAlchemy Plugin adds typing support for Mapped columns : Python
Python true static typing:Python
Videos
The "right" way is to use builtins when possible (e.g. dict over typing.Dict). typing.Dict is only needed if you use Python < 3.9. In older versions you couldn't use generic annotations like dict[str, Any] with builtins, you had to use Dict[str, Any]. See PEP 585
There is no big difference between typing.Dict and dict.
Just typing.Dict is actually a Generic Type, so it allows you to specify subtypes inside the brackets.
Like:
from typing import Dict
def func_1(arg_one: Dict[str, int]) -> Dict:
pass
But typing.Dict is only necessary if your Python version is under 3.9. Other wise you could do the same with regular dicts.
Example Python >= 3.9:
def func_1(arg_one: dict[str, int]) -> dict:
pass
https://blog.jonathanchun.com/2025/02/16/to-type-or-not-to-type/
I wrote this blog post as I've seen a lot of newer developers complain about Type hints and how they seem unnecessary. I tried to copy-paste a short excerpt from the blog post here but it kept detecting it as a question which is not allowed, so decided to leave it out.
I know there's plenty of content on this topic, but IMO there's still way too much untyped Python code!