[Python-ideas] Variable-length, homogeneous tuple: why? (was: Optional static typing -- the crossroads)
Andrew Barnert
abarnert at yahoo.com
Mon Aug 18 03:05:02 CEST 2014
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?
Anyway, I agree with you that, whatever it _currently_ means in MyPy, it _should_ mean a tuple of an arbitrary number of int values.
> 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).
> Some way to specify two or more types, e.g.
> - (str, int) for consistency with isinstance, issubclass
> - Union[str, int] as used by mypy
> - str|int obvious short-cut for Union
> - str*int will make type theorists happy
That last one will not make type theorists happy. A product of two types is a tuple (your case #2, not #3). What you want is a sum or union of two types, which you'd write str|int, str U int, or maybe str+int.
> Making Tuple[] a special case troubles me, I strongly prefer Tuple[int]
> to mean a homogenous tuple of ints.
>
> mypy already has Union[str, int] for union types (giving str|int as the
> obvious short-cut), which leaves (str, int) for the hetrogenous 2-tuple
> case.
>
> Perhaps in 3.6 we can consider allowing isinstance and issubclass to
> accept Union types as well as tuples of types.
>
>
> --
> Steven
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
More information about the Python-ideas
mailing list