Library › Coding AI › Software developers

Add type hints that still run on Python 3.8

Practice: Giving the right context. About 5 minutes.

About this exercise

The team wants type hints on users.py, but the batch servers still run Python 3.8, so list[str], dict[str, int] and X | None would crash at import. Use typing.List, Dict and Optional with one import line. Team style: define Row = List[str] once under the import and use List[Row] for users everywhere. Keep every function body unchanged and get find_user's return type right (it can return None). Return the 3 functions plus the import in exactly 1 code block of at most 18 lines, with under 20 words of text.

Common mistakes when you ask AI for this

  • Modern syntax on an old server. The AI writes list[str] and X | None by default, which crashes Python 3.8 at import. Try: Add: "Python 3.8: use typing.List, Dict, Optional; no list[...] or | None."
  • Ignoring the team's alias. The AI writes List[List[str]] everywhere or invents its own alias name. Try: Add: "Define Row = List[str] under the import and use List[Row]; find_user returns Optional[Dict[str, str]]."
  • Extra edits. The AI adds docstrings, a TypedDict or annotations inside the loops. Try: Add: "Only change the def lines and add one import. Bodies unchanged."
  • A long tutorial. The AI explains every hint and suggests mypy settings. Try: Add: "One code block, no text."

What a strong prompt includes

  • Names the Python version
  • Asks for typing.List style
  • Gives the Row alias
  • Protects the bodies
  • Limits the reply

Practice this now ↑

Related exercises