static typing
Static typing is a form of type checking in which a program’s expressions and variables carry types that are verified before execution, usually at compile time.
A type system assigns a type to each expression and uses inference rules to decide whether a program is well typed. Code that fails the rules is rejected before running, so a successful check guarantees that certain classes of run-time error, such as calling a number as a function, can’t occur.
Programming languages apply static typing in different ways. C, C++, and early Java require explicit annotations on most declarations. Go and Rust strike a balance, requiring explicit annotations on function signatures while inferring the types of local variables.
Other languages like ML, Haskell, and OCaml apply Hindley-Milner type inference to deduce most types from how values are used. Python uses type hints, which are typically consumed by an external static type checker but don’t prevent or affect execution.
Static typing favors earlier error detection, safer large-scale refactoring, richer editor completions, and compiler optimizations.
Related Resources
Tutorial
Python Type Checking (Guide)
In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit way. Recent versions of Python allow you to specify explicit type hints that can be used by different tools to help you develop your code more efficiently.
For additional information on related topics, take a look at the following resources:
- Python 3.12 Preview: Static Typing Improvements (Tutorial)
- Python Protocols: Leveraging Structural Subtyping (Tutorial)
- Duck Typing in Python: Writing Flexible and Decoupled Code (Tutorial)
- How to Use Type Hints for Multiple Return Types in Python (Tutorial)
- Python's Self Type: How to Annotate Methods That Return self (Tutorial)
- Python 3.14: Lazy Annotations (Tutorial)
- Astral's ty: A New Blazing-Fast Type Checker for Python (Tutorial)
- Python Type Checking (Course)
- Python Type Checking (Quiz)
- Exploring Protocols in Python (Course)
- Python Protocols: Leveraging Structural Subtyping (Quiz)
- Getting to Know Duck Typing in Python (Course)
- Duck Typing in Python: Writing Flexible and Decoupled Code (Quiz)
- Using Type Hints for Multiple Return Types in Python (Course)
- Python Annotations (Quiz)
- Astral's ty Type Checker for Python (Quiz)