Skip to content

Commit c342db8

Browse files
committed
[commands] Fix flag annotations in 3.14 using annotationlib
Fix #10349
1 parent 9580898 commit c342db8

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

discord/ext/commands/flags.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@
5050
from .context import Context
5151
from .parameters import Parameter
5252

53+
try:
54+
from annotationlib import call_annotate_function, get_annotate_from_class_namespace # type: ignore
55+
56+
def get_annotations_from_namespace(namespace: Dict[str, Any]) -> Dict[str, Any]:
57+
# In Python 3.14, classes no longer get `__annotations__` and instead a function
58+
# under __annotate__ is used instead that that takes a format argument on how to
59+
# receive those annotations.
60+
# Format 1 is full value, Format 3 is value and ForwardRef for undefined ones
61+
# So format 3 is the one we're typically used to
62+
annotate = get_annotate_from_class_namespace(namespace)
63+
if annotate is not None:
64+
return call_annotate_function(annotate, 3) # type: ignore
65+
return namespace.get('__annotations__', {})
66+
67+
except ImportError:
68+
69+
def get_annotations_from_namespace(namespace: Dict[str, Any]) -> Dict[str, Any]:
70+
return namespace.get('__annotations__', {})
71+
5372

5473
@dataclass
5574
class Flag:
@@ -177,15 +196,7 @@ def validate_flag_name(name: str, forbidden: Set[str]) -> None:
177196

178197

179198
def get_flags(namespace: Dict[str, Any], globals: Dict[str, Any], locals: Dict[str, Any]) -> Dict[str, Flag]:
180-
annotations = namespace.get('__annotations__', {})
181-
if '__annotate__' in namespace:
182-
# In Python 3.14, classes no longer get `__annotations__` and instead a function
183-
# under __annotate__ is used instead that that takes a format argument on how to
184-
# receive those annotations.
185-
# Format 1 is full value, Format 3 is value and ForwardRef for undefined ones
186-
# So format 3 is the one we're typically used to
187-
annotations = namespace['__annotate__'](3)
188-
199+
annotations = get_annotations_from_namespace(namespace)
189200
case_insensitive = namespace['__commands_flag_case_insensitive__']
190201
flags: Dict[str, Flag] = {}
191202
cache: Dict[str, Any] = {}

0 commit comments

Comments
 (0)