T-string below Python 3.14 also reports import-not-found for string.templatelib on line 1
维护者通常 1 天内回复
评估
调研方向
从 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 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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/mypy 的其他 Issue
-
Regression in 2.4.0: empty constraint tuple in PEP 695 type parameter (`class C[T: ()]`) is no longer reported可能已有人在做 @CheerathAniketh 于 3 天前认领。 未关闭topic-parser topic-pep-695
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
`stubgen --inspect-mode` crashes with `AttributeError on PEP 604 union syntax (X | Y)` *(mypy 2.1.0)*可能已有人在做 @ayushdoesdev 于 71 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
Inconsistent description of `strict_optional`可能已有人在做 @Joosboy 于 87 天前认领。 未关闭documentation
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
Cannot set `output` in `mypy.ini`可能重新可做 关联的 PR 已关闭且未合并。 未关闭bug topic-configuration topic-error-reporting
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
onyx-dot-app/onyx#16016 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
black --check exits 0 and prints All done when a multi file run is interrupted可能已有人在做 @jiteshgulati 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 2 天内回复
-
Non-positive worker counts wait for results without starting workers可能已有人在做 @IlyaKhalafi 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复