forked from Pycord-Development/pycord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_typing_annotated.py
124 lines (86 loc) · 3.25 KB
/
test_typing_annotated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from typing import Optional
from typing_extensions import Annotated
import discord
from discord import SlashCommandOptionType
from discord.commands.core import SlashCommand, slash_command
def test_typing_annotated():
async def echo(ctx, txt: Annotated[str, discord.Option()]):
await ctx.respond(txt)
cmd = SlashCommand(echo)
bot = discord.Bot()
bot.add_application_command(cmd)
dict_result = cmd.to_dict()
assert (
dict_result.get("options")[0].get("type") == SlashCommandOptionType.string.value
)
def test_typing_annotated_decorator():
bot = discord.Bot()
@bot.slash_command()
async def echo(ctx, txt: Annotated[str, discord.Option(description="Some text")]):
await ctx.respond(txt)
dict_result = echo.to_dict()
option = dict_result.get("options")[0]
assert option.get("type") == SlashCommandOptionType.string.value
assert option.get("description") == "Some text"
def test_typing_annotated_cog():
class echoCog(discord.Cog):
def __init__(self, bot_) -> None:
self.bot = bot_
super().__init__()
@slash_command()
async def echo(
self, ctx, txt: Annotated[str, discord.Option(description="Some text")]
):
await ctx.respond(txt)
bot = discord.Bot()
cog = echoCog(bot)
bot.add_cog(cog)
dict_result = cog.echo.to_dict()
option = dict_result.get("options")[0]
assert option.get("type") == SlashCommandOptionType.string.value
assert option.get("description") == "Some text"
def test_typing_annotated_cog_slashgroup():
class echoCog(discord.Cog):
grp = discord.commands.SlashCommandGroup("echo")
def __init__(self, bot_) -> None:
self.bot = bot_
super().__init__()
@grp.command()
async def echo(
self, ctx, txt: Annotated[str, discord.Option(description="Some text")]
):
await ctx.respond(txt)
bot = discord.Bot()
cog = echoCog(bot)
bot.add_cog(cog)
dict_result = cog.echo.to_dict()
option = dict_result.get("options")[0]
assert option.get("type") == SlashCommandOptionType.string.value
assert option.get("description") == "Some text"
def test_typing_annotated_optional():
async def echo(ctx, txt: Annotated[Optional[str], discord.Option()]):
await ctx.respond(txt)
cmd = SlashCommand(echo)
bot = discord.Bot()
bot.add_application_command(cmd)
dict_result = cmd.to_dict()
option = dict_result.get("options")[0]
assert option.get("type") == SlashCommandOptionType.string.value
def test_no_annotation():
async def echo(ctx, txt: str):
await ctx.respond(txt)
cmd = SlashCommand(echo)
bot = discord.Bot()
bot.add_application_command(cmd)
dict_result = cmd.to_dict()
option = dict_result.get("options")[0]
assert option.get("type") == SlashCommandOptionType.string.value
def test_annotated_no_option():
async def echo(ctx, txt: Annotated[str, "..."]):
await ctx.respond(txt)
cmd = SlashCommand(echo)
bot = discord.Bot()
bot.add_application_command(cmd)
dict_result = cmd.to_dict()
option = dict_result.get("options")[0]
assert option.get("type") == SlashCommandOptionType.string.value