python / python/typing

Merging/Modifying Types

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

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

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

説明

I have a decorator that modifies a class at runtime, kind of like a dataclass:

@modify
class Something:
    ...

Let's say modify adds foo onto Something, so after calling modify you could use it like so:

@modify
class Something
    ...

bar = Something.foo()

dataclass_transform doesn't support this kind of behavior at all. So, how would an API like this work?

I don't see why it would be difficult to implement some sort of Merge type, that takes in a type and "applies"" a Protocol to it. For the above example, it would work like this:

T = TypeVar("T")

class FooProtocol(Protocol):
    @classmethod
    def foo() -> None:
        ...

def modify(tp: type[T]) -> Merge[T, FooProtocol]:
    tp.foo = ...  # type: ignore
    return tp

@modify
class Something:
    ...

bar = Something.foo()

In the above example, ideally it would just sort of mark that the T is now compatible with FooProtocol, but a type: ignore is used inside of modify because it would probably be a mess to try and come up with some sort of API for actually converting a T to Merge[T, ...].

TL;DR A type that marks another type as being compatible with a protocol could be handy for making dataclass-like decorators.

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

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

はじめの一歩

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

調査の方向性

まず、提案されている Merge 型と、issue に記載されている dataclass_transform の制限を確認してください。この動作に適した場所が typing 仕様にあるか、またどのような互換性とデコレーターのセマンティクスを定義する必要があるかを判断してください。issue には、ファイル、テスト、具体的な実装のエントリーポイントはありません。

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

評価

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

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

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