Sample code for: Python 3.15 Preview: Lazy Imports - #808
Open
stephengruppetta wants to merge 2 commits into
Open
Sample code for: Python 3.15 Preview: Lazy Imports#808stephengruppetta wants to merge 2 commits into
stephengruppetta wants to merge 2 commits into
Conversation
added 2 commits
August 21, 2026 10:18
bench.py ignored the subprocess return code, so a script that crashed
was timed as if it had succeeded. A reader whose python isn't 3.15
would have seen cli_lazy.py die with a SyntaxError and bench.py report
it as a 2.5x speedup:
$ python3.14 bench.py cli_lazy.py --help
cli_lazy.py: 50 ms (best of 10)
That's the same silent, plausible-looking wrongness the tutorial warns
about in its own side-effects section. bench.py now checks the return
code, surfaces the captured stderr, and refuses to run at all below
3.15.
Also note in the README which files raise on purpose, and that a pyenv
build needs Tk headers or cli_eager.py won't start.
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.
Sample code for the Real Python tutorial Python 3.15 Preview: Lazy Imports.
Standard library only, but it needs Python 3.15 — most files use the
lazykeyword from PEP 810, which is aSyntaxErroron anything earlier. Every file here was run against a CPython 3.15.0rc1 build, and the tutorial quotes its real output.Layout
noisy_module.py,probe.pyshapes.py,partial.pybadfunc.pylazyIsn't Allowedreport_cli/type_checking_guard.py,lazy_annotation.pyif TYPE_CHECKINGDancefail.pycircular/bridge.py,allmode.pyreport_cli/holds three versions of the same CLI:cli_eager.py,cli_lazy.py(fivelazykeywords, nothing else changed), andcli_too_lazy.py, which also defers the two plugin imports and so silently empties the format registry.bench.pytimes any of them, and a--load-allflag reads every deferred name so you can check that deferral costs nothing once the modules are used.circular/has four self-contained folders:eager/fails,lazy/is fixed by deferring one side, andinit_eager/+init_lazy/both fail with the sameImportErrorbecause the cycle needs a value during module initialization.One change outside the new folder
pyproject.tomladdspython315-lazy-importsto ruff'sexcludelist. Two reasons:lazykeyword.ruff format --checkfails withinvalid-syntax: Simple statements must be separated by newlines or semicolonson any file using it, which would break CI.CI passes locally with the exclusion in place:
ruff format --check,ruff check, anddircheck.pyare all green.