Read the Docs

Mypy Documentation

Release 1.4.1 Jukka

Jun 25, 2023

FIRST STEPS

1 Contents

3

1.1 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2 Type hints cheat sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

1.3 Using mypy with an existing codebase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.4 Built-in types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.5 Type inference and type annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

1.6 Kinds of types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

1.7 Class basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

1.8 Annotation issues at runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

1.9 Protocols and structural subtyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

1.10 Dynamically typed code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

1.11 Type narrowing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

1.12 Duck type compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

1.13 Stub files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

1.14 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

1.15 More types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

1.16 Literal types and Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

1.17 TypedDict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

1.18 Final names, methods and classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

1.19 Metaclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

1.20 Running mypy and managing imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

1.21 The mypy command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

1.22 The mypy configuration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

1.23 Inline configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

1.24 Mypy daemon (mypy server) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

1.25 Using installed packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

1.26 Extending and integrating mypy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

1.27 Automatic stub generation (stubgen) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

1.28 Automatic stub testing (stubtest) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

1.29 Common issues and solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

1.30 Supported Python features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

1.31 Error codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

1.32 Error codes enabled by default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

1.33 Error codes for optional checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

1.34 Additional features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

1.35 Frequently Asked Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

2 Indices and tables

211

Index

213

i

ii

Mypy Documentation, Release 1.4.1

Mypy is a static type checker for Python. Type checkers help ensure that you're using variables and functions in your code correctly. With mypy, add type hints (PEP 484) to your Python programs, and mypy will warn you when you use those types incorrectly. Python is a dynamic language, so usually you'll only see errors in your code when you attempt to run it. Mypy is a static checker, so it finds bugs in your programs without even running them! Here is a small example to whet your appetite:

number = input("What is your favourite number?") print("It is", number + 1) # error: Unsupported operand types for + ("str" and "int")

Adding type hints for mypy does not interfere with the way your program would otherwise run. Think of type hints as similar to comments! You can always use the Python interpreter to run your code, even if mypy reports errors. Mypy is designed with gradual typing in mind. This means you can add type hints to your code base slowly and that you can always fall back to dynamic typing when static typing is not convenient. Mypy has a powerful and easy-to-use type system, supporting features such as type inference, generics, callable types, tuple types, union types, structural subtyping and more. Using mypy will make your programs easier to understand, debug, and maintain.

Note: Although mypy is production ready, there may be occasional changes that break backward compatibility. The mypy development team tries to minimize the impact of changes to user code. In case of a major breaking change, mypy's major version will be bumped.

FIRST STEPS

1

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download