1 of 6

python: typing = discussion

Nate Stemen

Unitary Fund

Quantum Wednesday

3 May 2023

2 of 6

types

Q: What is a type?

A: a thing that has certain properties

Q: what’s the point?

A: constrains the possible values an expression can have.

3 of 6

Types in python

Python 1.0

January 1994

PEP 484 - Type Hints

29-Sep-2014

4 of 6

Type Constructors

  • Build new types from existing ones

5 of 6

Subtyping

  • Suppose A is a subtype of B: A ≤ B
    • Anywhere B is needed, A can be substituted without issue.
  • Let C be a type constructor
  • How do C<A> relate to C<B>?
    • C<A> ≤ C<B> [covariant]
    • C<B> ≤ C<A> [contravariant]
    • C<A> ≡ C<B> [bivariant]
    • None of the above [invariant]
  • int ≤ float
  • How does list[int] relate to list[float]?

6 of 6

Best practices

  • Function arguments should be typed generally
  • Return types should be typed specifically
  • reveal_type is your friend!