Skip to content

Ensure forward references are resolved correctly in Python3.14#543

Merged
agronholm merged 9 commits intoagronholm:masterfrom
dionhaefner:master
Feb 3, 2026
Merged

Ensure forward references are resolved correctly in Python3.14#543
agronholm merged 9 commits intoagronholm:masterfrom
dionhaefner:master

Conversation

@dionhaefner
Copy link
Copy Markdown
Contributor

@dionhaefner dionhaefner commented Jan 29, 2026

Changes

Fixes #536.

The bug was that Python 3.14's ForwardRef.evaluate() method requires all referenced names to be available in the provided globals/locals dictionaries. When typeguard passed the caller's execution context (from TypeCheckMemo) instead of the module where the type was defined, forward references to types that aren't available in that context could not be resolved. Instead we now check if ForwardRef.__forward_module__ is defined, and let ForwardRef.evaluate() with default arguments figure out the resolution (which does the right thing out of the box).

Also had to fix an issue with unpacking typing.Required similar to NotRequired, in order to fix the original issue with pydantic in #536 (confirmed fixed on my machine).

Added a test that checks for both.

Checklist

If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):

  • You've added tests (in tests/) added which would fail without your patch
  • You've updated the documentation (in docs/, in case of behavior changes or new
    features)
  • You've added a new changelog entry (in docs/versionhistory.rst).

If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.

Updating the changelog

If there are no entries after the last release, use **UNRELEASED** as the version.
If, say, your patch fixes issue #999, the entry should look like this:

* Fix big bad boo-boo in the pytest plugin (#999 <https://github.com/agronholm/typeguard/issues/999>_; PR by @yourgithubaccount)

If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.

@agronholm
Copy link
Copy Markdown
Owner

Oh, nice! I had started working on this myself but I hadn't arrived at an actual fix yet, and got sidetracked with other projects.

@coveralls
Copy link
Copy Markdown

coveralls commented Jan 29, 2026

Coverage Status

coverage: 94.721% (+0.02%) from 94.705%
when pulling 6eb59fa on dionhaefner:master
into 740c1d1 on agronholm:master.

Comment thread src/typeguard/_utils.py
@agronholm
Copy link
Copy Markdown
Owner

I'll do a proper review within a few days.

Copy link
Copy Markdown
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would seem like you're mixing two different tasks here. Please send these fixes as separate PRs.

Comment thread tests/dummymodule.py Outdated
Comment thread tests/dummymodule.py Outdated
Copy link
Copy Markdown
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better now. Just resolve the remaining comments and I'll approve.

Comment thread tests/dummymodule.py Outdated
Copy link
Copy Markdown
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@agronholm agronholm merged commit 15c884b into agronholm:master Feb 3, 2026
11 checks passed
@agronholm
Copy link
Copy Markdown
Owner

Oh, and feel free to send a PR for that TypedDict fix you were working on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hitting NameError with Python 3.14 forward reference for TypedDict

3 participants