🏷️
PEP 484 · Speedrun · Python 3.5 (2015)
PEP 484 in 90 seconds — type hints, without TypeScript-envy
Python stays dynamic — but you can opt into typing.
Python is duck-typed. PEP 484 (2015) added an **optional** syntax for declaring types: ```python # Before def add(a, b): return a + b # After PEP 484 def add(a: int, b: int) -> int: return a + b ``` The interpreter ignores hints at runtime. So why did everyone adopt them?
Because the ecosystem grew teeth around them: - **mypy** flags type errors before runtime - **VS Code / PyCharm** use them for autocomplete + jump-to-definition - **Pydantic v2** validates incoming data at runtime - **FastAPI** generates OpenAPI docs and request parsers from them Python stays dynamic; tooling makes it feel typed.
Before — no hints (still valid Python)
def greet(name, times):
# Is name a str? a list? a number?
# Is times an int? Required? Optional?
# The function works; the reader has to guess.
return name * timesAfter — with PEP 484 hints
def greet(name: str, times: int = 1) -> str:
# Reader (and mypy, and your IDE) know:
# - name is a string
# - times is an int, defaults to 1
# - the function returns a string
return name * times🎯 Predict the output
What does this print? Pay attention — Python ignores hints at runtime.
def add(a: int, b: int) -> int:
return a + b
print(add("foo", "bar"))
print(add(2, 3))