Questions tagged [python-typing]

For questions related to PEP 484 (Type Hints), PEP 526 (Syntax for Variable Annotations) and the `typing` module.

Type hints in python, as specified by PEP 484 and PEP 483, are an optional feature that allow for detailed static analysis of python source code by third-party type-checkers. Popular third-party type-checkers include MyPy, Pylance, Pyright, Pyre, and PyCharm's builtin type-checker.

Support for type hints in python is provided by the typing module. Discussions regarding proposed enhancements to the typing module and python-typing syntax take place on the typing-sig mailing list.

Relevant PEPs

Since PEP 484, a number of subsequent PEPs have modified, enhanced and extended Python's framework for type annotations, including:

  • PEP 526: "Syntax for Variable Annotations"

  • PEP 544: "Protocols: Structural subtyping (static duck typing)"

  • PEP 561: "Distributing and Packaging Type Information"

  • PEP 563: "Postponed Evaluation of Annotations"

    See also: the April 2021 statement by the Python Steering Council regarding the future of this PEP.

  • PEP 585: "Type Hinting Generics In Standard Collections"

  • PEP 586: "Literal Types"

  • PEP 589: "TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys"

  • PEP 591: "Adding a final qualifier to typing"

  • PEP 593: "Flexible function and variable annotations"

    Introducing typing.Annotated

  • PEP 604: "Allow writing union types as X | Y"

  • PEP 612: "Parameter Specification Variables"

  • PEP 613: "Explicit Type Aliases"

  • PEP 647: "User-Defined Type Guards"

1841 questions
9 answers

How do I type hint a method with the type of the enclosing class?

I have the following code in Python 3: class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, other: Position) -> Position: return Position(self.x + other.x, self.y +…
Michael van Gerwen
  • 12,011
  • 3
  • 11
  • 8
4 answers

How to specify multiple return types using type-hints

I have a function in python that can either return a bool or a list. Is there a way to specify the return types using type hints? For example, is this the correct way to do it? def foo(id) -> list or bool: ...
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
3 answers

How do I add default parameters to functions when using type hinting?

If I have a function like this: def foo(name, opts={}): pass And I want to add type hints to the parameters, how do I do it? The way I assumed gives me a syntax error: def foo(name: str, opts={}: dict) -> str: pass The following doesn't throw…
  • 9,656
  • 4
  • 34
  • 51
4 answers

How can I specify the function type in my type hints?

How can I specify the type hint of a variable as a function type? There is no typing.Function, and I could not find anything in the relevant PEP, PEP 483.
  • 11,356
  • 5
  • 40
  • 74
8 answers

Type annotations for *args and **kwargs

I'm trying out Python's type annotations with abstract base classes to write some interfaces. Is there a way to annotate the possible types of *args and **kwargs? For example, how would one express that the sensible arguments to a function are…
  • 22,455
  • 16
  • 75
  • 126
2 answers

How to specify "nullable" return type with type hints

Suppose I have a function: def get_some_date(some_argument: int=None) -> %datetime_or_None%: if some_argument is not None and some_argument == 1: return datetime.utcnow() else: return None How do I specify the return type…
  • 5,351
  • 4
  • 23
  • 26
5 answers

How should I use the Optional type hint?

I'm trying to understand how to use the Optional type hint. From PEP-484, I know I can use Optional for def test(a: int = None) either as def test(a: Union[int, None]) or def test(a: Optional[int]). But how about following examples? def test(a :…
  • 7,797
  • 12
  • 50
  • 95
1 answer

How to annotate types of multiple return values?

How do I use type hints to annotate a function that returns an Iterable that always yields two values: a bool and a str? The hint Tuple[bool, str] is close, except that it limits the return value type to a tuple, not a generator or other type of…
Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
4 answers

What are type hints in Python 3.5?

One of the most talked-about features in Python 3.5 is type hints. An example of type hints is mentioned in this article and this one while also mentioning to use type hints responsibly. Can someone explain more about them and when they should be…
  • 20,055
  • 8
  • 52
  • 73
7 answers

Python type hinting without cyclic imports

I'm trying to split my huge class into two; well, basically into the "main" class and a mixin with additional functions, like so: file: import class Main(object, MyMixin): def func1(self, xxx): ...…
  • 8,747
  • 4
  • 44
  • 64
5 answers

Type hinting a collection of a specified type

Using Python 3's function annotations, it is possible to specify the type of items contained within a homogeneous list (or other collection) for the purpose of type hinting in PyCharm and other IDEs? A pseudo-python code example for a list of…
Eric W.
  • 7,148
  • 3
  • 20
  • 27
4 answers

Difference between defining typing.Dict and dict?

I am practicing using type hints in Python 3.5. One of my colleague uses typing.Dict: import typing def change_bandwidths(new_bandwidths: typing.Dict, user_id: int, user_name: str) -> bool: …
  • 8,383
  • 13
  • 61
  • 109
2 answers

Python void return type annotation

In python 3.x, it is common to use return type annotation of a function, such as: def foo() -> str: return "bar" What is the correct annotation for the "void" type? I'm considering 3 options: def foo() -> None: not logical IMO, because None…
  • 18,872
  • 15
  • 48
  • 69
5 answers

How do I annotate types in a for-loop?

I want to annotate a type of a variable in a for-loop. I tried this but it didn't work: for i: int in range(5): pass What I expect is working autocomplete in PyCharm 2016.3.2, but using pre-annotation didn't work: i: int for i in range(5): …
  • 3,960
  • 3
  • 16
  • 26
2 answers

Using List/Tuple/etc. from typing vs directly referring type as list/tuple/etc

What's the difference of using List, Tuple, etc. from typing module: from typing import Tuple def f(points: Tuple): return map(do_stuff, points) As opposed to referring to Python's types directly: def f(points: tuple): return map(do_stuff,…
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
2 3
99 100