SlideShare a Scribd company logo
Static Type Checking in Python
Sebastian Wagner, swagner@suse.com
Ceph component lead
1 / 19
Where is the Problem?Where is the Problem?
2 / 192 / 19
In a nutshell
Static typing vs. dynamic typing
Type annotations
mypy - http://mypy-lang.org/
3 / 19
Would I bene t from static typing?
Project is large or complex.
code base must be maintained for a long time.
Multiple developers are working on the same code
You're learning Python.
4 / 19
Where can static typing help?
Correct arguments to functions and operators
Spelling mistakes
Type compatibility check
None - a loophole in the type system
API Documentation
Editor support
5 / 19
Editor support
Error Highlighting:
Parameter Documentation
6 / 19
Editor support (2)
Code Completion:
7 / 19
Of course, it has limitations.Of course, it has limitations.
8 / 198 / 19
Syntax
9 / 19
Syntax: Functions
def stringify(num: int) -> str:
return str(num)
def add(num1: int, my_float: float = 3.5) -> float:
return num1 + my_float
add(1, 'string')
Running mypy reveals:
error: Argument 2 to "add" has incompatible type "str"; expected "float"
10 / 19
Syntax: Variables
my_var: int = 1
my_var: float = 1.0
my_var: bool = True
my_var: str = "test"
my_var: bytes = b"test"
11 / 19
Syntax: Using Generics
from typing import List, Set, Dict, Tuple
my_var: List[int] = [1]
my_var: Tuple[int, str] = (1, 'hello')
my_var: Set[int] = {6, 7}
my_var: Dict[int, str] = {6: 'six', 7: 'seven'}
from typing import List, Union, Optional
my_var: List[Union[int, str]] = [3, 5, "test", "fun"]
my_var: Optional[str] = None
12 / 19
Syntax: Classes
from typing import List
class MyClass:
charge_percent: int = 100
def __init__(self, items: List[str]) -> None:
self.items = items
def my_method(self, num: int, str1: str) -> str:
return num * str1
13 / 19
Syntax: Escape routes
from typing import Any
my_var: Any = mystery_function()
my_var = confusing_function() # type: ignore
Don't confuse with object!
Forward References
def f(foo: 'A') -> int
...
class A:
...
14 / 19
Syntax: Python 2
def add(num1, my_float = 3.5):
# type: (int, float) -> float
return num1 + my_float
my_var = 'hello' # type: str
15 / 19
Stub les
Interface file for type hints only
.pyi
Cython bindings
Native code
typeshed
16 / 19
Legacy Code
Generate type annotations
pyannotate
MonkeyType
Focus on modules that are imported often.
Write a script that runs mypy on the whole code base
17 / 19
Questions?Questions?
18 / 1918 / 19
Thank youThank you
19 / 1919 / 19

More Related Content

KEY
Scala implicits
PPTX
Input processing and output in Python
PPT
Lập trình C
PDF
Python-02| Input, Output & Import
PPTX
Second session from learn python tutorial
PPTX
Functions of stdio conio
PPTX
First session
PPTX
Bitwise Operations in Programming
Scala implicits
Input processing and output in Python
Lập trình C
Python-02| Input, Output & Import
Second session from learn python tutorial
Functions of stdio conio
First session
Bitwise Operations in Programming

What's hot (18)

PPTX
Operators and Control Statements in Python
PPTX
Presention programming
PPTX
Error correction-and-type-of-error-in-c
PDF
Principled Error Handling - Scalapeño
DOCX
Guess a number game
PPTX
AS TASKS #8
PDF
SPL 3 | Introduction to C programming
PPT
ML: A Strongly Typed Functional Language
PPTX
Introduction to Python Programming
PDF
C++17 not your father’s c++
PDF
First c program
PPTX
Loops in Python
PPTX
PYTHON REVISION TOUR - 1
PPTX
Python for Beginners(v2)
PPTX
Arrays and Lists in C#, Java, Python and JavaScript
PPTX
Python for Data Science
PDF
Learning C programming - from lynxbee.com
Operators and Control Statements in Python
Presention programming
Error correction-and-type-of-error-in-c
Principled Error Handling - Scalapeño
Guess a number game
AS TASKS #8
SPL 3 | Introduction to C programming
ML: A Strongly Typed Functional Language
Introduction to Python Programming
C++17 not your father’s c++
First c program
Loops in Python
PYTHON REVISION TOUR - 1
Python for Beginners(v2)
Arrays and Lists in C#, Java, Python and JavaScript
Python for Data Science
Learning C programming - from lynxbee.com
Ad

Similar to Static type checking in python (20)

