Specialized methods on generic classes
オープン
まだ誰も着手していません。
bug
topic-self-types
topic-type-variables
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
Let's say I have a generic class G.
from __future__ import annotations
from collections.abc import Sized
from typing import Generic, TypeVar
T = TypeVar("T")
class G(Generic[T]):
pass
I want to have a single method on this class that's only legal for a subset of T.
If I want to limit it to, say, int, it works:
class G(Generic[T]):
def test(self: G[int]) -> G[int]:
return self
G[int]().test() # Works, as it should
G[str]().test() # Error, as it should
But if I want to limit it to, say, a protocol like Sized, I get a false negative:
S = TypeVar("S", bound=Sized)
class G(Generic[T]):
def test(self: G[S]) -> G[S]:
return self
G[int]().test() # False negative - works, but it shouldn't
This is a pity, since it'd unlock some cool features I was working on vis-a-vis using attrs attributes. Are there any workarounds? Would this be handled by intersection types maybe?
Your Environment
- Mypy version used: mypy 1.0.0+dev.b8c03ab6809aab56928f3cd865edb44944a600a2
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: Python 3.10.6
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された Python 再現プログラムを報告された mypy バージョンで実行し、int 特殊化と Sized 制約付き特殊化の動作を比較します。次に、ジェネリックな self-type のチェック経路を追跡し、プロトコル制約付きの特殊化メソッドが意図した動作に従って受け入れられるか拒否されるかを確認する回帰テストを追加または更新します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools, tooling
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100