Python3 Type Checking
Scaling Python
Python is a great language for prototype development, but sometimes we want to develop production software with Python or we suffer the most recent cycle of prototypes becoming production. This is where the attributes that made Python a strong prototyping tool often interfere with the requirements of enterprise software (e.g. high reliability, complex deployment, multiple developers). Unittests go a long way towards ensuring robust execution of key mechanisms, but how can we catch typos in new code or nuanced return values?
Type Verification
PEP 3107 defined a grammar for arbitrary annotations on functions and variables across for Python 3.0+ and PEP 484 standardized the framing and interpretation for type annotations. Neither enforced any type-checking behavior, but instead made the type annotations available at run-time (via the __annotations__
attribute). mypy provides validation of type annotations with a reasonable type inference system that propagates types where appropriate (except for lists/dicts).
Example (from here)
import typing def read(vStream : str) -> typing.Tuple[int, int]: rval = 0 for idx in range(len(vStream)): rval = (rval * 128) + ord(vStream[idx]) if not ord(vStream[idx]) & 128: return (idx+1,rval) rval = rval - 128 + 1 return (0,rval)
September 15, 2019