8000 Convert selected ABCs to Protocols (#1220) · python/typeshed@ec2cb8e · GitHub
[go: up one dir, main page]

Skip to content

Commit ec2cb8e

Browse files
ilevkivskyigvanrossum
authored andcommitted
Convert selected ABCs to Protocols (#1220)
1 parent 7806420 commit ec2cb8e

File tree

4 files changed

+82
-43
lines changed

4 files changed

+82
-43
lines changed

stdlib/2/__builtin__.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class type(objec 8000 t):
8686
def __instancecheck__(self, instance: Any) -> bool: ...
8787
def __subclasscheck__(self, subclass: type) -> bool: ...
8888

89-
class int(SupportsInt, SupportsFloat, SupportsAbs[int]):
89+
class int:
9090
@overload
9191
def __init__(self, x: SupportsInt = ...) -> None: ...
9292
@overload
@@ -138,7 +138,7 @@ class int(SupportsInt, SupportsFloat, SupportsAbs[int]):
138138
def __hash__(self) -> int: ...
139139
def __nonzero__(self) -> bool: ...
140140

141-
class float(SupportsFloat, SupportsInt, SupportsAbs[float]):
141+
class float:
142142
def __init__(self, x: Union[SupportsFloat, str, unicode, bytearray] = ...) -> None: ...
143143
def as_integer_ratio(self) -> Tuple[int, int]: ...
144144
def hex(self) -> str: ...
@@ -179,7 +179,7 @@ class float(SupportsFloat, SupportsInt, SupportsAbs[float]):
179179
def __hash__(self) -> int: ...
180180
def __nonzero__(self) -> bool: ...
181181

182-
class complex(SupportsAbs[float]):
182+
class complex:
183183
@overload
184184
def __init__(self, re: float = ..., im: float = ...) -> None: ...
185185
@overload
@@ -470,7 +470,7 @@ class bytearray(MutableSequence[int]):
470470
def __gt__(self, x: str) -> bool: ...
471471
def __ge__(self, x: str) -> bool: ...
472472

473-
class bool(int, SupportsInt, SupportsFloat):
473+
class bool(int):
474474
def __init__(self, o: object = ...) -> None: ...
475475

476476
class slice(object):

stdlib/2/typing.pyi

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class _SpecialForm(object):
1818

1919
Tuple: _SpecialForm = ...
2020
Generic: _SpecialForm = ...
21+
Protocol: _SpecialForm = ...
2122
Callable: _SpecialForm = ...
2223
Type: _SpecialForm = ...
2324
ClassVar: _SpecialForm = ...
@@ -61,47 +62,60 @@ _V_co = TypeVar('_V_co', covariant=True) # Any type covariant containers.
6162
_KT_co = TypeVar('_KT_co', covariant=True) # Key type covariant containers.
6263
_VT_co = TypeVar('_VT_co', covariant=True) # Value type covariant containers.
6364
_T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
65+
_TC = TypeVar('_TC', bound=Type[object])
6466

65-
class SupportsInt(metaclass=ABCMeta):
67+
def runtime(cls: _TC) -> _TC: ...
68+
69+
@runtime
70+
class SupportsInt(Protocol, metaclass=ABCMeta):
6671
@abstractmethod
6772
def __int__(self) -> int: ...
6873

69-
class SupportsFloat(metaclass=ABCMeta):
74+
@runtime
75+
class SupportsFloat(Protocol, metaclass=ABCMeta):
7076
@abstractmethod
7177
def __float__(self) -> float: ...
7278

73-
class SupportsComplex(metaclass=ABCMeta):
79+
@runtime
80+
class SupportsComplex(Protocol, metaclass=ABCMeta):
7481
@abstractmethod
7582
def __complex__(self) -> complex: ...
7683

77-
class SupportsAbs(Generic[_T]):
84+
@runtime
85+
class SupportsAbs(Protocol[_T_co]):
7886
@abstractmethod
79-
def __abs__(self) -> _T: ...
87+
def __abs__(self) -> _T_co: ...
8088

81-
class SupportsRound(Generic[_T]):
89+
@runtime
90+
class SupportsRound(Protocol[_T_co]):
8291
@abstractmethod
83-
def __round__(self, ndigits: int = ...) -> _T: ...
92+
def __round__(self, ndigits: int = ...) -> _T_co: ...
8493

85-
class Reversible(Generic[_T_co]):
94+
@runtime
95+
class Reversible(Protocol[_T_co]):
8696
@abstractmethod
8797
def __reversed__(self) -> Iterator[_T_co]: ...
8898

89-
class Sized(metaclass=ABCMeta):
99+
@runtime
100+
class Sized(Protocol, metaclass=ABCMeta):
90101
@abstractmethod
91102
def __len__(self) -> int: ...
92103

93-
class Hashable(metaclass=ABCMeta):
104+
@runtime
105+
class Hashable(Protocol, metaclass=ABCMeta):
94106
# TODO: This is special, in that a subclass of a hashable class may not be hashable
95107
# (for example, list vs. object). It's not obvious how to represent this. This class
96108
# is currently mostly useless for static checking.
97109
@abstractmethod
98110
def __hash__(self) -> int: ...
99111

100-
class Iterable(Generic[_T_co]):
112+
@runtime
113+
class Iterable(Protocol[_T_co]):
101114
@abstractmethod
102115
def __iter__(self) -> Iterator[_T_co]: ...
103116

104-
class Iterator(Iterable[_T_co], Generic[_T_co]):
117+
@runtime
118+
class Iterator(Iterable[_T_co], Protocol[_T_co]):
105119
@abstractmethod
106120
def next(self) -> _T_co: ...
107121

@@ -124,7 +138,8 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
124138
gi_frame = ... # type: FrameType
125139
gi_running = ... # type: bool
126140

127-
class Container(Generic[_T_co]):
141+
@runtime
142+
class Container(Protocol[_T_co]):
128143
@abstractmethod
129144
def __contains__(self, x: object) -> bool: ...
130145

@@ -209,7 +224,8 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
209224
def __contains__(self, o: object) -> bool: ...
210225
def __iter__(self) -> Iterator[_VT_co]: ...
211226

212-
class ContextManager(Generic[_T_co]):
227+
@runtime
228+
class ContextManager(Protocol[_T_co]):
213229
def __enter__(self) -> _T_co: ...
214230
def __exit__(self, exc_type: Optional[Type[BaseException]],
215231
exc_value: Optional[BaseException],

stdlib/3/builtins.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class super:
103103
@overload
104104
def __init__(self) -> None: ...
105105

106-
class int(SupportsInt, SupportsFloat, SupportsAbs[int]):
106+
class int:
107107
@overload
108108
def __init__(self, x: Union[str, bytes, SupportsInt] = ...) -> None: ...
109109
@overload
@@ -157,7 +157,7 @@ class int(SupportsInt, SupportsFloat, SupportsAbs[int]):
157157
def __hash__(self) -> int: ...
158158
def __bool__(self) -> bool: ...
159159

160-
class float(SupportsFloat, SupportsInt, SupportsAbs[float]):
160+
class float:
161161
def __init__(self, x: Union[SupportsFloat, str, bytes] = ...) -> None: ...
162162
def as_integer_ratio(self) -> Tuple[int, int]: ...
163163
def hex(self) -> str: ...
@@ -196,7 +196,7 @@ class float(SupportsFloat, SupportsInt, SupportsAbs[float]):
196196
def __hash__(self) -> int: ...
197197
def __bool__(self) -> bool: ...
198198

199-
class complex(SupportsAbs[float]):
199+
class complex:
200200
@overload
201201
def __init__(self, re: float = ..., im: float = ...) -> None: ...
202202
@overload
@@ -535,7 +535,7 @@ class memoryview(Sized, Container[bytes]):
535535
def hex(self) -> str: ...
536536

537537

538-
class bool(int, SupportsInt, SupportsFloat):
538+
class bool(int):
539539
def __init__(self, o: object = ...) -> None: ...
540540

541541
class slice:

stdlib/3/typing.pyi

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class _SpecialForm:
1919

2020
Tuple: _SpecialForm = ...
2121
Generic: _SpecialForm = ...
22+
Protocol: _SpecialForm = ...
2223
Callable: _SpecialForm = ...
2324
Type: _SpecialForm = ...
2425
ClassVar: _SpecialForm = ...
@@ -64,51 +65,65 @@ _V_co = TypeVar('_V_co', covariant=True) # Any type covariant containers.
6465
_KT_co = TypeVar('_KT_co', covariant=True) # Key type covariant containers.
6566
_VT_co = TypeVar('_VT_co', covariant=True) # Value type covariant containers.
6667
_T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
68+
_TC = TypeVar('_TC', bound=Type[object])
6769

68-
class SupportsInt(metaclass=ABCMeta):
70+
def runtime(cls: _TC) -> _TC: ...
71+
72+
@runtime
73+
class SupportsInt(Protocol, metaclass=ABCMeta):
6974
@abstractmethod
7075
def __int__(self) -> int: ...
7176

72-
class SupportsFloat(metaclass=ABCMeta):
77+
@runtime
78+
class SupportsFloat(Protocol, metaclass=ABCMeta):
7379
@abstractmethod
7480
def __float__(self) -> float: ...
7581

76-
class SupportsComplex(metaclass=ABCMeta):
82+
@runtime
83+
class SupportsComplex(Protocol, metaclass=ABCMeta):
7784
@abstractmethod
7885
def __complex__(self) -> complex: ...
7986

80-
class SupportsBytes(metaclass=ABCMeta):
87+
@runtime
88+
class SupportsBytes(Protocol, metaclass=ABCMeta):
8189
@abstractmethod
8290
def __bytes__(self) -> bytes: ...
8391

84-
class SupportsAbs(Generic[_T]):
92+
@runtime
93+
class SupportsAbs(Protocol[_T_co]):
8594
@abstractmethod
86-
def __abs__(self) -> _T: ...
95+
def __abs__(self) -> _T_co: ...
8796

88-
class SupportsRound(Generic[_T]):
97+
@runtime
98+
class SupportsRound(Protocol[_T_co]):
8999
@abstractmethod
90-
def __round__(self, ndigits: int = ...) -> _T: ...
100+
def __round__(self, ndigits: int = ...) -> _T_co: ...
91101

92-
class Reversible(Generic[_T_co]):
102+
@runtime
103+
class Reversible(Protocol[_T_co]):
93104
@abstractmethod
94105
def __reversed__(self) -> Iterator[_T_co]: ...
95106

96-
class Sized(metaclass=ABCMeta):
107+
@runtime
108+
class Sized(Protocol, metaclass=ABCMeta):
97109
@abstractmethod
98110
def __len__(self) -> int: ...
99111

100-
class Hashable(metaclass=ABCMeta):
112+
@runtime
113+
class Hashable(Protocol, metaclass=ABCMeta):
101114
# TODO: This is special, in that a subclass of a hashable class may not be hashable
102115
# (for example, list vs. object). It's not obvious how to represent this. This class
103116
# is currently mostly useless for static checking.
104117
@abstractmethod
105118
def __hash__(self) -> int: ...
106119

107-
class Iterable(Generic[_T_co]):
120+
@runtime
121+
class Iterable(Protocol[_T_co]):
108122
@abstractmethod
109123
def __iter__(self) -> Iterator[_T_co]: ...
110124

111-
class Iterator(Iterable[_T_co], Generic[_T_co]):
125+
@runtime
126+
class Iterator(Iterable[_T_co], Protocol[_T_co]):
112127
@abstractmethod
113128
def __next__(self) -> _T_co: ...
114129
def __iter__(self) -> 'Iterator[_T_co]': ...
@@ -139,7 +154,8 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
139154
# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
140155
# See https: //github.com/python/typeshed/issues/655 for why this is not easy.
141156

142-
class Awaitable(Generic[_T_co]):
157+
@runtime
158+
class Awaitable(Protocol[_T_co]):
143159
@abstractmethod
144160
def __await__(self) -> Generator[Any, None, _T_co]: ...
145161

@@ -161,12 +177,14 @@ class AwaitableGenerator(Generator[_T_co, _T_contra, _V_co], Awaitable[_V_co],
161177
Generic[_T_co, _T_contra, _V_co, _S]):
162178
pass
163179

164-
class AsyncIterable(Generic[_T_co]):
180+
@runtime
181+
class AsyncIterable(Protocol[_T_co]):
165182
@abstractmethod
166183
def __aiter__(self) -> 'AsyncIterator[_T_co]': ...
167184

185+
@runtime
168186
class AsyncIterator(AsyncIterable[_T_co],
169-
Generic[_T_co]):
187+
Protocol[_T_co]):
170188
@abstractmethod
171189
def __anext__(self) -> Awaitable[_T_co]: ...
172190
def __aiter__(self) -> 'AsyncIterator[_T_co]': ...
@@ -194,16 +212,19 @@ if sys.version_info >= (3, 6):
194212
ag_frame = ... # type: FrameType
195213
ag_running = ... # type: bool
196214

197-
class Container(Generic[_T_co]):
215+
@runtime
216+
class Container(Protocol[_T_co]):
198217
@abstractmethod
199218
def __contains__(self, x: object) -> bool: ...
200219

201220

202221
if sys.version_info >= (3, 6):
203-
class Collection(Sized, Iterable[_T_co], Container[_T_co], Generic[_T_co]): ...
222+
@runtime
223+
class Collection(Sized, Iterable[_T_co], Container[_T_co], Protocol[_T_co]): ...
204224
_Collection = Collection
205225
else:
206-
class _Collection(Sized, Iterable[_T_co], Container[_T_co], Generic[_T_co]): ...
226+
@runtime
227+
class _Collection(Sized, Iterable[_T_co], Container[_T_co], Protocol[_T_co]): ...
207228

208229
class Sequence(_Collection[_T_co], Reversible[_T_co], Generic[_T_co]):
209230
@overload
@@ -289,14 +310,16 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
289310
def __contains__(self, o: object) -> bool: ...
290311
def __iter__(self) -> Iterator[_VT_co]: ...
291312

292-
class ContextManager(Generic[_T_co]):
313+
@runtime
314+
class ContextManager(Protocol[_T_co]):
293315
def __enter__(self) -> _T_co: ...
294316
def __exit__(self, exc_type: Optional[Type[BaseException]],
295317
exc_value: Optional[BaseException],
296318
traceback: Optional[TracebackType]) -> Optional[bool]: ...
297319

298320
if sys.version_info >= (3, 5):
299-
class AsyncContextManager(Generic[_T_co]):
321+
@runtime
322+
class AsyncContextManager(Protocol[_T_co]):
300323
def __aenter__(self) -> Awaitable[_T_co]: ...
301324
def __aexit__(self, exc_type: Optional[Type[BaseException]],
302325
exc_value: Optional[BaseException],

0 commit comments

Comments
 (0)
0