diff --git a/discord/enums.py b/discord/enums.py index 8fc139dffa..7c61ef3d49 100644 --- a/discord/enums.py +++ b/discord/enums.py @@ -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 [ @@ -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):