We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Precise Type Hints with Literals and Generics
Python
Check a value's type before you use it, limit what a value can be with Literal, Final and NewType, and write reusable functions and classes with generics. Learn why mypy will not pass a list[int] where a list[object] is expected, and finish by modernizing a typed module.
This track uses Python. You can make it your practice mode to browse related content across the site.
Your learning sequence
- 1 Not solved yet. Check a Value Before You Use It Practice · Easy
- 2 Not solved yet. Narrow a Union Without Treating Zero as Missing Practice · Medium
- 3 Not solved yet. Turn an Unknown Value into an int Practice · Medium
- 4 Not solved yet. Limit a Parameter to a Few Exact Values with Literal Practice · Medium
- 5 Not solved yet. Make mypy Report an Unhandled Event with assert_never Practice · Medium
- 6 Not solved yet. Lock Down a Rate Limiter with Final and @final Practice · Easy
- 7 Not solved yet. Give Each Task an ID from a ClassVar Counter Practice · Easy
- 8 Not solved yet. Keep Cents and Millis Apart with NewType Practice · Medium
- 9 Not solved yet. Describe Nested JSON with a Recursive Type Alias Practice · Medium
- 10 Generic Functions from Zero Read
- 11 Not solved yet. Partition and Chunk a List with Generic Helpers Practice · Medium
- 12 Not solved yet. Build a Generic Result Class Practice · Medium
- 13 Not solved yet. Limit a Type Parameter with Bounds and Constraints Practice · Hard
- 14 Not solved yet. Why a list[Dog] Is Not a list[Animal] Practice · Medium
- 15 Not solved yet. Accept Read-Only Inputs and Return Concrete Results Practice · Medium
- 16 Not solved yet. Control Variance in Your Own Generic Classes Practice · Hard
- 17 Not solved yet. Type the Callbacks of a Retry Helper Practice · Hard
- 18 Not solved yet. Catch a Renamed Method with @override Practice · Easy
- 19 Not solved yet. Keep the Subclass Type in a Chained Builder with Self Practice · Medium
- 20 Not solved yet. Write Your Own Narrowing Check with TypeIs Practice · Hard
- 21 Not solved yet. Find Out What isinstance Checks on a Protocol Practice · Hard
- 22 Not solved yet. List the Members of a Union at Runtime Practice · Easy
- 23 Not solved yet. Rewrite Old Typing Spellings Such as List and Optional Practice · Easy
- 24 Not solved yet. Capstone: Modernize a Typed Inventory Module Practice · Hard
- 25 Give a Type Parameter a Default Type Read
- 26 Read Annotations at Runtime in Python 3.14 Read
Check yourself
7 questions · not saved while you're signed outUse these questions to check your understanding and compare explanations. Your score does not affect track completion.
What does mypy catch in this call that a plain str annotation would let through?
Choose one answer to reveal the explanations. Answers are kept only while this page is open.
from typing import Literal
def lit(mode: Literal["r", "w"]) -> None: ...
lit("x")