get_type_hints change in behavior for Annotated with from future import annotations 3.9+
まだ誰も着手していません。
評価
調査の方向性
列挙されている Python バージョン全体で issue の Annotated と forward-reference の例を再現し、その後、報告で言及されている _eval_type パスを調査します。Annotated がある場合とない場合の動作を比較し、意図された get_type_hints の結果を判断します。動作に一貫性があるか、または回帰テストによってサポート対象のセマンティクスが明示されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Bug report
from __future__ import annotations
from typing import Sequence, Union
from typing_extensions import Annotated, get_type_hints
Config = Union[str, Sequence["Config"]]
class Foo:
x: Annotated[Config, "hi"]
print(Config)
print(get_type_hints(Foo)["x"])
The output prints are different in 3.8.13 vs 3.9.13/3.10.4/3.11.0b1. In 3.8.7 the output is,
typing.Union[str, typing.Sequence[ForwardRef('Config')]]
typing.Union[str, typing.Sequence[ForwardRef('Config')]]
In 3.9.13/3.10.4/3.11.0b1 the output is,
typing.Union[str, typing.Sequence[ForwardRef('Config')]]
typing.Union[str, typing.Sequence[typing.Union[str, typing.Sequence[ForwardRef('Config')]]]]
get_type_hints is doing 1 type expansion on forward reference. There's second inconsistency here which is that Annotated changes output in 3.9+. I'd expect get_type_hints(Foo, include_extras=False) to have same type with or without Annotated. If I drop Annotated and instead test
Config = Union[str, Sequence["Config"]]
class Foo:
x: Config
print(Config)
print(get_type_hints(Foo)["x"])
the 1 type expansion goes away and output is same as 3.8.7 output.
The from __future__ import annotations is important or you can also manually quote and do x: "Config".
The exact behavior is niche and would be understandable if this was documented as undefined implementation detail similar to how cross module aliases are awkward to work with at runtime.
Expansion affected runtime internal serialization tool I was using and I ended up needing to do some workarounds to fix tests. I haven't tried testing if pydantic/similar libraries handle this case well or not. Expansion also affects documentation produced by a tool like sphinx-autodoc-typehints.
Your environment
I'm on mac, although hope this isn't platform specific. I also tried testing from typing import Annotated instead of typing_extensions for 3.9+ and it didn't resolve inconsistency. My first guess is _eval_type is where this behavior change comes from. Newest version I tested up to was 3.11.0b1.
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 36k
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 558
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
docs pending
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
build type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
stdlib topic-email type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
-
🐛 Bug 🔔 Pending processing
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
jumpserver/jumpserver#17584 ·