[Python-ideas] PEP 484 (Type Hints) -- second draft
Andrew Barnert
abarnert at yahoo.com
Mon Mar 23 22:59:21 CET 2015
On Mar 23, 2015, at 2:41 PM, Cameron Simpson <cs at zip.com.au> wrote:
>
>> On 23Mar2015 04:55, Terry Reedy <tjreedy at udel.edu> wrote:
>>> On 3/23/2015 2:02 AM, David Foster wrote:
>>> * It's a bit vague how local variables should be typed in the presence of
>>> multiple assignments. Consider:
>>>
>>> ```
>>> if x > 0:
>>> root = sqrt(x) # type: int
>>> else:
>>> root = None
>>> ```
>>
>> root = sqrt(x) if x > 0 or None # type: Union(int, None)
>
> Should people need to reshape their code for this?
> In the example above it is small, though less readable already.
If this is going to be the example we keep focusing on...
Why would you have a custom int sqrt function whose return type is neither declared nor inferrable? I'd suspect that most variables that are initialized with a function call don't need a type hint, and if there are special cases where it comes up, they may be special enough that a real-life-plausible example is worth having rather than an example that's almost certainly unnecessary (or, in this case, an explicit type error, or code that type checks but only because you accidentally fooled the checker, because the last time I actually wrote, used, or wanted an int sqrt function it was in 6502 assembly...)
> But it scales very badly; what of a 4 way if etc? It moves from a bit less readable to a nightmare.
Agreed.
In fact, even when what you have is an expression in the first place, Python makes it easy (and relatively cheap) to break it up into a sequence of statements (e.g., assignments to temporary variables), and that's more often a good idea than the other way around, so typing had better work nicely for the statement version of things (and Jukka and Guido seem to have worked hard to make that true in general).
>
> Cheers,
> Cameron Simpson <cs at zip.com.au>
>
> Do not cross the oncoming lanes of death that are California's highways.
> - SJ Mercury News
> _______________________________________________
> 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