Python Developer interview question

How do you use type hints in Python?

Type hints document and statically check contracts but are not enforced automatically at runtime; use precise unions, protocols, and generics where they clarify APIs, then run a type checker in CI.

Direct answer

What a strong answer covers

How it works

Type hints are annotations the interpreter ignores and a type checker such as mypy or pyright enforces. Their value is in the contracts they make explicit: Optional forces callers to handle None, Protocol describes structural interfaces without inheritance, TypedDict and dataclasses shape data, and generics keep container types precise. Runtime validation at boundaries still needs a library such as pydantic because a hint does not stop bad JSON. Adoption works best incrementally, tightening strictness module by module.

Example

A function annotated def find_user(user_id: int) -> User | None makes pyright flag every caller that uses the result without a None check. A Protocol with a single send method lets the notification service accept an email client, an SMS client, and a test double without a shared base class.

Common mistakes

  • Annotating everything as Any to silence the checker and gaining nothing.
  • Trusting hints on data parsed from a request body without validating it.
  • Turning on strict mode for the whole codebase at once and drowning in thousands of errors.

A useful answer structure

Answer from your own work.

Do not memorise a script: use only examples you can explain honestly in follow-up questions.

Practise this answer with MiraHint

Open the desktop workspace and rehearse a clear answer in your own words.

Download MiraHint
Python Developer interview questions