Just try each type in the Union until one of them works; re-raise the last exception if none do. Make a factory for this which takes in a Union annotation and returns a function that will try all of the types so that we can just assign that function as the type directly. Remember to change the function's __name__, though, so the error messages look better (have the name be the type annotation string, e.g. Union[int, bool]).
Just try each type in the Union until one of them works; re-raise the last exception if none do. Make a factory for this which takes in a
Unionannotation and returns a function that will try all of the types so that we can just assign that function as the type directly. Remember to change the function's__name__, though, so the error messages look better (have the name be the type annotation string, e.g.Union[int, bool]).