Treat alternative syntax for unions (and other "new-semantics-old-syntax" typing features) in explicit `TypeAlias`es inside `if TYPE_CHECKING` blocks as stringized annotations.
還沒有人認領這個 Issue。
評估
研究方向
從連結的 pyright-play 和 mypy-play 範例開始,重點關注 Python 3.9、TYPE_CHECKING 以及明確標註的 TypeAliases。目前沒有確定任何 repository 檔案或測試;只有在確定這些情況應如何解讀的規格,並在相關型別檢查工具中完成相應的共識或實作工作後,才能算完成。
由索引模型根據 Issue 內容生成。
描述
The Problem
At runtime, TYPE_CHECKING is always False, only type checkers assume it is True. Therefore, if TYPE_CHECKING blocks are only parsed, but never evaluated by the Python interpreter. Since A = int | str is a valid syntax for all still-supported Python versions at the moment (all versions ever since typing was introduced, actually), it would make sense if type checkers were allowed to accept usage of "new-semantics-old-syntax" features (like the alternative union syntax) in explicitly-annotated TypeAliases inside of if TYPE_CHECKING blocks as long as the Python version specified [project.requires-python] (or it's equivalent in tools) can successfully parse that syntax. This is essentially the same as treating explicitly-annotated TypeAliases as stringized annotations by-default.
Examples
# Run on 3.9
from typing import TYPE_CHECKING
if TYPE_CHECKING:
A = int | str # Alternative syntax for unions requires Python 3.10 or newer # pyright
# Invalid type alias: expression is not a valid type # mypy
# Unsupported left operand type for | ("type[int]") # mypy
# With proposal
from typing import TYPE_CHECKING
from typing_extensions import TypeAlias
if TYPE_CHECKING:
A: TypeAlias = int | str # Treated as a stringized annotation because `: TypeAlias`
# is explicit, therefore valid.
- 主要語言
- Python
- 星號
- 1.8k
- 分支
- 302
- 平均合併
- 4 小時 38 分鐘
- 30 天內合併 PR
- 6
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/typing 的其他 Issue
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 72/100
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 75/100
-
Update reference/generics.html with PEP695 type parameter syntax可能已有人在做 @BHUVANSH855 於 186 天前認領。 未關閉topic: documentation
難度 2/5 1-3 小時 新手友好度 76/100
-
Remove PEP references in Typing Python Libraries guide可能重新可做 @Anujpandey12345 於 254 天前認領,目前沒有進行中的 PR。 未關閉topic: documentation
難度 2/5 1-3 小時 新手友好度 65/100
-
Finals and Literals: Spec contradiction可能已有人在做 @srittau 於 39 天前認領。 未關閉topic: conformance tests topic: typing spec
難度 3/5 1-2 天 新手友好度 72/100
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 82/100
aicell-lab/bioengine#232 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 74/100
modelscope/evalscope#1836 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
-
bug
難度 1/5 1 小時以內 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
jbaruch/speaker-toolkit#480 ·
維護者通常 1 天內回覆