Skip to content
Learn Netverks

Lesson

Step 29/36 81% through track

type-hints

Type hints

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Type hints: the syntax, patterns, and safety habits you need before advancing in Python.

Type hints improve tooling and large codebases—mypy/pyright show up in mature teams.

You will apply Type hints in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Type hints annotate expected types for static checkers (mypy, pyright)—Python still enforces types at runtime dynamically. Compare with compile-time guarantees in Java or C#.

Function and variable annotations

def greet(name: str) -> str:
    return f"Hi, {name}!"

count: int = 0
items: list[str] = []

Optional and union syntax

def find_user(user_id: int) -> dict | None:
    return None

Python 3.10+ uses X | Y; older code uses Optional[X] from typing.

Important interview questions and answers

  1. Q: Do hints affect runtime?
    A: No by default—they are stored in __annotations__ for tools; use pydantic or validators for runtime checks.
  2. Q: Why use hints in dynamic Python?
    A: IDE support, documentation, and catching bugs in CI with mypy—especially in large codebases.

Self-check

  1. What tool checks hints statically?
  2. Does Python reject str + int at runtime without hints?

Tip: Run mypy in CI—hints document intent but CPython ignores them at runtime by default.

Interview prep

Runtime enforcement?

No by default—use mypy/pyright in CI; pydantic for runtime validation if needed.

Why hints in dynamic Python?

Documentation, IDE autocomplete, and catching bugs before runtime in large teams.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Optional[str]?
  • mypy value?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump