4 ms·
PEP-484 Type Hints are for Python 3.5+, sorry if you are running on Python 2.x like majority of the Google python codebase does. Update: for the downvoters, th
by devy 8y ago
PEP-484 Type Hints are for Python 3.5+, sorry if you are running on Python 2.x like majority of the Google python codebase does.
Update: for the downvoters, the PEP listed the python version for 3.5 here https://www.python.org/dev/peps/pep-0484/ https://www.python.org/dev/peps/pep-0484/. Yes it does have a python 2.7 section but after Python 2.7 is discontinued pretty sure Python core team won't support type hints on 2.x. On top of that, not to mention the fact the type hint suggested on 2.x (as a comment) is different from the official 3.x syntax.
- eesmith 8y agoThat PEP has a section titled "Suggested syntax for Python 2.7 and straddling code", https://www.python.org/dev/peps/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code https://www.python.org/dev/peps/pep-0484/#suggested-syntax-f... > Some tools may want to support type annotations in code that must be compatible with Python 2.7. For this purpose this PEP has a suggested (but not mandatory) extension where function annotations are placed in a # type: comment. mypy supports those annotations for Python 2 code. Quoting http://mypy.readthedocs.io/en/latest/faq.html#how-do-i-type-check-my-python-2-code http://mypy.readthedocs.io/en/latest/faq.html#how-do-i-type-... : > How do I type check my Python 2 code? > You can use a comment-based function annotation syntax and use the --py2 command-line option to type check your Python 2 code. You’ll also need to install typing for Python 2 via pip install typing.
- epage 8y agoI fully endorse this. We've been using mypy on our Python2/Python3 project successfully. it is helpful for the type checking but I'm most interested in giving good data to IDEs for code completion.
- joshuamorton 8y agoCurious, do you mean google internal or external code? Google's been developing pytype for quite some time, which is used internally.
- ehsankia 8y agoIt's opensource and available externally tpp https://github.com/google/pytype https://github.com/google/pytype It just doesn't seem to have gotten as much traction yet.
- mundanevoice 8y agoThis is not true, type hints are supported as comments in Python2 as well.
- fermigier 8y agoActually it works quite well (using comments for type annotations) on Python 2.7 (or mixed 2.7-3.6, as we are currently using in my company) code bases.