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.
まだ誰も着手していません。
評価
調査の方向性
リンクされている pyright-play と mypy-play の例から始め、Python 3.9、TYPE_CHECKING、および明示的にアノテーションされた TypeAlias に焦点を当てます。リポジトリ内のファイルやテストは特定されていません。完了とするには、これらのケースをどのように解釈すべきかについて仕様を確定し、関連する型チェックツールで合意または実装作業を行う必要があります。
索引モデルが 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
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 8
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/typing のほかの issue
-
topic: typing spec
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
topic: typing spec
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
topic: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
topic: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
topic: conformance tests topic: typing spec
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
似ている issue
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
canonical/paas-charm#368 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
tech debt
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
StevenBlack/hosts#3256 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
qualcomm/qai-appbuilder#275 ·