← Back to Pull requests

Add `annotationlib`, `get_type_hints` formats and PEP 563 handling #1194

Closed
unknown closed this pull request without merging on Nov 02, 2026 13:49 UTC
View on GitHub
unknown unknown commented on Oct 30, 2026 22:00 UTC
Author

  • from __future__ import annotations is recorded on the functions and classes that follow it, so get_type_hints() evaluates their annotations as PEP 563 strings in the given namespaces only.

  • typing.get_type_hints(format=) accepts VALUE, FORWARDREF and STRING. FORWARDREF loads an unresolved name as a ForwardRef stringifier that composes source text for every operation, as CPython's annotationlib does; STRING returns the stored text.

  • New annotationlib module: Format, ForwardRef (a native type, also typing.ForwardRef) and get_annotations(), with a narrowed type stub.

  • Hint snippets always resolve names at run time, so a cached snippet sees a builtin rebound after it was compiled; every annotation evaluates before any forward reference resolves.

  • Divergences in limitations/typing.md and limitations/annotationlib.md.
  • Claude-Session: https://claude.ai/code/session_018zfZ5wGot8GsLjx4cowPZx

    <!-- This is an auto-generated description by cubic. -->
    ---

    Summary by cubic


    Adds PEP 563 handling, a new annotationlib module, and format support for typing.get_type_hints(). from __future__ import annotations was previously accepted as a no-op; the flag is now stamped on each compiled Code, inherited by exec()/eval() calls (including a global bound to exec), and persisted across REPL feeds, so get_type_hints() evaluates those annotations as PEP 563 strings in the given namespaces only. A __future__ import after any other statement is now a SyntaxError, as in CPython.

    typing.get_type_hints(format=) now accepts VALUE, FORWARDREF, and STRING. FORWARDREF returns an unresolved name as a ForwardRef stringifier that composes source text for every operation, matching CPython's annotationlib; STRING returns the stored text. The new annotationlib module provides Format, ForwardRef (also typing.ForwardRef), and get_annotations(), with a narrowed type stub.

    Behavior changes

  • Compiled class bodies end with a hidden type.__build_class__ builtin that carries the PEP 563 flag; a plain type() call doesn't, and its hand-built namespaces treat string annotations as forward references.

  • Only references a FORWARDREF evaluation created compose source text; a ForwardRef built by code an annotation calls behaves normally.

  • Hint snippets resolve names at run time, so a cached snippet sees a builtin rebound after it was compiled; every annotation evaluates before any forward reference resolves.

  • ForwardRef.evaluate() requires dict or None globals/locals for VALUE and FORWARDREF; STRING returns the text before the namespaces are validated. It honors module= by selecting that module's namespace (file module, bundled module, or __main__), and resolves a composed reference against the globals it was built in.
  • Documentation

  • Divergences recorded in limitations/typing.md and limitations/annotationlib.md.
  • <sup>Written for commit 985a117cd5c0a25d03336397b526bb386aafddac. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/pydantic/monty/pull/1194?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <a href="https://www.cubic.dev/action/auto-fix/pr/pydantic/monty/1194?returnTo=https%3A%2F%2Fgithub.com%2Fpydantic%2Fmonty%2Fpull%2F1194&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    Diff: 518 lines (+388 / -130) across 6 files Reviewer: samuelcolvin Review duration: 3828m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.53) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.53 · 518 lines diff across 6 files
    0.53
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.374
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #1194"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.7% (MEDIUM) · Higher = larger blast radius
    52.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.8% · Safe to merge
    20.8%
    Repository