python / python/typing

Allow `NotRequired[]` to be passed as a `TypeVar`

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

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

topic: feature
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

I just found myself wanting to do the following:

from typing import TypedDict, NotRequired, Generic, TypeVar, Union, Never

_T = TypeVar('_T')

Foo = Union['FooSub1[_T]', 'FooSub2[_T]', 'FooSub3[_T]']

class BaseFoo(TypedDict, Generic[_T]):
    bar: _T

class FooSub1(BaseFoo[_T]):
    sub1: str

class FooSub2(BaseFoo[_T]):
    sub2: str

class FooSub3(BaseFoo[_T]):
    sub3: str

def foo(foo_with_bar: Foo[int]):
    reveal_type(foo_with_bar) # Type of "foo_with_bar" is "FooSub1[int] | FooSub2[int] | FooSub3[int]"

def bar(foo_without_bar: Foo[NotRequired[Never]]): # error: "NotRequired" is not allowed in this context
    pass

Mypy and Pyright both disallow NotRequired to be passed as a TypeVar. However the only way to avoid doing that would require me to duplicate all subclass definitions, which I am not willing to do because I have more than a dozen and so much duplication would make the code less maintainable.

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

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

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

調査の方向性

issue の再現コードから始め、報告で名前が挙げられている 2 つのチェッカーである mypy と Pyright における NotRequired と TypeVar の現在のルールを比較してください。ファイルやテストは特定されていません。完了には、TypedDict のサブクラスを重複させずに、合意された型付けルールの設計と、それに対応するチェッカーの動作が必要です。

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

評価

技術スタック
python
領域
tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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