🐛 fix(resolver): accept guarded code the interpreter rejects - #757
Merged
gaborbernat merged 2 commits intoAug 24, 2026
Merged
Conversation
A TYPE_CHECKING block is read by type checkers, not run by the interpreter,
so it holds constructs that raise when executed. xarray writes
TypeVar("T_XarrayOther", bound="DataArray" | Dataset), which is a TypeError
at runtime. Executing the guard to recover its names hit that error, warned
about a failed type import, and then warned a second time when the
annotation using the name could not resolve. Both warnings pointed at code
that is correct.
An import that fails still warns, since a missing dependency is worth
knowing about. Any other statement now stands its names in as forward
references, which is what a type checker sees and what the renderer already
knows how to print.
Closes tox-dev#751
gaborbernat
force-pushed
the
fix-751-unexecutable-guarded-code
branch
from
August 24, 2026 14:38
339d7c9 to
e4cb9f3
Compare
for more information, see https://pre-commit.ci
|
Wow, awesome, thanks for the fixes. One question:
Does this mean that if a type checking guard has a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
TYPE_CHECKINGblock only has to satisfy a type checker, so it can hold expressions the interpreter refuses. xarray writesT_XarrayOther = TypeVar("T_XarrayOther", bound="DataArray" | Dataset), which raisesTypeError: unsupported operand type(s) for |: 'str' and 'type'the moment it runs. Executing the guard to recover its names hit that error and warnedFailed guarded type import. The annotation then reached a name nobody had bound, so a second warning followed:Cannot resolve forward reference ... name 'T_Other' is not defined. The rendered page was right the whole time. The warnings alone fail a build run with-W, which is what #751 reports.An import that raises keeps its warning, since a dependency missing from the docs environment is worth reporting, as #741 asked for. Other statements now leave their names behind as forward references, read off the assignment targets, or off the class or function name. That matches what a type checker sees, and
format_annotationprints such a reference as its own name, so the rendered output holds still while both warnings go away. ✨The stand-ins go into the module namespace the guarded code writes to, through
setdefault, so a name that already resolves keeps whatever it points at.