python / python/cpython

Inconsistent behavior when validating type parameter substitutions

オープン
#132,100 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib topic-typing type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

There are currently three different cases (two of them being closely related) where type parameters can be substituted/parameterized with concrete types:

  • On a user-defined generic class:

    class MyGeneric[T1, T2]: ...
    
    alias = MyGeneric[int, str]
    
  • On an already parameterized alias (following the previous example):

    temp_alias = MyGeneric[int, T2]
    alias = temp_alias[str]
    
  • On a PEP 695 type alias:

    type MyAlias[T1, T2] = dict[T1, T2]
    
    gen_alias = MyAlias[int, str]
    

All of these three cases have slightly different behavior. The one that seems to be the most accurate is the second one. As alias is a _GenericAlias instance, parameterizing it will call _GenericAlias.__getitem__. There is a bunch of logic in there, and it seems that both __typing_prepare_subst__ and then __typing_subst__ (which is only doing type check assertions) are being called.

However, the first case is not making the calls to __typing_subst__, meaning the following would unexpectedly work:

class A[T, **P]: ...

A[int, str]
# ok at runtime, should fail as `P` should be substituted with a valid parameter expression
# (another ParamSpec, the ellipsis, a list/tuple of types or a Concatenate form).

If you do the same on a _GenericAlias instance (matches the second case), an error is raised:

alias = A[T, P]

alias[int, str]
# TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got <class 'str'>

This leads us to the first point: should we apply the same logic between these two cases? To avoid breaking changes, we might have to consider forward references:

class A[T, **P]: ...

A[int, 'ForwardParamSpec']

ForwardParamSpec = ParamSpec('ForwardParamSpec')

So perhaps we can exclude ForwardRef from the type check in ParamSpec.__typing_subst__ (note that it already doesn't work for the second case).

I'll also note that having __typing_subst__ not called is not the only difference. For instance, the second case also calls _unpack_args() on the passed args, while the first case doesn't [^1]


Onto the last case (PEP 695 type aliases), currently no validation is performed whatsoever:

type MyAlias[T1, T2] = dict[T1, T2]

MyAlias[int]  # no error

So the second point is: should we apply the same logic as in case 1/2? Again, I don't know if applying the same logic here on type aliases is going to introduce any breaking changes concerns?

[^1]: Here is an example of how this manifests: https://gist.github.com/Viicos/db10da58914809a87c0a82c1b2f19162

CPython versions tested on:

3.14

Operating systems tested on:

Linux

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/typing.py の _GenericAlias.getitem、ParamSpec.typing_subst、_unpack_args() から調査を開始し、続いてそれらの経路を、直接的なジェネリッククラスおよび PEP 695 の型エイリアスの置換と比較します。レポート内の例を使用して、前方参照を含めて検証を一貫させるべきかどうかを判断し、3 つのケースすべてで結果として得られる動作を確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。