Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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/. 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.



That PEP has a section titled "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-... :

> 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.


I 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.


Actually 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.


Curious, do you mean google internal or external code? Google's been developing pytype for quite some time, which is used internally.


It's opensource and available externally tpp

https://github.com/google/pytype

It just doesn't seem to have gotten as much traction yet.


This is not true, type hints are supported as comments in Python2 as well.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: