[commands] Fix flag annotations in 3.14 using annotationlib

Fix #10349
This commit is contained in:
Rapptz
2025-11-12 20:17:43 -05:00
parent 9580898c97
commit c342db8534

View File

@@ -50,6 +50,25 @@ if TYPE_CHECKING:
from .context import Context
from .parameters import Parameter
try:
from annotationlib import call_annotate_function, get_annotate_from_class_namespace # type: ignore
def get_annotations_from_namespace(namespace: Dict[str, Any]) -> Dict[str, Any]:
# In Python 3.14, classes no longer get `__annotations__` and instead a function
# under __annotate__ is used instead that that takes a format argument on how to
# receive those annotations.
# Format 1 is full value, Format 3 is value and ForwardRef for undefined ones
# So format 3 is the one we're typically used to
annotate = get_annotate_from_class_namespace(namespace)
if annotate is not None:
return call_annotate_function(annotate, 3) # type: ignore
return namespace.get('__annotations__', {})
except ImportError:
def get_annotations_from_namespace(namespace: Dict[str, Any]) -> Dict[str, Any]:
return namespace.get('__annotations__', {})
@dataclass
class Flag:
@@ -177,15 +196,7 @@ def validate_flag_name(name: str, forbidden: Set[str]) -> None:
def get_flags(namespace: Dict[str, Any], globals: Dict[str, Any], locals: Dict[str, Any]) -> Dict[str, Flag]:
annotations = namespace.get('__annotations__', {})
if '__annotate__' in namespace:
# In Python 3.14, classes no longer get `__annotations__` and instead a function
# under __annotate__ is used instead that that takes a format argument on how to
# receive those annotations.
# Format 1 is full value, Format 3 is value and ForwardRef for undefined ones
# So format 3 is the one we're typically used to
annotations = namespace['__annotate__'](3)
annotations = get_annotations_from_namespace(namespace)
case_insensitive = namespace['__commands_flag_case_insensitive__']
flags: Dict[str, Flag] = {}
cache: Dict[str, Any] = {}