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 摘要。