
The | syntax can be used in all of these places, but only in Python 3.10 and higher. Making cast() a keyword could only happen in 3.11 and would therefore not solve your problem. El vie, 25 feb 2022 a las 9:06, Jonathan Slenders (< jonathan.slenders@gmail.com>) escribió:
Hi all,
Right now, I'm struggling a bit with the new Union syntax. It seems very important to keep in mind where it can be used and where it shouldn't be used.
Some places I've seen where it can't be used:
- In type aliases: `T = A | B` - When using a cast: `x = cast(A | B, y)` - In Pydantic models. - In NewType definitions: `T = NewType('T', A | B)`
This means, that we have to teach our users to be very careful about this, and know when to use `Union[...]` or `Optional[...]` instead.
So, about `cast()` specifically, I wonder whether it would make sense to turn that into a keyword, so that it doesn't evaluate the first argument at runtime, and is completely transparent. Would that be desirable?
For the other situations, I'm not sure what we could do about those, beside quoting the type. Sorry if this has been brought up before. I wonder whether there's any consensus about this and what direction we want to go in the future?
Thanks a lot, Jonathan _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: jelle.zijlstra@gmail.com