Last Updated: December 6, 2025
31 quizzes
Complete the code to annotate the age parameter as an integer and call the function
info: str = describe_user(age25)Click an option to fill the blank:
Complete the code so the variable user_id is explicitly annotated as an integer
user_idint = 42Click an option to fill the blank:
Complete the code to declare that the function returns a string
def greet_user(name: str)str:Click an option to fill the blank:
What do type hints change about how Python executes code?
Which is a correct function annotation for a function that takes a float and returns an int?
Which type from the typing module lets a parameter accept either an int or a str?
Which import is needed to use List and Dict for type hints in Python 3.9 code that prefers typing module generics?
What is the main benefit of using a generic class like Generic[T]?
In modern Python, which is the most concise way to say a value can be int or None?
Which type annotation best describes a callback that takes a str and returns bool?
How do you define a simple type alias for Dict[str, int]?
Which command runs mypy on a file called app.py?
Given price: float | None, which check is safest before using price in arithmetic?
What does mypy do when it finds a type mismatch?
Order the steps to create a generic Box class that stores any type using the typing module.
Drag and drop to reorder, or use the arrows.
Order the steps to use mypy to check a typed function in a new file.
Drag and drop to reorder, or use the arrows.
What is the output of this code using a variable annotation?
1total: int = 3
2extra: int = 4
3print(total + extra)What is the single line of output of this code with Optional type?
1from typing import Optional
2
3def format_price(price: Optional[float]) -> str:
4 if price is None:
5 return "N/A"
6 return f"${price:.2f}"
7
8print(format_price(None))What is printed when using a Union type in this function?
1from typing import Union
2
3def stringify(value: Union[int, float]) -> str:
4 return str(value * 2)
5
6print(stringify(2.5))What is the output of this code using a Callable type?
1from typing import Callable
2
3Processor = Callable[[str], str]
4
5def shout(text: str) -> str:
6 return text.upper()
7
8def run(p: Processor, value: str) -> str:
9 return p(value) + "!"
10
11print(run(shout, "hi"))What is the output of this code using a generic Box?
1from typing import Generic, TypeVar
2
3T = TypeVar('T')
4
5class Box(Generic[T]):
6 def __init__(self, item: T) -> None:
7 self.item = item
8
9 def get_item(self) -> T:
10 return self.item
11
12name_box: Box[str] = Box("Alice")
13print(name_box.get_item())Find the type-related bug in this code that uses Optional and Union.
Click on the line(s) that contain the bug.
from typing import Optional, Union def parse_age(value: Optional[str]) -> Union[int, None]: if value is None: return "unknown" return int(value)Identify the bug that would be reported by mypy in this generic Stack implementation.
Click on the line(s) that contain the bug.
from typing import Generic, List, TypeVar T = TypeVar('T') class Stack(Generic[T]): def __init__(self) -> None: self.items: List[T] = [] def push(self, item: T) -> None: self.items.append(item) def pop(self) -> T: return self.items.pop() if self.items else NoneMatch each typing construct with its most accurate description.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match the mypy behavior with the situation.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code to define a type alias for a list of user IDs and annotate the parameter correctly.
from typing import ListUserIdList = def active_user_count(user_ids: ) -> int: return len(user_ids)Click an option to fill blank 1:
Complete the code so that the function can accept either an int or a float and returns a str.
from typing import def to_label(value: ) -> str: return f"Value: {value}"Click an option to fill blank 1:
Complete the code to declare a generic Box class that stores items of any type.
from typing import , T = ('T')class Box(): def __init__(self, item: T) -> None: self.item = itemClick an option to fill blank 1:
Complete the code so that the function parameter may be a str or None and mypy knows when it's safe to call .upper().
from typing import def normalize_name(name: ) -> str: if name is None: return "" return name.()Click an option to fill blank 1:
Click the line that would cause a mypy error because of an incompatible assignment.
Click on the line to select.
from typing import List numbers: List[int] = [1, 2, 3]name: str = "Alice"name = 5print(name)Click the line where the return type does not match the annotation.
Click on the line to select.
from typing import Optional def first_letter(text: str) -> Optional[str]: if not text: return None return text