Merging/Modifying Types
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
先檢視提議的 Merge 型別,以及 issue 中所描述的 dataclass_transform 限制。判斷 typing 規範是否有適合這項行為的位置,以及需要定義哪些相容性與裝飾器語義;issue 未提供檔案、測試或具體的實作入口。
由索引模型根據 Issue 內容生成。
描述
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.
- 主要語言
- Python
- 星號
- 1.8k
- 分支
- 302
- 平均合併
- 4 小時 16 分鐘
- 30 天內合併 PR
- 7
環境準備
我們還沒有檢查這個專案的環境設定檔。先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/typing 的其他 Issue
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 72/100
python/typing#2356 · 2 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
-
topic: documentation
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
topic: documentation
難度 2/5 1-3 小時 新手友好度 65/100
python/typing#2150 · 2 則留言 · 2 個 reaction ·
維護者通常 1 天內回覆
-
topic: conformance tests topic: typing spec
難度 3/5 1-2 天 新手友好度 72/100
維護者通常 1 天內回覆
相似的 Issue
-
難度 1/5 1 小時以內 新手友好度 72/100
letsencrypt/cp-cps#353 ·
-
難度 2/5 1-3 小時 新手友好度 68/100
-
難度 2/5 1-3 小時 新手友好度 84/100
PedestrianDynamics/pyFDS-Evac#394 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
DOI-USGS/pywatershed#421 ·
-
難度 2/5 1-3 小時 新手友好度 78/100
python-pillow/Pillow#10087 · 1 則留言 ·
維護者通常 1 天內回覆