python-cmd2 / cmd2

cmd2 - quickly build feature-rich and user-friendly interactive command line applications in Python
https://cmd2.readthedocs.io/en/stable/
MIT License
618 stars 116 forks source link

Add type hinting #355

Closed tleonhardt closed 6 years ago

tleonhardt commented 6 years ago

Now that cmd2 no longer needs to support Python 2.7 from release 0.9.0 onward, we can begin to add type hinting to function/method arguments and return values.

Everyone is encouraged to begin doing this for all new and modified code. In the long run it should make the code easier to read and maintain.

Note: If you use type hinting for function/method arguments and/or return values, then just use type hinting. Don't put the type in a docstring comment if you are using type hinting. With tools like the mypy static analysis type checker and PyCharm, it is much easier to ensure that Python 3 optional type hints stay in sync with actual code changes. It is much more likely that the documentation comments could get out of sync.

If you modify an existing function/method to add type hinting and it previously had type info in the docstring, then please remove the type info from the docstring.

Please do NOT start adding type hinting to local variables as support for this syntax wasn't added until Python 3.6.

Also, for now please be careful to use types from the typing module which exist in Python 3.5 and the typing module backport for Python 3.4 - a lot of additional types were added to that module in Python 3.6.

tleonhardt commented 6 years ago

Supporting type hinting will require a conditional dependency on the typing module backport for Python 3.4.

I believe @anselor has already taken care of this on the autocompleter branch which incorporates type hinting in new code.