Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 10 · Production idioms (modern Python)typing.Annotated — metadata that hides BEHIND the typepredict144 / 161
+150 XP
Task
📝 **Task:** Predict the 5-line output. A `Range` metadata marker is attached to an `int` parameter via `Annotated`. The lesson then inspects the type hint via 3 helpers: `get_type_hints(include_extras=True)`, `get_origin`, `get_args`. Pay attention to what `get_origin` returns for an Annotated type — it's NOT the underlying type. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

from typing import Annotated, get_type_hints, get_args, get_origin


class Range:
    def __init__(self, lo: int, hi: int) -> None:
        self.lo = lo
        self.hi = hi

    def __repr__(self) -> str:
        return f"Range({self.lo}-{self.hi})"


def set_age(age: Annotated[int, Range(0, 120)]) -> None:
    pass


hints = get_type_hints(set_age, include_extras=True)
ann = hints["age"]

print(repr(ann))
print(get_origin(ann))
print(get_args(ann))
print(get_args(ann)[0])
print(get_args(ann)[1])

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…