Support for `ParamSpec` for `type`
還沒有人認領這個 Issue。
評估
研究方向
檢視範例中描述的 ParamSpec 對 type 和 Callable 的行為,然後將其與 typing issue #1966 中的可能方向進行比較。完成這項工作需要達成一種共識表示方式,該表示方式能保留建構函式參數資訊,並使提議的多載可區分,但此 issue 沒有指明實作檔案或測試。
由索引模型根據 Issue 內容生成。
描述
The problem I'm having is that the type doesn't support ParamSpec, so I can't use that to enforce args and kwargs, in the same way as as Callable. I'm not 100% sure whether this is correct, but isn't every type a Callable, so in a sense that type is a subtype of Callable, so there should be a way to represent type in the same way as a Callable to obey Liskov?
For example, I'm trying to write an overload such that passing in a type will return ExpectType, while a general Callable returns ExpectCallable:
from collections.abc import Callable
import typing_extensions as t
P = t.ParamSpec("P")
T_co = t.TypeVar("T_co", covariant=True)
class ExpectCallable(t.Generic[P, T_co]): ...
class ExpectType(ExpectCallable[P, T_co]): ...
@t.overload
def expect(
v: type[T_co], /, *args: P.args, **kwargs: P.kwargs
) -> ExpectType[P, T_co]: ...
@t.overload
def expect(
v: Callable[P, T_co], /, *args: P.args, **kwargs: P.kwargs
) -> ExpectCallable[P, T_co]: ...
def expect(
v: type[T_co] | Callable[P, T_co], /, *args: P.args, **kwargs: P.kwargs
) -> ExpectType[P, T_co] | ExpectCallable[P, T_co]:
return ExpectType() if isinstance(v, type) else ExpectCallable()
class A:
def __init__(self, inp: str, /) -> None: ...
def fn(inp: str, /) -> None: ...
t.assert_type(expect(A), ExpectType[[], A]) # Shouldn't be allowed
t.assert_type(expect(fn, "inp"), ExpectCallable[[str], None])
As you can see that t.assert_type(expect(A), ExpectType[[], A]) passes with no errors, even though A is suppose to take in a str argument. I'm expecting expect(A) to require a str argument, similar to how expect(fn, "inp") requires the second parameter "inp".
Note that I haven't used *args and **kwargs in the example for simplicity, but they are meant to be used inside of the Expect classes.
Maybe the simplest way is to add a new class TypeCallable that allows for TypeCallable[P, T] instead of modifying type, so the above example is achievable:
@t.overload
def expect(
v: TypeCallable[P, T_co], /, *args: P.args, **kwargs: P.kwargs
) -> ExpectType[P, T_co]: ...
@t.overload
def expect(
v: Callable[P, T_co], /, *args: P.args, **kwargs: P.kwargs
) -> ExpectCallable[P, T_co]: ...
def expect(
v: TypeCallable[P, T_co] | Callable[P, T_co], /, *args: P.args, **kwargs: P.kwargs
) -> ExpectType[P, T_co] | ExpectCallable[P, T_co]:
return ExpectType() if isinstance(v, type) else ExpectCallable()
Note that I think https://github.com/python/typing/issues/1966 might also solve the above problem.
- 主要語言
- Python
- 星號
- 1.8k
- 分支
- 302
- 平均合併
- 23 小時
- 30 天內合併 PR
- 8
貢獻指南
這個儲存庫沒有索引到貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/typing 的其他 Issue
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 72/100
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 75/100
-
topic: documentation
難度 2/5 1-3 小時 新手友好度 76/100
-
topic: documentation
難度 2/5 1-3 小時 新手友好度 65/100
-
topic: conformance tests topic: typing spec
難度 3/5 1-2 天 新手友好度 72/100
相似的 Issue
-
sponsored
難度 2/5 1-3 小時 新手友好度 65/100
-
難度 2/5 1-3 小時 新手友好度 86/100
Diaoul/subliminal#1382 ·
-
難度 1/5 1 小時以內 新手友好度 92/100
-
triage/confirmed
難度 2/5 1-3 小時 新手友好度 88/100
agentscope-ai/agentscope#2775 ·
-
難度 2/5 1-3 小時 新手友好度 84/100