[Python-ideas] Type Hinting Kick-off

Brett Cannon brett at python.org
Sun Dec 21 21:44:33 CET 2014


The proposal is to standardize how to specify type hints. The *assumption*
is that static type checkers will use the type hints. Nothing is being
forced to occur at runtime; Guido's proposal is outlining how static tools
that consume the type hints and people that use them should expect things
to work.

On Sun, Dec 21, 2014, 12:32 Andrew Svetlov <andrew.svetlov at gmail.com> wrote:

> Sorry, I want to ask again.
> The proposal is for static checks only?
> My expectations for processing annotations in runtime as-is (just a
> mark without any restrictions) will not changed?
>
> On Sun, Dec 21, 2014 at 10:11 PM, Brett Cannon <brett at python.org> wrote:
> > You're workflow won't necessarily break. You need to use a tool which
> > expects type hints for function annotations to cause you any problems. If
> > you simply don't use such a tool then you will have no problems.
> >
> >
> > On Sun, Dec 21, 2014, 11:55 Andrew Svetlov <andrew.svetlov at gmail.com>
> wrote:
> >>
> >> The silly question.
> >>
> >> I use python 3 annotations for argument checking.
> >>
> >> My assumption is very simple, for
> >>
> >> def f(arg: checker): pass
> >>
> >> the checker will raise ValueError or TypeError if arg is not correct.
> >> I do it by `checker(arg)` call.
> >>
> >> I use this in aiozmq rpc
> >> (http://aiozmq.readthedocs.org/en/0.5/rpc.html#signature-validation)
> >> for example and checkers from trafaret
> >> (https://github.com/Deepwalker/trafaret) works fine.
> >>
> >> Will proposed change break my workflow?
> >>
> >> On Sun, Dec 21, 2014 at 6:53 PM, Guido van Rossum <guido at python.org>
> >> wrote:
> >> > On Sat, Dec 20, 2014 at 2:00 PM, Dennis Brakhane
> >> > <brakhane at googlemail.com>
> >> > wrote:
> >> >>
> >> >> Maybe I'm missing something, but wouldn't type hinting as it's
> defined
> >> >> now break "virtual subclassing" of ABC?
> >> >>
> >> >> For example, given the following code:
> >> >>
> >> >>   from collections import Sequence
> >> >>
> >> >>   class MySequence:
> >> >>     ...
> >> >>
> >> >>   Sequence.register(MySequence)
> >> >>
> >> >> it seems to me like the following would work:
> >> >>
> >> >>   def foo(bar):
> >> >>       if not isinstance(bar, Sequence):
> >> >>           raise RuntimeError("Foo can only work with sequences")
> >> >>       ...
> >> >>
> >> >> but when rewritten for static type checking
> >> >>
> >> >>     def foo(bar: Sequence):
> >> >>         ....
> >> >>
> >> >> it would cease to work. At least I don't see a way a static type
> >> >> checker
> >> >> could handle this relaiably (the register call might occur anywhere,
> >> >> after all)
> >> >>
> >> >> Is this intentional?
> >> >>
> >> >> Even if this might be more a mypy/implementation question, it should
> be
> >> >> clear to users of typing.py if they should expect ABCs to break or
> not
> >> >
> >> >
> >> > Well, the program will still run fine with CPython, assuming you keep
> >> > the
> >> > isinstance() check in your program. The argument declaration does not
> >> > cause
> >> > calls with non-Sequence arguments to be rejected, it just guides the
> >> > static
> >> > checker (which is a program that you must run separately, in a similar
> >> > way
> >> > as a linter -- it is not built into CPython).
> >> >
> >> > The static checker may request the call foo(MySequence()) if it cannot
> >> > see
> >> > that MySequence is a Sequence. You have two options there: if you can
> >> > change
> >> > the code of MySequence, you should just inherit from Sequence rather
> >> > than
> >> > using the register() call; if you cannot change the code of
> MySequence,
> >> > you
> >> > should use a *stub* module which declares that MySequence is a
> Sequence.
> >> > You
> >> > can read up on stub modules in the mypy docs:
> >> > http://mypy.readthedocs.org/en/latest/basics.html#library-stubs
> >> >
> >> > --
> >> > --Guido van Rossum (python.org/~guido)
> >> >
> >> > _______________________________________________
> >> > Python-ideas mailing list
> >> > Python-ideas at python.org
> >> > https://mail.python.org/mailman/listinfo/python-ideas
> >> > Code of Conduct: http://python.org/psf/codeofconduct/
> >>
> >>
> >>
> >> --
> >> Thanks,
> >> Andrew Svetlov
> >> _______________________________________________
> >> Python-ideas mailing list
> >> Python-ideas at python.org
> >> https://mail.python.org/mailman/listinfo/python-ideas
> >> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
> --
> Thanks,
> Andrew Svetlov
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141221/9d764503/attachment.html>


More information about the Python-ideas mailing list