El jue, 16 dic 2021 a las 10:30, S Pradeep Kumar (<gohanpra@gmail.com>) escribió:
Same unpopular opinion as Никита: `Union` can actually be better when there are more than two types. `Optional` can be better when there are other special operators.

(Somewhat off-topic since PEP 677 is a draft PEP, but still about Union vs `|`. Commenting just in case it is useful and in case you have solutions.)

I discovered with the new callable syntax (PEP 677) that a naive usage of `|` can be confusing.

1. We can't naively union a callable type with another type:

```
union_of_callables: (int) -> str | (bool) -> str  # Syntax error!

union_of_callable2: ((int) -> str) | ((bool) -> str)  # Needs extra parens. Not intuitive or user-friendly.

union_of_callables3: Union[(int) -> str, (bool) -> str, (list[int]) -> str]  # Simpler and can be easily split into multiple lines, as Никита mentioned.
```

Likewise, `int | (str) -> bool` is a syntax error.

I feel like this should be legal. Is there a strong reason to disallow it?

Interestingly, TypeScript also disallows this and has a specific error message for this case: