[Python-ideas] Proposal: Use mypy syntax for function annotations

Cory Benfield cory at lukasa.co.uk
Thu Aug 14 22:33:00 CEST 2014


On 14 August 2014 19:52, Steven D'Aprano <steve at pearwood.info> wrote:
> I want to pass a Decimal to foo(). All I have to do is *not* install
> mypy, or disable it, and lo and behold, like magic, the type checking
> doesn't happen, and foo() operates by duck-typing just like in the glory
> days of Python 1.5. Both Fred and I are now happy, and with the explicit
> isinstance check removed, the only type checking that occurs when I run
> Fred's library are the run-time duck-typing checks.

Thanks for explaining Steven, that's a lot clearer. I understand where
you're coming from now.

I still don't agree, however. I suspect what's more likely to happen
is that Fred writes his code, a user goes to run it with duck typing,
and it breaks. Assuming the static checker is in CPython and on by
default, there are a number of options here, most of which are bad:

1. The user doesn't know about the type checker and Googles the
problem. They find there's a flag they can pass to make the problem go
away, so they do. They have now learned a bad habit: to silence these
errors, pass this flag. They can no longer gain any benefits from the
type checker: it may as well have been not there (or off by default).

2. The user doesn't know about the type checker and blames Fred's
library, opening a bug report. In extreme cases, for popular
libraries, this will happen so often that Fred will either relent and
remove the annotations or get increasingly frustrated and take it out
on the users. (I'm speaking from experience in this regard.)

3. The user knows about the type checker and isn't using it. They turn
it off. Fine, this is ok.

4. The user knows about the type checker but is using it for their own
code in the same program. They're between a rock and a hard place:
either they turn off the checker and lose the benefit in their own
code, or they stop duck typing. This is actually the worst of these
cases.

Basically, my objection is to the following (admittedly extreme) case:
a static type checker that is a) present in the core distribution, b)
on by default, and c) with the only available scope being per-program.
I think that such an implementation is a recipe for having everyone
learn to turn the checker off, wasting all the effort associated with
it.

I am much happier if (b) goes away. Off by default is fine. Not in the
core distribution at all is also fine (because it's effectively
off-by-default). Allowing refined scopes is also a good idea, but
doesn't solve the core problem: people will continue to just turn it
off.

I am not averse to having static checking be an option for Python and
for annotations to be the mechanism by which such typing is done. I
just think we should be really cautious about ever including it in
CPython.


More information about the Python-ideas mailing list