Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix py3.10 UnionType checks #1240

Merged
merged 1 commit into from
Apr 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,13 @@ def from_datatype(cls, datatype):
else:
raise TypeError("Invalid usage of typing.Union")

py_3_10_union_type = hasattr(types, "UnionType") and isinstance(datatype, types.UnionType)

if py_3_10_union_type or getattr(datatype, "__origin__", None) is Union:
# Python 3.10+ "|" operator or typing.Union has been used. The __args__ attribute is a tuple of the types.
# Type checking fails for this case, so ignore it.
return cls.from_datatype(datatype.__args__) # type: ignore

if datatype.__name__ in ["Member", "User"]:
return cls.user
if datatype.__name__ in [
Expand All @@ -669,11 +676,6 @@ def from_datatype(cls, datatype):
if datatype.__name__ == "Mentionable":
return cls.mentionable

if isinstance(datatype, types.UnionType) or getattr(datatype, "__origin__", None) is Union:
# Python 3.10+ "|" operator or typing.Union has been used. The __args__ attribute is a tuple of the types.
# Type checking fails for this case, so ignore it.
return cls.from_datatype(datatype.__args__) # type: ignore

if issubclass(datatype, str):
return cls.string
if issubclass(datatype, bool):
Expand Down