If this ever helps someone, for now I use the following
from annotationlib import Format, call_annotate_function # type: ignore[import-not-found]
from typing import cast
def get_namespace_annotations(namespace: dict[str, object]) -> dict[str, object]:
"""Get annotations from a namespace dict in python3.14."""
__annotations__ = cast("dict[str, object] | None", namespace.get("__annotations__"))
__annotate__ = namespace.get("__annotate__")
if __annotations__ is None:
if __annotate__ is None:
return {}
return call_annotate_function(__annotate__, Format.FORWARDREF)
# Avoid bad synchronization, suppress `__annotate__`
if __annotate__ is not None:
namespace["__annotate__"] = None
return __annotations__
which seems to work but does not sanitize __annotate__
and __annotations__
.