[Python-ideas] Proposal: Use mypy syntax for function annotations
David Mertz
mertz at gnosis.cx
Thu Aug 14 05:28:19 CEST 2014
A long while back I posted a recipe for using annotations for type
checking. I'm certainly not the first person to do this, and what I did
was deliberately simple:
https://code.activestate.com/recipes/578528-type-checking-using-python-3x-annotations/?in=user-4173018
The approach I used was to use per-function decorators to say that a given
function should be type checked. The type system I enforce in that recipe
is much less than what mypy allows, but I can't see a real reason that it
couldn't be extended to cover exactly the same range of type specifiers.
The advantage I perceive in this approach is that it is purely optional,
per module and per function. As well, it doesn't actually require making
ANY change to Python 3.5 to implement it. Or as a minimal change, an extra
decorator could simply be available in functools or elsewhere in the
standard library, which implemented the full semantics of mypy.
Now admittedly, this would be type checking, but not *static* type
checking. There may not be an easy way to make a pre-runtime "lint" tool
do the checking there. On the other hand, as a number of posters have
noted, there's also no way to enforce, e.g. 'Iterable[String]' either
statically.
I'm not the BDFL of course, but I do not really get what advantage there is
to the pre-runtime check that can catch a fairly small subset of type
constraints rather than check at runtime everything that is available then
(as the decorator approach could get you).
On Wed, Aug 13, 2014 at 8:16 PM, Łukasz Langa <lukasz at langa.pl> wrote:
> On Aug 13, 2014, at 8:08 PM, Andrew Barnert <abarnert at yahoo.com> wrote:
>
> On Aug 13, 2014, at 18:56, Łukasz Langa <lukasz at langa.pl> wrote:
>
> One of my big dreams about Python is that one day we'll drop support for
> strings being iterable. Nothing of value would be lost and that would
> enable us to use isinstance(x, Iterable) and more importantly isinstance(x,
> Sequence). Funny that this surfaces now, too.
>
>
> IIRC, str doesn't implement Container, and therefore doesn't implement
> Sequence, because its __contains__ method is substring match instead of
> containment. So if you really want to treat sequences of strings separately
> from strings, you can.
>
>
> str and bytes objects respond True to both isinstance(x, Container) and
> isinstance(x, Sequence).
>
> But you’re right, off topic.
>
> --
> Best regards,
> Łukasz Langa
>
> WWW: http://lukasz.langa.pl/
> Twitter: @llanga
> IRC: ambv on #python-dev
>
>
> _______________________________________________
> 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/
>
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140813/0397a52b/attachment.html>
More information about the Python-ideas
mailing list