PPT
2. Data, Operators, IO.ppt
PDF
python lab programs.pdf
PPT
Spsl iv unit final
PPT
Spsl iv unit final
PDF
Python Basics
PPT
Data Types and Operators in C Programming
PDF
Introduction to Python Programming | InsideAIML
PPTX
Python programming workshop session 3
PPTX
MODULE. .pptx
PPTX
Revision-of-the-basics-of-python. concepts
PPT
2. Data, Operators, IO (5).ppt
PPTX
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
PPT
2. data, operators, io
PPT
2. Data, Operators, IO in C Programing and its features
DOCX
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
PPTX
Revision-of-thehki-basics-of-python.pptx
PPT
2. data, operators, io
PDF
Golang and Eco-System Introduction / Overview
PPT
Fantom on the JVM Devoxx09 BOF
PPTX
Functional programming with FSharp
2. Data, Operators, IO.ppt
python lab programs.pdf
Spsl iv unit final
Spsl iv unit final
Python Basics
Data Types and Operators in C Programming
Introduction to Python Programming | InsideAIML
Python programming workshop session 3
MODULE. .pptx
Revision-of-the-basics-of-python. concepts
2. Data, Operators, IO (5).ppt
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
2. data, operators, io
2. Data, Operators, IO in C Programing and its features
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
Revision-of-thehki-basics-of-python.pptx
2. data, operators, io
Golang and Eco-System Introduction / Overview
Fantom on the JVM Devoxx09 BOF
Functional programming with FSharp
Ad

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
System and Network Administration Chapter 2
PDF
Digital Strategies for Manufacturing Companies
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
top salesforce developer skills in 2025.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Digital Systems & Binary Numbers (comprehensive )
PPT
Introduction Database Management System for Course Database
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Introduction to Artificial Intelligence
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo Companies in India – Driving Business Transformation.pdf
System and Network Administration Chapter 2
Digital Strategies for Manufacturing Companies
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PTS Company Brochure 2025 (1).pdf.......
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Design an Analysis of Algorithms II-SECS-1021-03
top salesforce developer skills in 2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Designing Intelligence for the Shop Floor.pdf
CHAPTER 2 - PM Management and IT Context
Digital Systems & Binary Numbers (comprehensive )
Introduction Database Management System for Course Database
Upgrade and Innovation Strategies for SAP ERP Customers
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Wondershare Filmora 15 Crack With Activation Key [2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Introduction to Artificial Intelligence

Static type checking in python

  • 1. Static Type Checking in Python Sebastian Wagner, swagner@suse.com Ceph component lead 1 / 19
  • 2. Where is the Problem?Where is the Problem? 2 / 192 / 19
  • 3. In a nutshell Static typing vs. dynamic typing Type annotations mypy - http://mypy-lang.org/ 3 / 19
  • 4. Would I bene t from static typing? Project is large or complex. code base must be maintained for a long time. Multiple developers are working on the same code You're learning Python. 4 / 19
  • 5. Where can static typing help? Correct arguments to functions and operators Spelling mistakes Type compatibility check None - a loophole in the type system API Documentation Editor support 5 / 19
  • 7. Editor support (2) Code Completion: 7 / 19
  • 8. Of course, it has limitations.Of course, it has limitations. 8 / 198 / 19
  • 10. Syntax: Functions def stringify(num: int) -> str: return str(num) def add(num1: int, my_float: float = 3.5) -> float: return num1 + my_float add(1, 'string') Running mypy reveals: error: Argument 2 to "add" has incompatible type "str"; expected "float" 10 / 19
  • 11. Syntax: Variables my_var: int = 1 my_var: float = 1.0 my_var: bool = True my_var: str = "test" my_var: bytes = b"test" 11 / 19
  • 12. Syntax: Using Generics from typing import List, Set, Dict, Tuple my_var: List[int] = [1] my_var: Tuple[int, str] = (1, 'hello') my_var: Set[int] = {6, 7} my_var: Dict[int, str] = {6: 'six', 7: 'seven'} from typing import List, Union, Optional my_var: List[Union[int, str]] = [3, 5, "test", "fun"] my_var: Optional[str] = None 12 / 19
  • 13. Syntax: Classes from typing import List class MyClass: charge_percent: int = 100 def __init__(self, items: List[str]) -> None: self.items = items def my_method(self, num: int, str1: str) -> str: return num * str1 13 / 19
  • 14. Syntax: Escape routes from typing import Any my_var: Any = mystery_function() my_var = confusing_function() # type: ignore Don't confuse with object! Forward References def f(foo: 'A') -> int ... class A: ... 14 / 19
  • 15. Syntax: Python 2 def add(num1, my_float = 3.5): # type: (int, float) -> float return num1 + my_float my_var = 'hello' # type: str 15 / 19
  • 16. Stub les Interface file for type hints only .pyi Cython bindings Native code typeshed 16 / 19
  • 17. Legacy Code Generate type annotations pyannotate MonkeyType Focus on modules that are imported often. Write a script that runs mypy on the whole code base 17 / 19
  • 19. Thank youThank you 19 / 1919 / 19