[Python-ideas] Optional Static Typing -- the Python Way

Nick Coghlan ncoghlan at gmail.com
Wed Aug 20 23:25:24 CEST 2014


On 21 Aug 2014 01:53, "Guido van Rossum" <guido at python.org> wrote:
>
> Was that meant to support Antoine's position (ABCs are not type
descriptions) or mine (they are)?

Yours. The ability to register any type with any ABC means that even an
explicit isinstance() ABC check at runtime is only advisory - developers
are free to force a "true" answer if they choose to do so. You can even
disable a check entirely by registering "object" with the relevant ABC
(actually doing that would likely be a *bad idea*, but the interpreter
allows it). That puts them squarely in the "descriptions of intent"
category for me.

A bit more rambling, since I've only been skimming (at best) the type
hinting threads:

The thing I appreciate getting out of linters is picking up the "this is
almost certainly wrong" cases (like trying to call "indx" on something I
have said I expect to be a sequence). This is especially useful when code
is modified over time, but retains the original annotations documenting the
interface assumptions - if you expand the function to cover new types,
you'll need to change the annotation, which is also a good reminder that
the docs will need updating.

I'm not particularly worried if there are still cases where linters go "I
have no idea what that object is, so I'll assume anything goes". After all,
that's the baseline we start from when we don't run a linter at all.

Cheers,
Nick.

>
>
> On Wed, Aug 20, 2014 at 4:27 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>
>>
>> On 20 Aug 2014 10:14, "Guido van Rossum" <guido at python.org> wrote:
>> >
>> > On Tue, Aug 19, 2014 at 4:02 PM, Antoine Pitrou <antoine at python.org>
wrote:
>> >>
>> >> Le 19/08/2014 18:50, Terry Reedy a écrit :
>> >>
>> >>> On 8/19/2014 9:27 AM, Nick Coghlan wrote:
>> >>>
>> >>> What I like: 'optional type hints' based on a fleshed-out ABC systems
>> >>> that collects all ABCs together on one module via import from current
>> >>> files. It seems that we have been slowly groping towards this for
years.
>> >>
>> >>
>> >> Hmm, I've been saying this already, but my intuition is that it's a
bad idea to conflate *type descriptions* (what this proposal is about) and
actual *runtime types* (what ABCs are).
>> >
>> >
>> > But are they? I think the registration mechanism makes it clear that
they aren't (necessarily) runtime types -- by linking a concrete type with
an ABC through registration you are pretty clearly stating that the ABC
*describes* (an aspect of) the concrete class without automatically adding
any behavior from the ABC to it.
>>
>> One of the ways I describe the explicit registration mechanism to people
is as giving you the option of lying to the interpreter. If you use
explicit registration, your registered type basically walks around with a
sign saying "I'm a duck!" and the interpreter goes "OK, you're a duck!" and
assumes it will be able to quack without actually checking in advance. It's
really just a constrained version of normal duck typing, where the
interpreter just assumes that *everything* may exhibit duck-like tendencies.
>>
>> Cheers,
>> Nick.
>
>
>
>
> --
> --Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140821/ca478814/attachment.html>


More information about the Python-ideas mailing list