All courses
· intermediate

Type Hints in Python

Say what a function expects and returns, and let a checker find the mistakes before the program runs. Annotations, the shapes that describe real code, and how to add them to a codebase gradually.

Pathaxis · Original content

Written in house, from the work rather than from a syllabus. Every example is one we would use ourselves, and every exercise is checked against the same engine that grades your answer.

85 min 6 lessons 20 quiz questions 6 exercisesStart course

What you’ll learn

  • Annotate arguments and return values, and know what Python does and does not enforce
  • Describe containers precisely with list, dict and tuple annotations
  • Handle a value that may be absent with X | None, and narrow it before use
  • Constrain a string to a fixed set of values with Literal
  • Describe what an object can do rather than what it inherits, using Protocol
  • Adopt typing gradually, and know where annotations pay for themselves

Before you start

  • · Comfortable writing functions and classes
  • · Familiar with lists, dictionaries and exceptions

Syllabus

01

Annotating code

What an annotation is, and what it is not.

02

Describing real shapes

Containers, absent values and fixed sets of options.

03

Typing that scales

Structural types, reusable generic code, and adopting it gradually.