[Python-Dev] Type hints -- a mediocre programmer's reaction

Guido van Rossum guido at python.org
Thu Apr 23 02:45:13 CEST 2015


On Wed, Apr 22, 2015 at 2:38 PM, Chris Barker <chris.barker at noaa.gov> wrote:

>
>
> Oh wait, maybe it won't -- a string IS a sequence of strings. That's why
>> this is an insidious bug in the first place.
>
> On Tue, Apr 21, 2015 at 11:32 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>
>
>> I was just thinking today that for this, typing needs a subtraction
>> (difference) operation in addition to an addition (union) operation:
>> Difference(Iterable(str), str)
>>
>
> Yup -- that might solve, it, but it feels a bit odd -- I can take any
> Iterable of string, except a string. -- but what if there are others that
> won't work??? But I guess that's the core of putting type hints on a
> dynamic language.
>
> Still, I tend to think that this particular issue is really a limitation
> with Python's type system -- nothing to do with type hinting.
>
> I can see that a character type seems useless in Python, but there are
> lessons from other places: a numpy array is a collection of (usually)
> numbers that can be treated as a single entity -- much like a string is a
> collection of characters that is treated as a single entity -- in both
> cases, it's core to convenience and performance to do that. But with numpy,
> when you index an array, you get something back with one less dimension:
>
> index into a 3-d array, you get a 2-d array
> index into a 2-d array, you get a 1-d array
> index into a 1-d array, you get a scalar -- NOT a length-one 1-d array
>
> Sometimes this is a pain for generic code, but more often than not it's
> critical to writing dynamic code -- not because you couldn't do the
> operations you want, but because it's important to distinguish between a
> scalar and an array that happens to have only one value.
>
> Anyway, the point is that being able to say "all these types, except this
> one" would solve this particular problem -- but would it solve any others?
> Do we want this to work around a quirk in Pythons string type?
>
> NOTE: I know full well that adding a character type to Python is not worth
> it.
>

If you switch to bytes the problem goes away. :-P

More seriously, I doubt there are other important use cases for Difference.

Given that even if Difference existed, and even if we had a predefined type
alias for Difference[Iterable[str], str], you' still have to remember to
mark up all those functions with that annotation. It almost sounds simpler
to just predefine this function:

def make_string_list(a: Union[str, Iterable[str]]) -> Iterable[str]:
    if isinstance(a, str):
        return [a]
    else:
        return a

and call this in those functions that have an Interable[str] argument. Now
instead of getting errors for all the places where a caller mistakenly
passes a single str, you've *fixed* all those call sites. Isn't that more
Pythonic? :-)

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


More information about the Python-Dev mailing list