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

Guido van Rossum guido at python.org
Mon Aug 18 01:17:39 CEST 2014


I sent that a little too soon; I should add that I think this is the right
way; and that's why I keep suggesting a different way to spell a
homogeneous variable-length tuple. 1-tuples should not be special.


On Sun, Aug 17, 2014 at 4:15 PM, Guido van Rossum <guido at python.org> wrote:

> I still think you are mistaken. I don't think mypy has a way to spell a
> homogeneous arbitrary-length tuple. All uses of Tuple[...] refer to
> "anonymous struct" tuples.
>
> I tried this:
>
> from typing import Tuple
>
> def f(a: Tuple[int]) -> None:
>     pass
>
> def main() -> None:
>     f((1,))
>     f((1, 2))
>
> and I get an error for the second call, f((1, 2)):
>
> a.py: In function "main":
> a.py, line 8: Argument 1 to "f" has incompatible type "Tuple[int, int]";
> expected "Tuple[int]"
>
>
>
> On Sun, Aug 17, 2014 at 3:46 PM, Andrew Barnert <
> abarnert at yahoo.com.dmarc.invalid> wrote:
>
>> On Sunday, August 17, 2014 1:34 PM, Guido van Rossum <guido at python.org>
>> wrote:
>>
>> >Where is it said that Tuple[int] is a homogeneous variable size list?
>>
>>
>> (I'm assuming you're referring to the homogeneity and arbitrary length
>> here, not the fact that someone presumably said "list" when they meant
>> "tuple", because otherwise the answer is trivial…)
>>
>> First, that's how the current typing.py interprets it: Tuple[str] is a
>> homogeneous, arbitrary-length (although of course unchanging, because it's
>> immutable) tuple of strings.
>>
>>
>> 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].
>>
>> Third, if it didn't mean that, how would you define the argument types to
>> any of Nick's examples? For example:
>>
>>     def isinstance(obj: object, types: type | Tuple[type]) -> bool:
>>
>> That had better mean a homogeneous arbitrary-length tuple of types; if
>> not, there doesn't seem to be any other way to declare its type.
>>
>
>
>
> --
> --Guido van Rossum (python.org/~guido)
>



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


More information about the Python-ideas mailing list