Don't exclude imports in the else branch of TYPE_CHECKING guards - #324
Open
pylaterreur wants to merge 1 commit into
Open
pylaterreur wants to merge 1 commit into
pylaterreur wants to merge 1 commit into
Conversation
With exclude_type_checking_imports, every import inside an `if TYPE_CHECKING:` statement was treated as type checking only, including the imports in its `elif` and `else` branches. Those branches run when TYPE_CHECKING is false, that is, at runtime, so their imports were wrongly left out of the graph, and any contracts they break were kept. Only treat the branches guarded by TYPE_CHECKING as type checking only. This also recognises `elif TYPE_CHECKING:` again, as the import scanner did before it was ported to Rust, and stops a nested `if TYPE_CHECKING:` from marking the rest of the enclosing guard as runtime code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
Merging this PR will not alter performance
Comparing Footnotes
|
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.
Fixes #323.
With
exclude_type_checking_imports=True, the imports in theelif/elsebranches of anif TYPE_CHECKING:statement were excluded along with the guarded ones, even though those branches run at runtime. Contracts that such imports break were therefore kept. For example,build_graph("filelock", exclude_type_checking_imports=True)has no import fromfilelocktofilelock._read_writeorfilelock._async_read_write, whichfilelock/__init__.pyimports in such anelsebranch.The import parser now only marks the branches guarded by
TYPE_CHECKINGas type checking only:elsebranches, andelifbranches with other conditions, are runtime code.elif TYPE_CHECKING:branches are type checking only, as they were with the Python import scanner.if TYPE_CHECKING:restores the enclosing state when it ends, rather than resetting it to "not type checking".Graphs built without
exclude_type_checking_importsare unchanged.On the installed versions of filelock, numpy, aiohttp, propcache, docutils, anyio and packaging (with
include_external_packages=True), this adds 14 runtime imports that were missing, such asfilelock -> filelock._read_write,numpy._typing._array_like -> numpy._core.multiarrayandaiohttp.connector -> ssl. It also removes 2 type checking only ones that were kept:packaging.version -> typing_extensionsandpackaging.specifiers -> typing_extensions, which are imported underelif TYPE_CHECKING:.There's no measurable performance change. Building the graph of nine installed packages (1,920 modules, release builds, one thread, with
exclude_type_checking_imports=True) takes 3,399M instructions before and 3,401M after, within the run-to-run noise of about 2M. The instructions spent in the import visitor go down by 0.4M.latestsection at the top ofCHANGELOG.rst. (If it's not there, add it.)AUTHORS.rst.just full-check. (I ran the lint (Python, plus Rust with the pinned 1.97.0 toolchain), the docs build,cargo test, and the Python tests on 3.10 to 3.14. I didn't run them on 3.14t, 3.15 or 3.15t.)🤖 Generated with Claude Code
https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN