From c2a8dde6261af318a6e79a69d4af60fdd0d0fc63 Mon Sep 17 00:00:00 2001 From: shiftinv <8530778+shiftinv@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:57:40 +0200 Subject: [PATCH] fix(commands): resolve unstringified annotation before caching (#1120) --- changelog/1120.bugfix.rst | 1 + disnake/utils.py | 13 +++++++------ tests/test_utils.py | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 changelog/1120.bugfix.rst diff --git a/changelog/1120.bugfix.rst b/changelog/1120.bugfix.rst new file mode 100644 index 0000000000..146ac4a8de --- /dev/null +++ b/changelog/1120.bugfix.rst @@ -0,0 +1 @@ +|commands| Fix edge case in evaluation of multiple identical annotations with forwardrefs in a single signature. diff --git a/disnake/utils.py b/disnake/utils.py index 95f35003ce..1d06f137d2 100644 --- a/disnake/utils.py +++ b/disnake/utils.py @@ -1134,13 +1134,14 @@ def evaluate_annotation( if implicit_str and isinstance(tp, str): if tp in cache: return cache[tp] - evaluated = ( - eval( # noqa: PGH001, S307 # this is how annotations are supposed to be unstringifed - tp, globals, locals - ) - ) + + # this is how annotations are supposed to be unstringifed + evaluated = eval(tp, globals, locals) # noqa: PGH001, S307 + # recurse to resolve nested args further + evaluated = evaluate_annotation(evaluated, globals, locals, cache) + cache[tp] = evaluated - return evaluate_annotation(evaluated, globals, locals, cache) + return evaluated if hasattr(tp, "__args__"): implicit_str = True diff --git a/tests/test_utils.py b/tests/test_utils.py index 48ef75134a..a8f52e6b1f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -758,8 +758,8 @@ def test_normalise_optional_params(params, expected) -> None: ("Tuple[dict, List[Literal[42, 99]]]", Tuple[dict, List[Literal[42, 99]]], True), # 3.10 union syntax pytest.param( - "int | Literal[False]", - Union[int, Literal[False]], + "int | float", + Union[int, float], True, marks=pytest.mark.skipif(sys.version_info < (3, 10), reason="syntax requires py3.10"), ),