-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathtypes.py
More file actions
140 lines (101 loc) · 4.38 KB
/
Copy pathtypes.py
File metadata and controls
140 lines (101 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""Advanced types."""
from collections.abc import Awaitable, Callable
from typing import Any, TypeVar
from typing_extensions import ParamSpec, Protocol
AnyType = TypeVar("AnyType")
ClassType = TypeVar("ClassType", bound=type)
SelfType = TypeVar("SelfType")
ParamType = TypeVar("ParamType")
ReturnType = TypeVar("ReturnType", covariant=True)
LocalReturnType = TypeVar("LocalReturnType", covariant=True)
NexusServiceType = TypeVar("NexusServiceType")
CallableType = TypeVar("CallableType", bound=Callable[..., Any])
CallableAsyncType = TypeVar("CallableAsyncType", bound=Callable[..., Awaitable[Any]])
CallableSyncOrAsyncType = TypeVar(
"CallableSyncOrAsyncType",
bound=Callable[..., Any | Awaitable[Any]],
)
CallableSyncOrAsyncReturnNoneType = TypeVar(
"CallableSyncOrAsyncReturnNoneType",
bound=Callable[..., None | Awaitable[None]],
)
MultiParamSpec = ParamSpec("MultiParamSpec")
ProtocolParamType = TypeVar("ProtocolParamType", contravariant=True)
ProtocolReturnType = TypeVar("ProtocolReturnType", covariant=True)
ProtocolSelfType = TypeVar("ProtocolSelfType", contravariant=True)
class CallableAsyncNoParam(Protocol[ProtocolReturnType]):
"""Generic callable type."""
def __call__(self) -> Awaitable[ProtocolReturnType]:
"""Generic callable type callback."""
...
class CallableSyncNoParam(Protocol[ProtocolReturnType]):
"""Generic callable type."""
def __call__(self) -> ProtocolReturnType:
"""Generic callable type callback."""
...
class CallableAsyncSingleParam(Protocol[ProtocolParamType, ProtocolReturnType]):
"""Generic callable type."""
def __call__(self, __arg: ProtocolParamType) -> Awaitable[ProtocolReturnType]:
"""Generic callable type callback."""
...
class CallableSyncSingleParam(Protocol[ProtocolParamType, ProtocolReturnType]):
"""Generic callable type."""
def __call__(self, __arg: ProtocolParamType) -> ProtocolReturnType:
"""Generic callable type callback."""
...
class MethodAsyncNoParam(Protocol[ProtocolSelfType, ProtocolReturnType]):
"""Generic callable type."""
def __call__(__self, self: ProtocolSelfType) -> Awaitable[ProtocolReturnType]:
"""Generic callable type callback."""
...
class MethodSyncNoParam(Protocol[ProtocolSelfType, ProtocolReturnType]):
"""Generic callable type."""
def __call__(__self, self: ProtocolSelfType) -> ProtocolReturnType:
"""Generic callable type callback."""
...
class MethodAsyncSingleParam(
Protocol[ProtocolSelfType, ProtocolParamType, ProtocolReturnType]
):
"""Generic callable type."""
def __call__(
self, __self: ProtocolSelfType, __arg: ProtocolParamType, /
) -> Awaitable[ProtocolReturnType]:
"""Generic callable type callback."""
...
class MethodSyncSingleParam(
Protocol[ProtocolSelfType, ProtocolParamType, ProtocolReturnType]
):
"""Generic callable type."""
def __call__(
self, __self: ProtocolSelfType, __arg: ProtocolParamType, /
) -> ProtocolReturnType:
"""Generic callable type callback."""
...
class MethodSyncOrAsyncNoParam(Protocol[ProtocolSelfType, ProtocolReturnType]):
"""Generic callable type."""
def __call__(
self, __self: ProtocolSelfType
) -> ProtocolReturnType | Awaitable[ProtocolReturnType]:
"""Generic callable type callback."""
...
class MethodSyncOrAsyncSingleParam(
Protocol[ProtocolSelfType, ProtocolParamType, ProtocolReturnType]
):
"""Generic callable type."""
def __call__(
self, __self: ProtocolSelfType, __param: ProtocolParamType, /
) -> ProtocolReturnType | Awaitable[ProtocolReturnType]:
"""Generic callable type callback."""
...
# TODO(cretz): Open MyPy bug on this, see https://gitter.im/python/typing?at=62cc24c5904f20479ac0c854
# class CallableAsyncMultiParam(Protocol[ReturnType]):
# def __call__(self, *__args: Any) -> Awaitable[ReturnType]:
# ...
# class CallableSyncMultiParam(Protocol[ReturnType]):
# def __call__(self, *__args: Any) -> ReturnType:
# ...
# class MethodAsyncMultiParam(Protocol[SelfType, ReturnType]):
# def __call__(__self, self: SelfType, *__args: Any) -> Awaitable[ReturnType]:
# ...
# TODO(cretz): Does not work, see https://github.com/python/mypy/issues/11855
# MethodAsyncMultiParam = Callable[Concatenate[SelfType, MultiParamSpec], Awaitable[ReturnType]]