mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 12:18:59 +00:00
Fix nested Annotated calls not resolving
This commit is contained in:
parent
8dd186cf1e
commit
1c7747fe9d
3
.github/workflows/test.yml
vendored
3
.github/workflows/test.yml
vendored
@ -26,8 +26,7 @@ jobs:
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip setuptools wheel "coverage[toml]" pytest pytest-asyncio pytest-cov pytest-mock
|
||||
pip install -U -r requirements.txt
|
||||
python -m pip install -e .[test]
|
||||
|
||||
- name: Run tests
|
||||
shell: bash
|
||||
|
@ -750,9 +750,6 @@ def get_supported_annotation(
|
||||
if isinstance(annotation, Transformer):
|
||||
return (annotation, MISSING, False)
|
||||
|
||||
if hasattr(annotation, '__metadata__'):
|
||||
return get_supported_annotation(annotation.__metadata__[0])
|
||||
|
||||
if inspect.isclass(annotation):
|
||||
if issubclass(annotation, Transformer):
|
||||
return (annotation(), MISSING, False)
|
||||
|
@ -160,12 +160,6 @@ def get_signature_parameters(
|
||||
if annotation is Greedy:
|
||||
raise TypeError('Unparameterized Greedy[...] is disallowed in signature.')
|
||||
|
||||
if hasattr(annotation, '__metadata__'):
|
||||
# Annotated[X, Y] can access Y via __metadata__
|
||||
metadata = annotation.__metadata__
|
||||
if len(metadata) >= 1:
|
||||
annotation = metadata[0]
|
||||
|
||||
params[name] = parameter.replace(annotation=annotation)
|
||||
|
||||
return params
|
||||
|
@ -184,11 +184,6 @@ def get_flags(namespace: Dict[str, Any], globals: Dict[str, Any], locals: Dict[s
|
||||
flag.name = name
|
||||
|
||||
annotation = flag.annotation = resolve_annotation(flag.annotation, globals, locals, cache)
|
||||
if hasattr(annotation, '__metadata__'):
|
||||
# Annotated[X, Y] can access Y via __metadata__
|
||||
metadata = annotation.__metadata__
|
||||
if len(metadata) >= 1:
|
||||
annotation = flag.annotation = metadata[0]
|
||||
|
||||
if flag.default is MISSING and hasattr(annotation, '__commands_is_flag__') and annotation._can_be_constructible():
|
||||
flag.default = annotation._construct_default
|
||||
|
@ -1062,6 +1062,11 @@ def evaluate_annotation(
|
||||
cache[tp] = evaluated
|
||||
return evaluated
|
||||
|
||||
if hasattr(tp, '__metadata__'):
|
||||
# Annotated[X, Y] can access Y via __metadata__
|
||||
metadata = tp.__metadata__[0]
|
||||
return evaluate_annotation(metadata, globals, locals, cache)
|
||||
|
||||
if hasattr(tp, '__args__'):
|
||||
implicit_str = True
|
||||
is_literal = False
|
||||
|
5
setup.py
5
setup.py
@ -39,7 +39,7 @@ extras_require = {
|
||||
'sphinx==4.4.0',
|
||||
'sphinxcontrib_trio==1.1.2',
|
||||
'sphinxcontrib-websupport',
|
||||
'typing-extensions',
|
||||
'typing-extensions>=4.3,<5',
|
||||
],
|
||||
'speed': [
|
||||
'orjson>=3.5.4',
|
||||
@ -52,7 +52,8 @@ extras_require = {
|
||||
'pytest',
|
||||
'pytest-asyncio',
|
||||
'pytest-cov',
|
||||
'pytest-mock'
|
||||
'pytest-mock',
|
||||
'typing-extensions>=4.3,<5',
|
||||
]
|
||||
}
|
||||
|
||||
|
61
tests/test_annotated_annotation.py
Normal file
61
tests/test_annotated_annotation.py
Normal file
@ -0,0 +1,61 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-present Rapptz
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import Optional
|
||||
from typing_extensions import Annotated
|
||||
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands
|
||||
|
||||
import pytest
|
||||
|
||||
def test_annotated_annotation():
|
||||
# can't exactly test if the parameter is the same, so just test if it raises something
|
||||
@app_commands.command()
|
||||
async def foo(interaction: discord.Interaction, param: Annotated[float, Optional[int]]):
|
||||
pass
|
||||
|
||||
|
||||
def to_hex(arg: str) -> int:
|
||||
return int(arg, 16)
|
||||
|
||||
class Flag(commands.FlagConverter):
|
||||
thing: Annotated[int, to_hex]
|
||||
|
||||
assert Flag.get_flags()['thing'].annotation == to_hex
|
||||
|
||||
@commands.command()
|
||||
async def bar(ctx: commands.Context, param: Annotated[float, Optional[int]]):
|
||||
pass
|
||||
|
||||
assert bar.clean_params['param'].annotation == Optional[int]
|
||||
|
||||
@commands.command()
|
||||
async def nested(ctx: commands.Context, param: Optional[Annotated[str, int]]):
|
||||
pass
|
||||
|
||||
assert nested.clean_params['param'].annotation == Optional[int]
|
||||
|
Loading…
x
Reference in New Issue
Block a user