Skip to content

Avoid missing-import errors for t-strings below Python 3.14 - #22188

Open
jw9829 wants to merge 1 commit into
python:masterfrom
jw9829:fix-tstring-templatelib-target-22184
Open

jw9829 wants to merge 1 commit into
python:masterfrom
jw9829:fix-tstring-templatelib-target-22184

Conversation

@jw9829

@jw9829 jw9829 commented Oct 12, 2026

Copy link
Copy Markdown

Fixes #22184. Only add the implicit string.templatelib dependency when targeting Python 3.14 or newer.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@jw9829, this is ready to merge as it stands. The version gate on compute_dependencies is the change #22184 asked for, and I falsified it at head 1d0f834 against base 0ff0c12: with mypy/build.py reverted to base and the new case kept, the case fails with the line-1 import-not-found the issue reports.

non-blocking: a .mypy_cache written by the unfixed build keeps reporting both errors under this head only where version_id is unchanged, such as the .git-less exports below or a run under --skip-version-check. Validation accepts the cached metadata before compute_dependencies runs, and build.py:2048 abandons it on a version change. A cold cache gives one syntax error:

$ for d in head falsify base; do [ -e /scratch/p18-mypy-22188/$d/.git ] && echo "$d has .git" || echo "$d no .git"; done
head no .git
falsify no .git
base no .git
$ cd /scratch/p18-mypy-22188/cachecheck && rm -rf cache
$ PYTHONPATH=../falsify /scratch/p18-mypy-22188/venv314/bin/python -m mypy --python-version 3.13 --cache-dir=cache t1.py
t1.py:1: error: Cannot find implementation or library stub for module named "string.templatelib"  [import-not-found]
t1.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
t1.py:2: error: T-strings: requires Python 3.14 or newer (current target: Python 3.13)  [syntax]
Found 2 errors in 1 file (checked 1 source file)
$ PYTHONPATH=../head /scratch/p18-mypy-22188/venv314/bin/python -m mypy --python-version 3.13 --cache-dir=cache t1.py
t1.py:1: error: Cannot find implementation or library stub for module named "string.templatelib"  [import-not-found]
t1.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
t1.py:2: error: T-strings: requires Python 3.14 or newer (current target: Python 3.13)  [syntax]
Found 2 errors in 1 file (checked 1 source file)
$ rm -rf cache && PYTHONPATH=../head /scratch/p18-mypy-22188/venv314/bin/python -m mypy --python-version 3.13 --cache-dir=cache t1.py
t1.py:2: error: T-strings: requires Python 3.14 or newer (current target: Python 3.13)  [syntax]
Found 1 error in 1 file (checked 1 source file)

t1.py is the two-line file from #22184, ../falsify is this head with mypy/build.py reverted, ../head the pull request unmodified. The suite on Python 3.14.7, then the reverted file against the new case:

$ cd /scratch/p18-mypy-22188/head && PYTHONPATH=$PWD ../venv314/bin/python -m pytest -q -n 4 mypy/test/testcheck.py
8310 passed, 27 skipped, 7 xfailed in 72.20s (0:01:12)
$ cd ../base && PYTHONPATH=$PWD ../venv314/bin/python -m pytest -q -n 4 mypy/test/testcheck.py
8309 passed, 27 skipped, 7 xfailed in 72.47s (0:01:12)
$ cd ../falsify && PYTHONPATH=$PWD ../venv314/bin/python -m pytest -q mypy/test/testcheck.py -k TemplateStringBelowTarget
FAILED mypy/test/testcheck.py::TypeCheckSuite::check-python314.test::testTemplateStringBelowTargetDoesNotImportTemplatelib
  A: main:1: error: Cannot find implementation or library stub for module named "string.templatelib"
1 failed in 1.59s
$ cd ../head && ../venv314/bin/black --check mypy/build.py
All done! 1 file would be left unchanged.
$ ../venv314/bin/ruff check mypy/build.py
All checks passed!
# not run: mypy_primer, the mypyc-compiled jobs, dmypy; black 26.10.1 and ruff 0.17.0 locally

The dependency has one writer, mypy/build.py:3447, and both existing 3.14 cases in check-python314.test still pass, so the supported path is untouched. @ilevkivskyi, you merged 9 recent changes touching these two files. version.py:15-20 ties version_id to the git revision, so merging and a release both clear that window. I'd leave the warm-cache window out of the release notes. Do you see a case I'm missing?

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.

T-string below Python 3.14 also reports import-not-found for string.templatelib on line 1

2 participants