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

Since Python 3.5 (3 years ago) type hints are part of the core syntax. Don't need to put them in comments. https://www.python.org/dev/peps/pep-0484/#type-definition-sy...


They badly need to update the docs, then. Almost everything I see on that page does this:

vec = [] # type: Vector[float]

vec = [] # type: Iterable[Tuple[float, float]]

a = MyClass() # type: MyClass[int]

x = Foo() # type: Foo[int]


Ugh that's because PEP 484 (which introduced 'proper' type hints) only introduced it for parameters and return types and implemented in Python 3.5. Type hinting bare variables was introduced with PEP 526 and implemented in Python 3.6.

While I really like the concept of PEPs, and I think keeping original (like as they were written) PEPs around are the right thing to do, I do wish sometimes that Python did a better job at incorporating changes from PEPs into the core documentation once implemented.


> a_list = [] # type: List[T]

How do you do that with out a comment?


from typing import List

a_list: List[T] = []

Visit https://docs.python.org/3/library/typing.html for more information.


> a_list: List[T] = []


a_list: List[T] = []




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

Search: