Allow Self or Self-like type arguments for generics
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
從 AbstractContextManager 範例以及關於將 Self 用作泛型型別引數的既有限制開始。將 ManagerB 和 SubManager 的預期行為與目前關於 Self、子類化和泛型型別引數的規則進行比較。當通用解決方案能夠準確推斷具體子類別,同時保留被覆寫或繼承的 enter 方法的正確行為時,即可視為完成。
由索引模型根據 Issue 內容生成。
描述
from contextlib import AbstractContextManager
class ManagerA(AbstractContextManager):
def __init__(self, x: int) -> None:
self._x = x
def __enter__(self) -> int:
return self._x
This can raise type check errors because AbstractContextManager is a generic type, and this code omits the type argument. Fair enough, and easy to fix:
from contextlib import AbstractContextManager
class ManagerA(AbstractContextManager[int]):
def __init__(self, x: int) -> None:
self._x = x
def __enter__(self) -> int:
return self._x
But what if we are not overriding the __enter__ method? Consider this example:
from contextlib import AbstractContextManager
class ManagerB(AbstractContextManager):
def __init__(self, x: int) -> None:
self._x = x
# Other methods are defined, but __enter__ is not overridden
In this case, there is no great way to accurate type annotate it in order to reflect the fact that the __enter__ method always returns the object that it was called on (which is the default implementation of the __enter__ method in AbstractContextManager.
If we were willing to mark this class as final, then we could annotate it as:
class ManagerB(AbstractContextManager["ManagerB"]):
And that would work. But if we want to be able to subclass it, then this would no longer be accurate. Consider:
from contextlib import AbstractContextManager
from types import TracebackType
from typing import Type
class ManagerB(AbstractContextManager["ManagerB"]):
def __init__(self, x: int) -> None:
self._x = x
# Other methods are defined, but __enter__ is not overridden
def __exit__(
self, exc_type: Type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None) -> bool | None:
return None
class SubManager(ManagerB):
def harf(self) -> None:
print("harf")
with SubManager(1) as subman:
subman.harf()
This runs fine, but the type checker will rightly complain. The annotations indicate that subman should be of type ManagerB, but ManagerB has no harf attribute.
It feels like it would be appropriate to use:
class ManagerB(AbstractContextManager[Self]):
But Self is not allowed to be used in this context.
The feature I would like to see is some way to accurately handle non-final subclasses of AbstractContextManager that do not overwrite the __enter__ method (or that overwrite it but still return self).
Obviously this issue applies generally to generics -- this is just an obvious example since the Python implementation of that class makes it impossible to type annotate many of its subclasses. But ideally any solution would apply generally to generics -- essentially giving a way to use Self (or something Self-like) as the type argument for a generic.
- 主要語言
- Python
- 星號
- 1.8k
- 分支
- 302
- 平均合併
- 4 小時 16 分鐘
- 30 天內合併 PR
- 7
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 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 天內回覆
-
Update reference/generics.html with PEP695 type parameter syntax可能已有人在做 @BHUVANSH855 於 180 天前認領。 未關閉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
-
[Bug]: Non-vision image fallback calls vision_analyze with an empty source for oversized inline images and tells the model the image is corrupt可能已有人在做 @liuhao1024 今天認領。 未關閉comp/agent P2 tool/vision type/bug
難度 2/5 1-3 小時 新手友好度 88/100
NousResearch/hermes-agent#132605 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
pymc-labs/pymc-marketing#3102 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
vllm-project/compressed-tensors#920 ·
維護者通常 1 天內回覆
-
documentation good first issue
難度 2/5 1-3 小時 新手友好度 72/100
-
accepted bug wg/router-models-inference-runtime
難度 2/5 1-3 小時 新手友好度 88/100
vllm-project/semantic-router#4524 · 1 則留言 ·
維護者通常 1 天內回覆