Aran-Fey/introspection

Union types raises

Closed this issue · 5 comments

In Python 3.10 is_typing_type(str|None) results in Expected a class or type, not str | None, where as is_typing_type(Union[str, None]) works. str|None is of type types.UnionType instead of typing._UnionGenericAlias which appears to be new in Python 3.10.

Fixed, thanks for reporting. The new version is already up on pypi, so you can easily update.

Thanks for the quick response 🎉

@Aran-Fey I might have gone to deep into the stack trace when logging this. I was actually having a problem with is_parameterized_generic. Given that Union[str, None] and str|None are equivalent, I would expect is_parameterized_generic(str|None) to return True. I seems like that can be fixed by adding types.UnionType to the isinstance check in _is_parameterized_generic. I'm not sure where else this could be problematic.

Whoops, that's my bad. I got distracted in the middle of looking for functions that might interact incorrectly with types.UnionType, and then forgot to finish that. I think I've found (and fixed) all of them now, but do let me know if you find anything else that doesn't behave the way it should.

P.S. While I was looking through these functions, I discovered some inconsistent behavior in regards to exceptions. The functions that operate on generics will now throw a ValueError instead of a TypeError if the input is a type, but not a generic type.

Thanks again 🙂