|
50 | 50 | from .context import Context |
51 | 51 | from .parameters import Parameter |
52 | 52 |
|
| 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 | + |
53 | 72 |
|
54 | 73 | @dataclass |
55 | 74 | class Flag: |
@@ -177,15 +196,7 @@ def validate_flag_name(name: str, forbidden: Set[str]) -> None: |
177 | 196 |
|
178 | 197 |
|
179 | 198 | 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) |
189 | 200 | case_insensitive = namespace['__commands_flag_case_insensitive__'] |
190 | 201 | flags: Dict[str, Flag] = {} |
191 | 202 | cache: Dict[str, Any] = {} |
|
0 commit comments