nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

Questions
2 of 5
1What is the walrus operator (:=), and in what scenarios does it improve code clarity?
2What are type hints, and does Python enforce them at runtime?
3What is a Protocol in the typing module, and how does it enable structural typing ("duck typing" with static checks)?
4How do positional-only (/) and keyword-only (*) parameter markers refine function signatures?
5What is the difference between Optional[X], Union[X, Y], and the newer X | Y syntax (PEP 604)?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
02 / 05

What are type hints, and does Python enforce them at runtime?

Difficulty: 5/10
Type Hints, Static Typing, Annotations, Mypy

Type hints are optional annotations; enforcement is done by static type checkers, not at runtime

Type hints are annotations attached to variables, function parameters, and return values. They are stored in annotations and are not checked when the code runs. A function declared def add(a: int, b: int) -> int: still accepts strings at runtime. The value of type hints comes from static analysis tools like mypy, pyright, and pyre, which read the annotations and report inconsistencies before the code runs. They also power IDE autocompletion, refactoring, and documentation. Since Python 3.5 (PEP 484), the syntax has evolved, and from future import annotations (PEP 563) makes annotations lazy strings, which avoids evaluating them at definition time. The trade-off is that you must run a type checker in CI to get any benefit, and annotations add a small maintenance cost. The most common misconception is that Python enforces types at runtime; it does not. You can use libraries like pydantic or typeguard to add runtime validation, but that is separate from the language.

  1. 1

    Annotations are metadata, not contracts. Python does not raise TypeError for a wrong argument type.

  2. 2

    Static checkers (mypy, pyright) enforce types at development time, not at runtime.

  3. 3

    from future import annotations postpones evaluation of annotations, making them strings and avoiding import cycles for type-only imports.

  4. 4

    typing.get_type_hints resolves forward references and returns the evaluated annotations.

  5. 5

    Trade-off: type hints improve safety and tooling but require discipline and a type checker in CI. Untyped code is still valid Python.

  6. 6

    Common mistake: assuming that def f(x: int) -> int: raises on f('a'). It does not.

  7. 7

    Common mistake: using mutable defaults in annotations or misunderstanding that Optional[X] means X or None, not an optional parameter.

  8. 8

    Version note: PEP 484 (3.5) introduced the typing module. PEP 563 (3.7) added postponed evaluation via future. PEP 604 (3.10) added the | operator. PEP 585 (3.9) allowed built-in generics like list[int].

javascript

Scenario Questions

0-2 years experience

  1. 1Does Python raise an error if you pass a string to a function annotated with int?
  2. 2What is the purpose of annotating a function return type?

2-5 years experience

  1. 1You add type hints and mypy reports an error, but the code runs fine. Which one is correct?
  2. 2You need to annotate a function that returns a list of dicts. How do you write it in Python 3.9+?

5-8 years experience

  1. 1You have a large codebase without type hints. How do you introduce them incrementally without blocking development?
  2. 2You need runtime validation for an API boundary. Which library do you use and why?

8+ years experience

  1. 1Design a typing strategy for a multi-package repository that includes strict mypy checks, gradual adoption, and performance considerations for annotation evaluation.
  2. 2Explain how PEP 563, PEP 649, and PEP 604 interact with runtime introspection, and how to future-proof annotations in a library.

Follow-up Questions

  • How would you add runtime validation for type hints without using a type checker?
  • What is the difference between from __future__ import annotations and typing.get_type_hints?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.