[Python-ideas] PEP 484 (Type Hints) -- first draft round

Guido van Rossum guido at python.org
Sat Jan 24 20:40:14 CET 2015


On Sat, Jan 24, 2015 at 11:04 AM, Ethan Furman <ethan at stoneleaf.us> wrote:

> I think "# type: OFF" should just ignore any existing annotations, but
> still track things such as  function parameters
> -- maybe by assigning Any to all paramaters it finds?
>

Having slept on it, that's probably best for simple, undecorated functions
and methods in vanilla classes.

For decorated functions and methods it's more complicated though -- if the
type checker doesn't understand the decorator it's not safe to assume that
the signature is unchanged. (E.g. @staticmethod, @classmethod, @property
all modify the signature -- the type checker should know about those, but
many custom decorators do this too.) Maybe for decorated functions and
methods the best it can do is to record the existence of the function name
(since the decorator syntax guarantees that), giving it the type 'Any'.

It may also have to do this for classes if it can't figure out whether
there is a custom metaclass or what a class decorator does.

But we're now out of the realm of how to disable type checking a whole
module, and I don't worry so much about this -- to some extent it's a
matter of quality of implementation how well the type checker copes with
such modules and at what level it must assume the type 'Any'.

For quickly disabling a whole module or package, we still have a choice.
Every type checker can have its own configuration mechanism that allows
selectively telling the checker to stay away from certain module, and this
should be good enough for most users; the PEP shouldn't have to specify
anything here except hint at the usefulness of such a feature.

The question remains what package owners can do if their package has code
that causes the type checker to be too noisy. They can use # type: OFF
comments (with the above caveat that there might still be noise at call
sites outside the package), or they can include stub modules (which require
some thought to get right -- even the pinfer tool often guesses wrong). Do
we want to support a "type checker shut up!" file can be present in the
package root? This may be handy to avoid noise from e.g. mypy, but I'd
imagine that e.g. PyCharm might want to ignore this and still look for
useful things to aid its completion feature.

Thinking about this more, I think we needn't do anything about this now,
and we can see how things work out once 3.5 is in active use.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150124/ab487710/attachment.html>


More information about the Python-ideas mailing list