type of a variable is known at compile time instead of at runtime. • Examples include C, C++, Java etc. • The big benefit of static type checking is that it allows many type errors to be caught early in the development cycle.
of verifying the type safety of a program at runtime • Examples include Python, JavaScript, Ruby etc • Dynamic typing is more flexible and results in more compact programs since it doesn’t types to be spelled out
know the type Union : Use Union when something could be one of a few types Optional : Use Optional for values that could be None Iterable : Use Iterable for generic iterables
code we can create stub files (.pyi files) to declare the types of variables and methods • Stub files do not contain run time logic and the method body is replaced with ellipses. x: int def func1(code: str) -> int: ... def func2(a: int, b: int = ...) -> int: …
when running a program • The type annotations are treated as comments • Import the types you use in your type hints using the typing module • Docstrings should appear after the type comment
Python standard library and Python builtins, as well as third party packages. • Each Python module is represented by a .pyi stub. This is a normal Python file except that all the methods are empty. • This data can be used for static analysis, type checking or type inference.
catching errors before runtime • Easy to incorporate into CI • Got editor support via plugins (Pycharm, Emacs) • Active community. Not perfect but improving quickly
and now every function has two definitions • It’s not possible to annotate contents inside functions • There is no check that the implementation matches signature of the stub file • We cannot type check the code in the stub file
may encounter issues at some point • Not all (top at least) Python packages have stub files associated with them • Also try tools like Pyre, Ptype and MonkeyType • Static typing sounds like time consuming but worth it in long term