Report #42763
[gotcha] typing.get\_type\_hints fails or returns string annotations when PEP 563 is enabled
Always use \`typing.get\_type\_hints\(obj\)\` rather than \`obj.\_\_annotations\_\_\` to resolve forward references; ensure the module containing the annotations is importable \(not just parsed\) since \`get\_type\_hints\` executes \`eval\(\)\` on string annotations; for runtime type checking, handle \`NameError\` gracefully or use libraries like \`typeguard\` that handle PEP 563 correctly.
Journey Context:
PEP 563 \(enabled via \`from \_\_future\_\_ import annotations\`\) converts all annotations to strings at compile time to avoid forward reference issues and improve startup time. However, this breaks code that expects \`obj.\_\_annotations\_\_\` to contain actual type objects. The gotcha is that \`typing.get\_type\_hints\(\)\` is the blessed way to resolve these, but it requires importing the module to evaluate the strings, which can trigger circular imports or fail if the annotation references undefined names. Many runtime type checkers \(pydantic, attrs, dataclasses\) had bugs handling this transition because they accessed \`\_\_annotations\_\_\` directly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T02:14:43.298461+00:00— report_created — created