Python is DOOMED! Again!

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jan 28 03:05:49 CET 2015


Mario Figueiredo wrote:

> Looking at PEP 3107, i'm left wondering: what if I have for instance
> already annotated my functions for parameter marshalling, following the
> syntax expected of that specific library, and now I want to annotate
> them for type hinting for the purposes of static analysis?

You cannot simultaneously follow two competing standards.

If you have a library that expects annotations to represent (let's say)
parameter marshalling, and another library that expects annotations to
represent documentation, and a third library that expects annotations to
represent hyperlinks to external resources, and a fourth that expects them
to be used for range checking, would you expect to be able to use all four
competing and incompatible uses of annotations in the same function at the
same time? Or even any two such libraries at the same time?

Of course not. At best, each library will ignore annotations that it doesn't
understand. Possibly they will print warnings, or raise exceptions, and
force you to choose between them. At worst, the libraries will misinterpret
the annotation and do the wrong thing, with consequences ranging from
annoying to disastrous. But unless all four libraries are designed from the
start to cooperate, you cannot expect them to cooperate.

Why should type hints be held to a higher (and impossible) standard?

If you use annotations for some other purposes, your choices post-PEP 484
will become:

- Do absolutely nothing different, and your annotations will continue to
work exactly as they currently do. You're not currently running a
type-checker which expects annotations to be type-hints, so you merely
continue *not running* such a type-checker.

Or, if you decide that you do want to run a type-checker:

- Find a type-checker that doesn't use annotations, and use whatever rules
it uses. I believe some linters (obiwan comes to mind?) may use type hints
in docstrings to do simple type checking. Find such a tool, or write your
own, and use it.

- Find a type-checker which can cooperatively interoperate with your
existing use of annotations, if such a thing exists. Feel free to write one
if you wish, or pony up the money to pay somebody else to do so.

- Use a stub file, and a type-checker which will ignore the annotations and
use the stub file instead.

- Give up on your dream of lexical analysis of Python. You can't have
everything, and your Python experience will be just like 2014 all over
again.

- Give up on your current use of annotations, and replace them with
type-hints.

- Change to some other language that does everything you want, and re-write
your entire application in this new language.

- Stamp your feet and hold your breath until you collapse and see if that
helps. (It probably won't, but my two year old nephew[1] thinks it does.)

PEP 484 gives you many choices, starting with "do absolutely nothing, and
absolutely nothing will change".





[1] Actually, that's a gross slander. For somebody right in the middle of
the terrible twos, he's relatively sweet natured and well behaved.

-- 
Steven




More information about the Python-list mailing list