urbansim_templates icon indicating copy to clipboard operation
urbansim_templates copied to clipboard

Data type annotation

Open smmaurer opened this issue 5 years ago • 0 comments

Recent versions of Python have been building out support for data type annotation, where you can indicate the expected types of function inputs and outputs, or of standalone variables.

The annotations don't affect anything at runtime, but they allow static checks for consistency during development and testing, and some other cool things. Types can be simple (int, bool), compound (e.g. lists or dicts of other things), or classes.

It would be great to include type annotation throughout UrbanSim Templates! Not only does it seem like a good coding practice for library building, but it also aligns well with what the templates themselves are trying to accomplish.

The annotation syntax is compatible with Python 3.0+, but most of the useful features were introduced in Python 3.6. In Python 2.7 the annotations will raise a SyntaxError, so we'd have to drop Python 2.7 support if we add this. I think it would probably be worth it.

Resources:

  • https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html
  • https://realpython.com/python-type-checking/

Additional functionality coming in Python 3.8: https://www.python.org/dev/peps/pep-0557/

smmaurer avatar Mar 06 '19 22:03 smmaurer