On Sep 3, 2019, at 19:45, Steven D'Aprano <st...@pearwood.info> wrote:
> 
> On Thu, Aug 29, 2019 at 06:20:55PM +0100, Rob Cliffe via Python-ideas wrote:
> 
>>> isinstance(x, str | int) ==> "is x an instance of str or int"
>> 
>> Er, is that necessary when you can already write
>> isinstance(x, (str, int))
> 
> It's not *necessary* it's just nicer.

Definitely. It reads even more like what it means than the existing spelling, 
and it’s something novices are almost certain to expect to work, and so on.

But that implies that you can also write this:

    isinstance(x, Union[str, int])

… because, after all, str|int is defined as meaning exactly that. Which implies 
that the current rule that instantiated genetic types cannot be used for 
runtime type checks needs to be changed.

That’s certainly plausible, and maybe reasonable. But as I understand it, the 
idea that there things can’t be used for runtime type checks was a fundamental 
guiding principle to the typing design. So, it’s not something to be changed 
lightly. Someone has to go back to the reason for that principle (which may not 
be clearly stated anywhere, in which case it has to be extracted from things 
that _have_ been argued), and then make the case for why it should be violated 
here. And I haven’t seen anyone make that case. (If someone has and I missed 
it, apologies; chunks of this thread keep getting flagged as spam for some 
reason…)

If we’re lucky, it was just a matter of “We haven’t really thought it through 
beyond the collection-like generics, so let’s defer it until later because it’s 
always easier to add features than to take them away.” Then the argument is 
just “That later is now” plus the specific argument for Union (and Optional), 
which seems like a pretty easy case to make. (After all, checking Iterator[int] 
at runtime is impossible, and checking Iterator is the same useful thing that 
the long-standing ABC has always checked; neither of those is true for Union, 
where Union[int, str] is both easy to do and obviously useful and matches 
functionality that’s been in the runtime type system since at least early 2.x, 
while checking just plain Union is both new to typing and completely useless.)

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5IIX7TQXCMUB2SJOGOXDZYKBLFWCUVEQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to