Type annotations in Python are a way to add explicit type information to variables, function parameters, and return values using a syntax introduced in PEP 484. They are used to improve code readability, enable better IDE support, and allow static type checking with tools like mypy, Pyright, or PyCharm.

Syntax and Basic Usage

  • Function parameters and return types:

    def greet(name: str) -> str:
        return f"Hello, {name}!"

    Here, name: str indicates the expected input type, and -> str specifies the return type.

  • Variable annotations:

    count: int = 0
    names: list[str] = ["Alice", "Bob"]

Key Features

  • No runtime enforcement: Type annotations are ignored by the Python interpreter. They do not raise errors if incorrect types are used at runtime.

  • Static analysis: Tools like mypy use annotations to catch type-related bugs before execution.

  • Improved developer experience: IDEs use type hints for auto-completion, refactoring, and error detection.

Advanced Type Annotations

  • Union types: Use | (Python 3.10+) or Union (earlier versions):

    def process(data: int | str) -> bool:
        return isinstance(data, int)
  • Optional types: Use X | None or Optional[X]:

    def get_user(id: int) -> str | None:
        # returns None if user not found
        pass
  • Generics and TypeVar:

    from typing import TypeVar, Generic
    T = TypeVar('T')
    
    class Box(Generic[T]):
        def __init__(self, item: T):
            self.item = item
  • Typed dictionaries:

    from typing import TypedDict
    
    class Person(TypedDict):
        name: str
        age: int

Benefits

  • Better code documentation

  • Fewer runtime errors via static analysis

  • Improved collaboration and code maintainability

  • Enhanced tooling support (e.g., AI-assisted code completion)

For more details, refer to the official typing documentation and PEP 484.

I use them as though they’re required as they help readability so much Answer from pan0ramic on reddit.com
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
1 month ago - For this reason, tuples are special-cased in Python’s typing system. tuple accepts any number of type arguments: # OK: ``x`` is assigned to a tuple of length 1 where the sole element is an int x: tuple[int] = (5,) # OK: ``y`` is assigned to a tuple of length 2; # element 1 is an int, element 2 is a str y: tuple[int, str] = (5, "foo") # Error: the type annotation indicates a tuple of length 1, # but ``z`` has been assigned to a tuple of length 3 z: tuple[int] = (1, 2, 3)
🌐
Python
typing.python.org › en › latest › spec › annotations.html
Type annotations — typing documentation
Generally, an annotation expression is a type expression, optionally surrounded by one or more type qualifiers or by Annotated. Each type qualifier is valid only in some contexts. Note that while annotation expressions are the only expressions valid as type annotations in the type system, the Python language itself makes no such restriction: any expression is allowed.
Discussions

