[Types-sig] Sample declarations

Paul Prescod paulp@ActiveState.com
Tue, 13 Mar 2001 11:12:32 -0800

Jeremy Hylton wrote:
> If we want to add type declarations to argument lists, we need to
> document exactly what the function requires or we need to pick a
> useful set of standard types (supports __getitem__, mapping, mutable
> mapping) and fix all the code that only implements part of a
> particular type.

I think we will fail if we try to be too prescriptive and tight in our
definitions. I assert that the primary goal of the system is to check
for accidental passing of one type for another then the fact that you
might pass an incompletely implemented class is merely a logic error
like dividing by 0. That's the beauty of type systems: anything you
don't want to implement (for whatever reason) ends up being called a
logic/runtime error.

And to a certain extent this is ALWAYS going to be the situation. What
happens in Java when I don't feel like implementing a method that I
don't think I need? I just put a NotImplementedAssertion in there and
check that it never gets hit at runtime. How is that better than simply
choosing not to implement the method so that the Python *runtime* raises
the NotImplementedAssertion?

I propose that we try to work out a set of interfaces for common
protocols. If it starts to get out of hand in subtypes of subtypes of
union types of subtypes then we merely back off to a situation more
similar to the status quo. 

I think that there are two things we need to make it work a little bit

 1. A way for instances to say "HEY! I'm a mapping, just like that C
extension object over there" It is a little weird that there is a
tp_mapping slot but no __i_am_a_mapping__ flag. __interfaces__ is the
obvious mechanism but it's been held up in committee for several

 2. Documentation on the *general* requirements for mapping types. Or
perhaps the *minimal* requirements for mapping types. They can choose to
go beyond and authors can choose to use more at their own risk.

If we can also find mutually agreeable formal definitions for the
protocols then that's great. But if we back-off to the position that
anything that claims to be a mapping passes a type-check for Mapping,
that's acceptable too.

This is actually how the C API works today. How do we know if something
is a sequence? All it needs to do is claim that it is. Not every
sequence operation is guaranteed to work for every sequence-claiming
object. It's incredibly loose and would give anyone coming to the
language from a strongly-typed language the heebie-jeebies. But it seems
to work!

>>> map(None, [])
>>> map(None,{})
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: list() argument must be a sequence

Could I break the map function by passing in a "weird" sequence? Yes.
Life goes on.

Summary: agreed-upon, formal definitions of protocols are NOT a
requirement for success.

    Programming the way
    indented it.
       - (originated with Skip Montanaro?)