[Python-ideas] Variable-length, homogeneous tuple: why? (was: Optional static typing -- the crossroads)

Andrew Barnert abarnert at yahoo.com
Mon Aug 18 03:41:08 CEST 2014


On Sunday, August 17, 2014 4:36 PM, Łukasz Langa <lukasz at langa.pl> wrote:

>On Aug 17, 2014, at 3:46 PM, Andrew Barnert <abarnert at yahoo.com.dmarc.invalid> wrote:
>
>>Second, what else _would_ it mean? If List[str] and Set[str] mean homogeneous arbitrary-length lists and sets of strs, and the same goes for Iterable[str] and MutableSequence[str] and IO[str] and AnyStr[str] and every other example in typing.py, it would be pretty surprising if it weren't the same for Tuple[str].
>>
>
>
>You're playing the uniformity card and usually I would agree with you. In this case, there is no uniformity between different *data structures*.

There is uniformity between every single generic data structure defined by MyPy except Tuple. That makes Tuple an exceedingly special special case.

And its specialness is almost invisible. I went through typing.py in more detail, and I'm pretty sure there is nothing there to indicate that the Tuple = TypeAlias(tuple) line is going to do anything different than all of the other TypeAlias calls. Whatever difference there is must be in the private, implementation-specific code.


> Nobody expects to be able to say: int[int], dict[str], set[str: int].

But int and dict are not sequences; tuple is.

> Tuples were meant to be heterogenic, Raymond draws that distinction in many classes and talks he gives.

Sure. But as Nick pointed out, there are places all over even the core language and the stdlib where they're used homogeneously. You can argue that was a mistake, but you can't just wish it away.

>All in all, I think it's not at all confusing to say that tuple[int, int] means a point, for instance (1, 1). 

I think it's far less confusing to say that (int, int) is the type of (1, 1).

And other languages agree:

    $ ghci
    Prelude> set +t
    Prelude> (1, 1)
    (1,1)
    it :: (Integer, Integer)
    Prelude> ^D
    Leaving GHCi.
    $ xcrun swift
      1> (1, 1)

    $R0: (Int, Int) = {
      0 = 1
      1 = 1
    }
      2> ^D

Those languages don't try to make heterogeneous tuples look like parametric collection types. Why should Python?


More information about the Python-ideas mailing list