How many Python core developers use type annotations?
I use them as though they’re required as they help readability so much More on reddit.com
🌐 r/Python
121
169
November 14, 2023
python - How to properly function annotate / type hint a list of strings - Stack Overflow
I am trying to figure out how to properly function annotate or type hint a list of strings. For example, if I had a function like this: def send_email(self, from_address: str, to_addresses: list[st... More on stackoverflow.com
🌐 stackoverflow.com
python - How to use typing.Annotated - Stack Overflow
I'm having a hard time understanding what typing.Annotated is good for from the documentation and an even harder time finding explanations/examples outside the documentation. In what context would ... More on stackoverflow.com
🌐 stackoverflow.com
Type annotations in the stdlib - Core Development - Discussions on Python.org
I’d like us to think carefully about this. I agree that this might be nice for “simple” type annotations, where an argument can e.g. only be a str or whatever. But I’d be wary of starting a large-scale project to add type hints in lots of places. As a typeshed maintainer and a codeowner ... More on discuss.python.org
🌐 discuss.python.org
10
October 24, 2022
🌐
DEV Community
dev.to › etenil › why-i-stay-away-from-python-type-annotations-2041
Why I stay away from Python type annotations - DEV Community
June 22, 2020 - With annotations, our code is "specific" by default and we work hard to make it generic. Note the quotes around "specific" as this is only enforced by linting tools like mypy and so you still need to do your pre-flight checks. This is a fundamental shift in the nature of the language. A lot of developers like to claim that type hints are the best thing for the language since sliced bread. However I get the feeling those people only look at Python in a vacuum.
🌐
Reddit
reddit.com › r/python › how many python core developers use type annotations?
r/Python on Reddit: How many Python core developers use type annotations?
November 14, 2023 - Why shouldn't they use python? ... I think even with type annotations it's faster to write than c# and java. It's just less verbose with nice language features.
🌐
CodiLime
codilime.com › blog › software development › backend › give python more speed with type annotations
Give Python more speed with type annotations
August 26, 2024 - In addition to improving code readability, type annotations can also help reduce bugs in your Python projects. By explicitly stating the expected data types for variables and function parameters, you're less likely to accidentally assign a value of the wrong type or pass an incorrect argument to a function.
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Functions › TypeAnnotations.html
12.7. Type Annotations — Foundations of Python Programming
Python allows you to indicate the intended type of the function parameters and the type of the function return value in a function definition using a special notation demonstrated in this example: This definition of duplicate makes use of type annotations that indicate the function’s parameter ...
Find elsewhere
🌐
Python
peps.python.org › pep-0526
PEP 526 – Syntax for Variable Annotations | peps.python.org
Note that, although the syntax does allow tuple packing, it does not allow one to annotate the types of variables when tuple unpacking is used: # Tuple packing with variable annotation syntax t: Tuple[int, ...] = (1, 2, 3) # or t: Tuple[int, ...] = 1, 2, 3 # This only works in Python 3.8+ # Tuple unpacking with variable annotation syntax header: str kind: int body: Optional[List[str]] header, kind, body = message
🌐
FastAPI
fastapi.tiangolo.com › python-types
Python Types Intro - FastAPI
Python has support for optional "type hints" (also called "type annotations").
🌐
Medium
medium.com › @ramanbazhanau › advanced-type-annotations-in-python-part-1-3c9a592e394
Advanced Type Annotations in Python | Medium | Medium
September 20, 2023 - Explore Python's advanced type hinting with this in-depth guide. Dive into Generics, TypeGuards, TypedDict, and more to enhance code readability and robustness. Master type annotations for better coding.
🌐
LogRocket
blog.logrocket.com › home › understanding type annotation in python
Understanding type annotation in Python - LogRocket Blog
June 4, 2024 - This tutorial will explore type hints and how you can add them to your Python code. It will focus on the mypy static type-checking tool and its operations in your code. You’ll learn how to annotate variables, functions, lists, dictionaries, and tuples.
🌐
Florimond Manca
florimond.dev › en › posts › 2018 › 07 › why-i-started-using-python-type-annotations-and-why-you-should-too
Why I started using Python type annotations – and why you should too - Florimond Manca
July 27, 2018 - However for simple functions, using a full-blown docstring just to describe arguments and return values sometimes felt like a workaround — for the fact that Python did not offer any notion of type hinting whatsoever (or so I thought). Type annotations can sometimes replace a docstring altogether as they are — in my opinion — a very clean and simple way of documenting inputs and outputs.
🌐
Mypy
mypy.readthedocs.io › en › stable › cheat_sheet_py3.html
Type hints cheat sheet - mypy 1.19.1 documentation
# For most types, just use the name of the type in the annotation # Note that mypy can usually infer the type of a variable from its value, # so technically these annotations are redundant x: int = 1 x: float = 1.0 x: bool = True x: str = "test" x: bytes = b"test" # For collections on Python 3.9+, the type of the collection item is in brackets x: list[int] = [1] x: set[int] = {6, 7} # For mappings, we need the types of both keys and values x: dict[str, float] = {"field": 2.0} # Python 3.9+ # For tuples of fixed size, we specify the types of all the elements x: tuple[int, str, float] = (3, "yes
🌐
DERLIN.
blog.derlin.ch › python-type-hints-and-future-annotations
Python, type hints, and future annotations
February 2, 2024 - If you ever tried using new type hinting features in old Python versions or struggled with forward references, you may have stumbled upon (and used) the famous future import: from __future__ import annotations def foo(a: list[int], b: dict[str, object], c: int) -> str | None: ...
🌐
Medium
leapcell.medium.com › explaining-python-type-annotations-a-comprehensive-guide-to-the-typing-module-87d9e3599d59
Explaining Python Type Annotations: A Comprehensive Guide to the typing Module | by Leapcell | Medium
March 12, 2025 - Among them, List, Tuple, and Dict are the most commonly used type aliases. Taking List as an example, it represents the list type in Python, and the type of elements in the list can be specified through square brackets.
🌐
YouTube
youtube.com › indently
Python: A Quick Guide To Type Annotations (ft. Mypy) - YouTube
In today’s video we will be learning about type annotations in Python, and also why I always try to type everything with no exceptions. ▶ Become job-ready wi...
Published   March 21, 2024
Views   33K
🌐
Galaxy
training.galaxyproject.org › training-material › topics › data-science › tutorials › python-typing › tutorial.html
Hands-on: Python - Type annotations / Python - Type annotations / Foundations of Data Science
February 13, 2023 - Run wget https://training.galaxyproject.org/training-material/topics/data-science/tutorials/python-typing/data-science-python-typing.ipynb · Select the notebook that appears in the list of files on the left. ... Right click one of these links: Jupyter Notebook (With Solutions), Jupyter Notebook (Without Solutions) Save Link As.. In some languages type annotations are a core part of the language and types are checked at compile time, to ensure your code can never use the incorrect type of object.
🌐
HackerNoon
hackernoon.com › type-annotation-in-python
Type Annotation In Python | HackerNoon
February 1, 2022 - Type Annotation are a quick way to validate the actual type of the variables or arguments that are being passed to the functions it is also called type hinting.
🌐
Python.org
discuss.python.org › core development
Type annotations in the stdlib - Core Development - Discussions on Python.org
October 24, 2022 - I’d like us to think carefully about this. I agree that this might be nice for “simple” type annotations, where an argument can e.g. only be a str or whatever. But I’d be wary of starting a large-scale project to add type hints in lots of places. As a typeshed maintainer and a codeowner ...