Skip to content

Commit 92a0fb8

Browse files
committed
Stop using Literal ordering to determine default !eval Python version
Instead of using typing.get_args(...) and fetching the first value returned (which is mostly safe, but not guaranteed to be so), use a new constant DEFAULT_PYTHON_VERSION to control the current default Python executor when no explicit interpreter version is passed.
1 parent 4ff670c commit 92a0fb8

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

bot/exts/utils/snekbox/_cog.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from bot.exts.help_channels._channel import is_help_forum_post
2121
from bot.exts.utils.snekbox._constants import (
2222
ANSI_REGEX,
23+
DEFAULT_PYTHON_VERSION,
2324
ESCAPE_REGEX,
2425
MAX_OUTPUT_BLOCK_CHARS,
2526
MAX_OUTPUT_BLOCK_LINES,
@@ -602,7 +603,7 @@ async def eval_command(
602603
) -> None:
603604
"""Run Python code and get the results."""
604605
code: list[str]
605-
python_version = python_version or get_args(SupportedPythonVersions)[0]
606+
python_version = python_version or DEFAULT_PYTHON_VERSION
606607
job = EvalJob.from_code("\n".join(code)).as_version(python_version)
607608
await self.run_job(ctx, job)
608609

@@ -643,7 +644,7 @@ async def timeit_command(
643644
) -> None:
644645
"""Profile Python Code to find execution time."""
645646
code: list[str]
646-
python_version = python_version or get_args(SupportedPythonVersions)[0]
647+
python_version = python_version or DEFAULT_PYTHON_VERSION
647648
args = self.prepare_timeit_input(code)
648649
job = EvalJob(args, version=python_version, name="timeit")
649650

bot/exts/utils/snekbox/_constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
REDO_EMOJI = "\U0001f501" # :repeat:
2020
REDO_TIMEOUT = 30
2121

22-
SupportedPythonVersions = Literal["3.14", "3.13", "3.13t"]
22+
SupportedPythonVersions = Literal["3.13", "3.13t", "3.14"]
23+
24+
DEFAULT_PYTHON_VERSION: SupportedPythonVersions = "3.14"

bot/exts/utils/snekbox/_eval.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import contextlib
44
from dataclasses import dataclass, field
55
from signal import Signals
6-
from typing import get_args
76

87
from discord.utils import escape_markdown, escape_mentions
98

109
from bot.constants import Emojis
11-
from bot.exts.utils.snekbox._constants import SupportedPythonVersions
10+
from bot.exts.utils.snekbox._constants import DEFAULT_PYTHON_VERSION, SupportedPythonVersions
1211
from bot.exts.utils.snekbox._io import FILE_COUNT_LIMIT, FILE_SIZE_LIMIT, FileAttachment, sizeof_fmt
1312
from bot.log import get_logger
1413

@@ -24,7 +23,7 @@ class EvalJob:
2423
args: list[str]
2524
files: list[FileAttachment] = field(default_factory=list)
2625
name: str = "eval"
27-
version: SupportedPythonVersions = get_args(SupportedPythonVersions)[0]
26+
version: SupportedPythonVersions = DEFAULT_PYTHON_VERSION
2827

2928
@classmethod
3029
def from_code(cls, code: str, path: str = "main.py") -> EvalJob:

tests/bot/exts/utils/snekbox/test_snekbox.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import asyncio
22
import unittest
33
from base64 import b64encode
4-
from typing import get_args
54
from unittest.mock import AsyncMock, MagicMock, Mock, call, create_autospec, patch
65

76
from discord import AllowedMentions
@@ -11,7 +10,8 @@
1110
from bot import constants
1211
from bot.errors import LockedResourceError
1312
from bot.exts.utils import snekbox
14-
from bot.exts.utils.snekbox import EvalJob, EvalResult, Snekbox, SupportedPythonVersions
13+
from bot.exts.utils.snekbox import EvalJob, EvalResult, Snekbox
14+
from bot.exts.utils.snekbox._constants import DEFAULT_PYTHON_VERSION
1515
from bot.exts.utils.snekbox._io import FileAttachment
1616
from tests.helpers import MockBot, MockContext, MockMember, MockMessage, MockReaction, MockUser
1717

@@ -22,7 +22,7 @@ def setUp(self):
2222
self.bot = MockBot()
2323
self.cog = Snekbox(bot=self.bot)
2424
self.job = EvalJob.from_code("import random")
25-
self.default_version = get_args(SupportedPythonVersions)[0]
25+
self.default_version = DEFAULT_PYTHON_VERSION
2626

2727
@staticmethod
2828
def code_args(code: str) -> tuple[EvalJob]:

0 commit comments

Comments
 (0)