0

Specifying multiple datatypes for an argument / Invalid usage of typing.Union

Hi, I was trying to write a discord bot in python, using pycord with slash commands I require a command to take either a string or an object(discord.Member) as an argument. (Datatypes must be specified for proper handling) So function could not be overloaded and optional arguments are out of the question, but an argument should be of either discord.Membre type or string I tried using typing.Union for that, as follows: -------------------------------------- @bot.command( guild_ids=server_ids, name="who", description="Get information about someone's nation" ) async def _who( ctx: discord.ApplicationContext, member: typing.Union[discord.Member, str] ): -------------------------------------- It gives me the following error: -------------------------------------- raise TypeError("Invalid usage of typing.Union") TypeError: Invalid usage of typing.Union -------------------------------------- Any suggestions?

8th Jul 2022, 10:00 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
3 Respuestas
+ 1
You can use the isinstance function to check from which class (str or discord.Member) the argument is an instance of
8th Jul 2022, 3:42 PM
Emerson Prado
Emerson Prado - avatar
+ 1
According to their documentation you did it right: https://docs.pycord.dev/en/master/ext/commands/commands.html#typing-union I am not using pycord myself, but i can suggest you checking their github for more examples or ask in their discord server (linked in the following link): https://github.com/Pycord-Development/pycord
8th Jul 2022, 11:46 AM
Herr Rozwel
Herr Rozwel - avatar
0
@Emerson Prado Thanks a lot! That helped :)
9th Jul 2022, 11:05 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar