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

Guido van Rossum guido at python.org
Mon Aug 18 03:49:59 CEST 2014


On Sun, Aug 17, 2014 at 6:05 PM, Andrew Barnert <
abarnert at yahoo.com.dmarc.invalid> wrote:

> On Sunday, August 17, 2014 4:17 PM, Steven D'Aprano <steve at pearwood.info>
> wrote:
>
> > There's two, or three, cases to consider:
> >
> > Variable sized tuple of some homogenous type
> > - e.g. (1,), (1, 2)
> > - Tuple[int] for consistency with other types
> > - mypy doesn't appear to support this
>
> Are you sure?
>
> In [typing.py](
> https://github.com/JukkaL/mypy/blob/master/lib-typing/3.2/typing.py#L17),
> Tuple is defined as TypeAlias(tuple), exactly the same way List is defined
> as TypeAlias(list). And I don't see any code anywhere else in that module
> that adds the special-casing to it.
>
> So, isn't this what MyPy is already doing? Or is there some hidden
> functionality outside of typing.py that changes it?
>

There is -- typing.py is not the typechecker, it's just a bunch of dummies
crafted so that your code can also be executed by vanilla Python 3.2. (If
you look at the definition of TypeAlias a few lines earlier, it has almost
no semantics, and you can see that it is also used for wildly other types,
e.g. Function and Union.


> Anyway, I agree with you that, whatever it _currently_ means in MyPy, it
> _should_ mean a tuple of an arbitrary number of int values.
>

I disagree. (I've said that before, but there seems to be a large delay
between our exchanges.)

The Foo[bar, ...] notation has no inherent semantics -- compare for example
Dict[str, int], Tuple[str, int], and Union[str, int]. The most useful way
to define Tuple[T1, T2, ..., Tn] is to use it for an n-tuple whose element
types are T1 etc.


> > Fixed size tuple of given hetrogeneous types
> > - e.g. (23, "abc"), (42, "xyz")
> > - mypy uses Tuple[int, str] which is a special case
>
> This is the one I'd like to write as (int, str).
>

I know that's your proposal, but it seems to blind you for the other
position.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140817/1b92438c/attachment-0001.html>


More information about the Python-ideas mailing list