Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭 适合新手
#22,184 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@jw9829 已经在做这个了。

开始于 2026年10月12日。

  • #22188 来自 @jw9829 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

从 mypy/build.py 中的 State.compute_dependencies 开始。issue 指出,当设置了 tree.uses_template_strings 时会添加 string.templatelib。检查选项中如何表示目标 Python 版本,并在 mypy/test/testcheck.py 中为目标版本为 Python 3.13 的 t-string 添加一个回归用例。完成标准是该用例只报告第 2 行的版本错误,同时 Python 3.14 的 template string 行为仍然正常。

由索引模型根据 Issue 内容生成。

描述

bug topic-parser
Bug Report

With a target below 3.14, a t-string gets the expected syntax error and also an import-not-found for string.templatelib, reported on line 1 of the file. Nothing on line 1 imports anything, and a # type: ignore on the t-string line does not silence it.

To Reproduce
y = 10
print(t"hello {y}")
Expected Behavior

Only the version error on line 2. The old parser stops at line 2 with Invalid syntax, so this is new with the native parser.

Actual Behavior
$ mypy --no-incremental --python-version 3.13 t1.py   # master at fe98edc, also 2.4.0 (compiled)
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)

State.compute_dependencies adds string.templatelib whenever tree.uses_template_strings is set (mypy/build.py:3447, from #20900). typeshed lists that module as 3.14-, so below 3.14 the dependency can't resolve, and it has no import line, so the error lands on line 1. Gating it on the target version removes the extra error:

        if self.tree.uses_template_strings and self.options.python_version >= (3, 14):
            self.add_dependency("string.templatelib")
$ mypy --no-incremental --python-version 3.13 t1.py   # with that change
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)
$ mypy --no-incremental --python-version 3.14 t4.py   # Template annotation plus reveal_type(t"{y}")
t4.py:4: note: Revealed type is "string.templatelib.Template"
Success: no issues found in 1 source file
$ pytest -q -n 4 mypy/test/testcheck.py   # with that change
8302 passed, 27 skipped, 7 xfailed in 73.00s
# no existing case covers a t-string below 3.14
Your Environment
  • Mypy version used: 2.4.0 (compiled), and master at fe98edc (not compiled)
  • Mypy command-line flags: --no-incremental --python-version 3.13 (3.12 gives the same)
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.13.15

@JukkaL I'd put the gate in compute_dependencies as above, because it is one line and does not depend on which parser built the tree. Happy to open the pull request with a check-python314.test case if you agree.

主要语言
Python
星标
20.7k
派生
3.3k
平均合并
1 天 20 小时
30 天内合并 PR
106

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/mypy 的其他 Issue

查看 python/mypy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。