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 天內回覆