ManimCommunity / manim

A community-maintained Python framework for creating mathematical animations.
https://www.manim.community
MIT License
20.94k stars 1.52k forks source link

Discussion: Enforcing Type Annotations and Run Time Typechecking #1212

Open cobordism opened 3 years ago

cobordism commented 3 years ago

1: Enable Typechecking in CI We have recently begun adding type hints to our code. However we currently don't check whether the type hints are correct, nor whether they are followed. There are python type checkers such as mypy and I wish to suggest that we enable typechecking as part of our CI. http://mypy-lang.org/ (alternative: https://github.com/google/pytype)

2: Automatically check types of function arguments Another feature that has been cropping up in our code is a bunch of functions called _typecheck_inputs() that do nothing else than check the types of the variables passed to a function as arguments and give an error if the type is not the one expected.

This seems to me to be a wasteful repeat of effort. There are ways to enforce at runtime that types passed to a function conform to the type hints. (examples: pydantic, pytypes, enforce). https://github.com/RussBaz/enforce

I suggest we use one of those libraries instead of writing a separate _typecheck_inputs for every class and every function that only serves to redo what the type hints already provide.

3: Discuss see below ;)

cobordism commented 3 years ago

Typecheckers: http://mypy-lang.org/ https://pyre-check.org/ https://github.com/google/pytype

Input Validation: https://github.com/samuelcolvin/pydantic https://github.com/Stewori/pytypes https://github.com/RussBaz/enforce

Other things to look at: https://github.com/Instagram/MonkeyType https://realpython.com/python-type-checking/#static-type-checking https://miltos.allamanis.com/publications/2020typilus/ https://github.com/marketplace/actions/typilus-suggest-python-type-annotations