News for hackers. Stuff that matters.
Re:A Failed Experiment with Python Type Annotations by rcfox on Tuesday June 11, 2019 @05:48PM (#20159827)
I recently added type annotations to my Python 3.7 project and found it very helpful. Just the process of adding the annotations caught a handful of bugs!

Some things I found useful:

1. from __future__ import annotations

As I mentioned in another comment, this lets you write annotations naturally without worrying about when something gets defined.

2. typing.TYPE_CHECKING is only True while type checking.

This allows you to conditionally import files that would otherwise cause a circular dependency so that you can use the symbols for annotations.

3. def foo(bar: str = None) -> str

If an argument defaults to None, then mypy will recognize that its type is actually Optional[whatever]. So in the example above, bar is an Optional[str]. (I'm not sure if this is mypy-specific.) Optional[T] is equivalent to Union[T, None].

4. You can make typedefs easily.

MyType = Union[Sequence[str], Dict[str, int]]

Makes writing complicated annotations easier.

Re:A Failed Experiment with Python Type Annotations by yegle on Tuesday June 11, 2019 @09:11PM (#20160921)
If your code doesn't need to be py 2/3 compatible it's fine to annotate using str. But if you do, typing.Text and bytes is better.
Re:A Failed Experiment with Python Type Annotations by quietbritishjim on Wednesday June 12, 2019 @03:43AM (#20162550)
Am I missing something? Annotations aren't supported by Python 2 so if you're using them then you never need to worry about Python 2/3 compatibility.
Re:A Failed Experiment with Python Type Annotations by joshuamorton on Wednesday June 12, 2019 @04:15AM (#20162686)
Yes and no.

Annotations in comments or in type stubs are supported in python2 (you can look at typeshed for typing.Text and conditional py2/3 stuff). There's also some other cases, but they're...unique.

Re:A Failed Experiment with Python Type Annotations by rcfox on Wednesday June 12, 2019 @08:29AM (#20163831)
mypy supports comment-based annotations for Python 2. https://mypy.readthedocs.io/en/latest/python2.html
Re:A Failed Experiment with Python Type Annotations by rcfox on Tuesday June 11, 2019 @09:48PM (#20161070)
Good to know, but I hope to never need this.