Mypy Documentation Release 0.920+Dev.209A7193feb4bbfa38d09232b0a5a916e9d2e605.Dirty
Total Page:16
File Type:pdf, Size:1020Kb
Mypy Documentation Release 0.920+dev.209a7193feb4bbfa38d09232b0a5a916e9d2e605.dirty Jukka Sep 30, 2021 First steps 1 Introduction 3 2 Getting started 5 2.1 Installing and running mypy.......................................5 2.2 Function signatures and dynamic vs static typing............................6 2.3 More function signatures.........................................6 2.4 Additional types, and the typing module.................................7 2.5 Local type inference...........................................9 2.6 Library stubs and typeshed........................................9 2.7 Configuring mypy............................................ 10 2.8 Next steps................................................ 10 3 Using mypy with an existing codebase 13 3.1 Start small................................................ 13 3.2 Mypy runner script............................................ 14 3.3 Continuous Integration.......................................... 14 3.4 Annotate widely imported modules................................... 15 3.5 Write annotations as you go....................................... 15 3.6 Automate annotation of legacy code................................... 15 3.7 Speed up mypy runs........................................... 15 3.8 Introduce stricter options......................................... 15 4 Type hints cheat sheet (Python 3) 17 4.1 Variables................................................. 17 4.2 Built-in types............................................... 18 4.3 Functions................................................. 18 4.4 When you’re puzzled or when things are complicated.......................... 19 4.5 Standard “duck types”.......................................... 20 4.6 Classes.................................................. 21 4.7 Coroutines and asyncio.......................................... 21 4.8 Miscellaneous.............................................. 22 4.9 Decorators................................................ 22 5 Type hints cheat sheet (Python 2) 25 5.1 Built-in types............................................... 25 5.2 Functions................................................. 26 5.3 When you’re puzzled or when things are complicated.......................... 27 i 5.4 Standard “duck types”.......................................... 28 5.5 Classes.................................................. 29 5.6 Miscellaneous.............................................. 29 5.7 Decorators................................................ 29 6 Built-in types 31 6.1 Simple types............................................... 31 6.2 Any type................................................. 31 6.3 Generic types............................................... 32 7 Type inference and type annotations 33 7.1 Type inference.............................................. 33 7.2 Explicit types for variables........................................ 33 7.3 Explicit types for collections....................................... 34 7.4 Compatibility of container types..................................... 34 7.5 Context in type inference......................................... 35 7.6 Declaring multiple variable types at a time............................... 35 7.7 Starred expressions............................................ 35 8 Kinds of types 37 8.1 Class types................................................ 37 8.2 The Any type............................................... 37 8.3 Tuple types................................................ 38 8.4 Callable types (and lambdas)....................................... 39 8.5 Union types................................................ 40 8.6 Optional types and the None type.................................... 40 8.7 Disabling strict optional checking.................................... 43 8.8 Type aliases................................................ 44 8.9 Named tuples............................................... 44 8.10 The type of class objects......................................... 45 8.11 Text and AnyStr............................................. 46 8.12 Generators................................................ 46 9 Class basics 49 9.1 Instance and class attributes....................................... 49 9.2 Annotating __init__ methods...................................... 50 9.3 Class attribute annotations........................................ 50 9.4 Overriding statically typed methods................................... 51 9.5 Abstract base classes and multiple inheritance............................. 52 10 Annotation issues at runtime 55 10.1 String literal types............................................ 55 10.2 Future annotations import (PEP 563).................................. 56 10.3 typing.TYPE_CHECKING....................................... 56 10.4 Class name forward references...................................... 56 10.5 Import cycles............................................... 57 10.6 Using classes that are generic in stubs but not at runtime........................ 58 10.7 Using types defined in stubs but not at runtime............................. 59 10.8 Using generic builtins.......................................... 59 10.9 Using X | Y syntax for Unions...................................... 59 10.10 Using new additions to the typing module................................ 59 11 Protocols and structural subtyping 61 11.1 Predefined protocols........................................... 61 11.2 Simple user-defined protocols...................................... 65 ii 11.3 Defining subprotocols and subclassing protocols............................ 65 11.4 Recursive protocols........................................... 66 11.5 Using isinstance() with protocols.................................... 67 11.6 Callback protocols............................................ 67 12 Dynamically typed code 69 12.1 Operations on Any values........................................ 69 12.2 Any vs. object.............................................. 70 13 Type checking Python 2 code 71 13.1 Multi-line Python 2 function annotations................................ 72 13.2 Additional notes............................................. 72 14 Type narrowing 75 14.1 Type narrowing expressions....................................... 75 14.2 Casts................................................... 77 14.3 User-Defined Type Guards........................................ 78 15 Duck type compatibility 81 16 Stub files 83 16.1 Creating a stub.............................................. 83 16.2 Stub file syntax.............................................. 84 16.3 Using stub file syntax at runtime..................................... 84 17 Generics 87 17.1 Defining generic classes......................................... 87 17.2 Generic class internals.......................................... 88 17.3 Defining sub-classes of generic classes................................. 88 17.4 Generic functions............................................. 90 17.5 Generic methods and generic self.................................... 90 17.6 Variance of generic types......................................... 92 17.7 Type variables with value restriction................................... 93 17.8 Type variables with upper bounds.................................... 94 17.9 Declaring decorators........................................... 94 17.10 Generic protocols............................................. 96 17.11 Generic type aliases........................................... 97 18 More types 99 18.1 The NoReturn type............................................ 99 18.2 NewTypes................................................ 100 18.3 Function overloading........................................... 102 18.4 Advanced uses of self-types....................................... 107 18.5 Typing async/await............................................ 109 18.6 TypedDict................................................ 112 19 Literal types 117 19.1 Parameterizing Literals.......................................... 118 19.2 Declaring literal variables........................................ 118 19.3 Intelligent indexing............................................ 119 19.4 Tagged unions.............................................. 120 19.5 Exhaustive checks............................................ 121 19.6 Limitations................................................ 122 20 Final names, methods and classes 123 iii 20.1 Final names................................................ 123 20.2 Final methods.............................................. 125 20.3 Final classes............................................... 126 21 Metaclasses 127 21.1 Defining a metaclass........................................... 127 21.2 Metaclass usage example......................................... 128 21.3 Gotchas and limitations of metaclass support.............................. 128 22 Running mypy and managing imports 129 22.1 Specifying code to be checked...................................... 129 22.2 Reading a list of files from a file..................................... 130 22.3 How mypy handles imports....................................... 130 22.4 Missing imports............................................. 131 22.5 Following imports............................................ 133 22.6 Mapping file paths to modules...................................... 134 22.7 How imports are found.......................................... 135 22.8 Other advice and best practices....................................