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, and tuple directly in type hints instead of importing from typing. For example: def process(items: list[str]) -> dict[str, int]:.

  • Union Types: Combine multiple types using | (Python 3.10+) or Union (older versions). Example: x: int | str means x can be an integer or a string.

  • Optional: Use str | None or Optional[str] to indicate a value may be None.

  • Generic Types: Use List[T], Dict[K, V], Tuple[T, ...], etc., for container types with specific element types.

  • Protocols: Define structural interfaces using Protocol to 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, Mapping over concrete types like list or dict when only iteration or indexing is needed.

  • Use object instead of Any for arguments that accept any object (e.g., def print_obj(o: object)).

  • Avoid Any unless necessary; it disables type checking.

  • Use shorthand syntax (str | int) over Union[str, int] for cleaner code.

  • Use NewType to 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.

Yes, I use type hints extensively. It helps with autocompletion and let's you catch bugs before you even run the code. lt is also much easier to use a 3rd party module if functions/methods are annotated, since you know what kinds of arguments they expect from you. Answer from LongerHV on reddit.com
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
1 week ago - Doing Derived = NewType('Derived', Original) will make the static type checker treat Derived as a subclass of Original, which means a value of type Original cannot be used in places where a value of type Derived is expected. This is useful when you want to prevent logic errors with minimal runtime cost. Added in version 3.5.2. Changed in version 3.10: NewType is now a class rather than a function. As a result, there is some additional runtime cost when calling NewType over a regular function. Changed in version 3.11: The performance of calling NewType has been restored to its level in Python 3.9.
Discussions

python - When/why use types from typing module for type hints - Stack Overflow
What is exactly the 'right' way for type hinting? My IDE (and resulting code) works fine for type hints using either of below options, but some types can be imported from the typing module. Is ther... More on stackoverflow.com
🌐 stackoverflow.com
Python Type Hints and why you should use them.
because we're adults and not using them is catastrophically unprofessional More on reddit.com
🌐 r/Python
119
220
February 17, 2025
Pycharm SQLAlchemy Plugin adds typing support for Mapped columns : Python
This has been a long standing issue on Pycharm and nobody on the company seems interested in it. Looks like somebody went ahead and created a... More on old.reddit.com
🌐 r/Python
Python true static typing:Python
The official Python community for Reddit! Stay up to date with the latest news, packages, and meta information relating to the Python programming... More on old.reddit.com
🌐 r/Python
🌐
Pyrefly
pyrefly.org › python typing 101
Python Typing 101 | Pyrefly
You can also specify a parameter as optional by using Optional type, or now with the | None syntax. # Optional typing example from typing import Optional middle_name: Optional[str] = None # classic nickname: str | None = None # 3.10+ shorthand ... Both variables can either be a string or None. Optional[str] is the traditional syntax (pre-Python 3.10).
🌐
Python
typing.python.org › en › latest › spec › protocol.html
Protocols — typing documentation
Type checkers should be able to select a correct element from a union after a safe isinstance() or issubclass() call. For narrowing from non-union types, type checkers can use their best judgement (this is intentionally unspecified, since a precise specification would require intersection types). ... © Copyright 2021, The Python Typing Team.
🌐
W3Schools
w3schools.com › python › ref_module_typing.asp
Python typing Module
Use it to add type annotations for better code documentation, IDE support, and static type checking with tools like mypy. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Find elsewhere
🌐
Justin A. Ellis
jellis18.github.io › post › 2023-01-15-advanced-python-types
Some Advanced Typing Concepts in Python - Justin A. Ellis
January 15, 2023 - To start off, let me admit that yes, Python is a dynamically (gradually?) typed language. However, with modern Python (3.6+, and really with 3.10+) and static tooling (i.e. mypy, pyright/pylance) it is a pretty robust statically typed language. Most third party libraries either are typed or have types stubs distributed separately.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-typing-module
Python typing module - Use type checkers effectively | DigitalOcean
August 4, 2022 - This is a good way for the programmer to hint the type of the object(s) being used, during compilation time itself and ensure that the type checkers work correctly. This makes Python code much more readable and robust as well, to other readers!
🌐
Python
typing.python.org › en › latest › spec › annotations.html
Type annotations — typing documentation
The grammar assumes the code has already been parsed as Python code, and loosely follows the structure of the AST. Syntactic details like comments and whitespace are ignored. <Name> refers to a special form. Most special forms must be imported from typing or typing_extensions, except for None, InitVar, type, and tuple.
🌐
Python
peps.python.org › pep-0484
PEP 484 – Type Hints | peps.python.org
The proposal is strongly inspired by mypy. For example, the type “sequence of integers” can be written as Sequence[int]. The square brackets mean that no new syntax needs to be added to the language. The example here uses a custom type Sequence, imported from a pure-Python module typing.
🌐
FastAPI
fastapi.tiangolo.com › python-types
Python Types Intro - FastAPI
Then you can declare a variable to be of type Person: ... class Person: def __init__(self, name: str): self.name = name def get_person_name(one_person: Person): return one_person.name ... Notice that this means "one_person is an instance of the class Person". It doesn't mean "one_person is the class called Person". Pydantic is a Python library to perform data validation.
🌐
Python
typing.python.org › en › latest › reference › best_practices.html
Typing Best Practices — typing documentation
For return values, prefer concrete types (list, dict, etc.) for concrete implementations.
🌐
GitHub
github.com › typeddjango › awesome-python-typing
GitHub - typeddjango/awesome-python-typing: Collection of awesome Python types, stubs, plugins, and tools to work with them.
Type Check Your Django Application - An article based on two recent talks on adding type checks to Django. typing - Official Python documentation for typing module.
Starred by 1.9K users
Forked by 75 users
🌐
Medium
medium.com › @moraneus › exploring-the-power-of-pythons-typing-library-ff32cec44981
Exploring the Power of Python’s typing Library | by Moraneus | Medium
May 17, 2024 - Python’s typing module has profoundly impacted the way developers author and comprehend Python code. This library brings a form of static type checking to a language historically known for its very dynamic nature.
🌐
Reddit
reddit.com › r/python › python type hints and why you should use them.
r/Python on Reddit: Python Type Hints and why you should use them.
February 17, 2025 -

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!

🌐
GitHub
github.com › python › typing
GitHub - python/typing: Python static typing home. Hosts the documentation and a user help forum.
Python static typing home. Hosts the documentation and a user help forum. - python/typing
Starred by 1.7K users
Forked by 290 users
Languages   Python 84.3% | HTML 15.7%
🌐
Python
typing.python.org › en › latest › spec › concepts.html
Type system concepts — typing documentation
This is not to say that the language is “untyped”. Values at runtime have a type and their uses obey typing rules. Not every operation will be checked, but certain primitive operations in the language such as attribute access or arithmetic are. Python is a dynamically typed language.
🌐
Python
typing.python.org
Static Typing with Python — typing documentation
merge-pyi, a thin wrapper around ApplyTypeAnnotationsVisitor from libCST that integrates .pyi signatures as inline type-hints in Python source code.
🌐
Medium
mrslima.medium.com › python-typing-88923684500b
Python Typing. Annotations & Type Hints for Python 3.5… | by Daniela Lima | Medium
October 6, 2021 - Because if you don’t give it a type, it’s assumed it could be anything. This is just being more explicit. You’re saying this is intended to actually accept anything. ... So a lot of times when you are writing a function, in Python, you want one of the parameters to be anything that’s a sequence, you don’t care what type of sequence it is.