worktoy 1.2.3.dev1__py3-none-any.whl → 1.3.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- worktoy/__init__.py +1 -1
- worktoy/dispatch/__init__.py +6 -1
- worktoy/dispatch/_abstract.py +478 -0
- worktoy/dispatch/_overload.py +99 -7
- worktoy/mcls/__init__.py +17 -5
- worktoy/mcls/_abc_meta.py +98 -0
- worktoy/mcls/_abc_space.py +552 -0
- worktoy/mcls/space_hooks/__init__.py +5 -1
- worktoy/mcls/space_hooks/_abc_hook.py +138 -0
- worktoy/mcls/space_hooks/_load_space_hook.py +53 -2
- worktoy/mcls/space_hooks/_name_hook.py +58 -10
- worktoy/waitaminute/meta/__init__.py +8 -2
- worktoy/waitaminute/meta/_missing_implementation.py +63 -0
- worktoy/waitaminute/meta/_wrong_implementation.py +75 -0
- {worktoy-1.2.3.dev1.dist-info → worktoy-1.3.0.dev0.dist-info}/METADATA +2 -2
- {worktoy-1.2.3.dev1.dist-info → worktoy-1.3.0.dev0.dist-info}/RECORD +19 -13
- {worktoy-1.2.3.dev1.dist-info → worktoy-1.3.0.dev0.dist-info}/WHEEL +0 -0
- {worktoy-1.2.3.dev1.dist-info → worktoy-1.3.0.dev0.dist-info}/licenses/LICENSE +0 -0
- {worktoy-1.2.3.dev1.dist-info → worktoy-1.3.0.dev0.dist-info}/top_level.txt +0 -0
worktoy/__init__.py
CHANGED
|
@@ -48,7 +48,7 @@ and the enclosing class is left without the decoration.
|
|
|
48
48
|
# Copyright (c) 2025-2026 Asger Jon Vistisen
|
|
49
49
|
from __future__ import annotations
|
|
50
50
|
|
|
51
|
-
__version__ = '1.
|
|
51
|
+
__version__ = '1.3.0-dev0'
|
|
52
52
|
|
|
53
53
|
from . import utilities
|
|
54
54
|
from . import waitaminute
|
worktoy/dispatch/__init__.py
CHANGED
|
@@ -6,13 +6,16 @@ live dispatch happens one layer up, where 'BaseMeta' in 'worktoy.mcls'
|
|
|
6
6
|
compiles them into a 'Dispatcher' during class construction. They serve
|
|
7
7
|
a class based on 'BaseObject' or built by a metaclass based on 'BaseMeta';
|
|
8
8
|
a class whose metaclass comes from another library takes a metaclass
|
|
9
|
-
combined with 'BaseMeta'.
|
|
9
|
+
combined with 'BaseMeta'. The 'abstract' decorator declares a method, or
|
|
10
|
+
the signatures of an overloaded name, that a subclass has to write; a
|
|
11
|
+
class built by 'ABCMeta' of 'worktoy.mcls' holds its subclasses to it.
|
|
10
12
|
|
|
11
13
|
- flexCall, isFlex
|
|
12
14
|
- CallMeMaybe
|
|
13
15
|
- Permuter, PermuterMethod
|
|
14
16
|
- TypeSig
|
|
15
17
|
- Dispatcher
|
|
18
|
+
- abstract
|
|
16
19
|
- overload
|
|
17
20
|
"""
|
|
18
21
|
# Apache-2.0 license
|
|
@@ -25,6 +28,7 @@ from ._permuter import Permuter
|
|
|
25
28
|
from ._permuter_method import PermuterMethod
|
|
26
29
|
from ._type_sig import TypeSig
|
|
27
30
|
from ._dispatcher import Dispatcher
|
|
31
|
+
from ._abstract import abstract
|
|
28
32
|
from ._overload import overload
|
|
29
33
|
|
|
30
34
|
__all__ = [
|
|
@@ -35,5 +39,6 @@ __all__ = [
|
|
|
35
39
|
'PermuterMethod',
|
|
36
40
|
'TypeSig',
|
|
37
41
|
'Dispatcher',
|
|
42
|
+
'abstract',
|
|
38
43
|
'overload',
|
|
39
44
|
]
|
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The 'abstract' decorator declares a method a subclass has to write, and
|
|
3
|
+
is the record of that declaration.
|
|
4
|
+
"""
|
|
5
|
+
# Apache-2.0 license
|
|
6
|
+
# Copyright (c) 2026 Asger Jon Vistisen
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import dis
|
|
10
|
+
from types import FunctionType
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from ..utilities import textFmt
|
|
14
|
+
from ..waitaminute import TypeException
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING: # pragma: no cover, no mutate
|
|
17
|
+
from typing import Any, Optional
|
|
18
|
+
|
|
19
|
+
_CO_VARARGS: int = 0x04 # CPython compile flag
|
|
20
|
+
_CO_VARKEYWORDS: int = 0x08 # CPython compile flag
|
|
21
|
+
# CO_GENERATOR, CO_COROUTINE, CO_ITERABLE_COROUTINE, CO_ASYNC_GENERATOR
|
|
22
|
+
_CO_GENERATORS: int = 0x20 | 0x80 | 0x100 | 0x200
|
|
23
|
+
|
|
24
|
+
# What Python compiles a blank body to, whichever way it is written:
|
|
25
|
+
# a load of 'None' and a return, under the names of every version from
|
|
26
|
+
# 3.7 to 3.15. 'RESUME' opens every function from 3.11 on.
|
|
27
|
+
_SKIPPED_STEPS: tuple = ('RESUME', 'NOP')
|
|
28
|
+
_BLANK_STEPS: tuple = (
|
|
29
|
+
'LOAD_CONST', 'LOAD_COMMON_CONSTANT', 'RETURN_VALUE', 'RETURN_CONST',
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class abstract: # NOQA
|
|
35
|
+
"""
|
|
36
|
+
Some classes declare a method without having anything to run for it:
|
|
37
|
+
every class based on them has to write it, as a model of a list has to
|
|
38
|
+
say how many rows it holds. The class body must be able to say so at
|
|
39
|
+
the method, for a bound method, a classmethod and a staticmethod, and
|
|
40
|
+
for the signatures of an overloaded name:
|
|
41
|
+
|
|
42
|
+
class Model(BaseObject, metaclass=ABCMeta):
|
|
43
|
+
|
|
44
|
+
@abstract
|
|
45
|
+
def rowCount(self) -> int: ...
|
|
46
|
+
|
|
47
|
+
@overload(str, int)
|
|
48
|
+
@abstract
|
|
49
|
+
def data(self, index: str, role: int) -> Any: ...
|
|
50
|
+
|
|
51
|
+
Such a declaration runs nothing, so a function with a body under it
|
|
52
|
+
is a mistake and must be refused where it is written. What it declares
|
|
53
|
+
must be on record, since a subclass writing the method is held to it:
|
|
54
|
+
the kind of method, and what the method receives. And a call that
|
|
55
|
+
does reach the declaration, as through 'super()', must do nothing.
|
|
56
|
+
|
|
57
|
+
Implementation
|
|
58
|
+
--------------
|
|
59
|
+
|
|
60
|
+
'@abstract' over a blank function gives an 'abstract' object, the
|
|
61
|
+
record of that function, as '@overload' gives a record of its own. A
|
|
62
|
+
body of '...', of 'pass', of a docstring alone or of 'return None' is
|
|
63
|
+
blank, Python compiling each to the same thing; any other body, and a
|
|
64
|
+
generator or coroutine function, raises 'TypeError'. The record keeps
|
|
65
|
+
the function, its name and its docstring, gives the shape of its
|
|
66
|
+
parameters through 'getShape', and does nothing when called. Given a
|
|
67
|
+
'classmethod' or 'staticmethod' object it returns one of the same kind
|
|
68
|
+
over the record. Given the record of '@overload', it puts itself in
|
|
69
|
+
the place of the function that record holds, so '@abstract' stands at
|
|
70
|
+
any position among the '@overload' decorators of a stack. A class
|
|
71
|
+
built by 'ABCMeta' of 'worktoy.mcls' holds its subclasses to what is
|
|
72
|
+
declared, and the space hook of its namespace says so on each record
|
|
73
|
+
of its body by setting '__abc_only__'. A record no such hook took is
|
|
74
|
+
refused as its class is created, since nothing there would hold a
|
|
75
|
+
subclass to it.
|
|
76
|
+
|
|
77
|
+
Attributes
|
|
78
|
+
----------
|
|
79
|
+
|
|
80
|
+
__wrapped__ : FunctionType
|
|
81
|
+
The blank function.
|
|
82
|
+
|
|
83
|
+
__name__, __qualname__, __module__, __doc__ : str
|
|
84
|
+
Those of the blank function.
|
|
85
|
+
|
|
86
|
+
__abc_only__ : bool
|
|
87
|
+
'False' on a new record, and set to 'True' by the namespace of an
|
|
88
|
+
abstract class for each record of its body.
|
|
89
|
+
|
|
90
|
+
__declaring_class__ : type or None
|
|
91
|
+
The class whose body declares the record, once that class exists,
|
|
92
|
+
and 'None' until then. A class based on it inherits the record and
|
|
93
|
+
leaves this as it is.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
97
|
+
# NAMESPACE # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
98
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
99
|
+
|
|
100
|
+
# Private Variables
|
|
101
|
+
__abc_only__: bool = False
|
|
102
|
+
__declaring_class__: Optional[type] = None
|
|
103
|
+
|
|
104
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
105
|
+
# STATIC METHODS # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
106
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
107
|
+
|
|
108
|
+
@staticmethod
|
|
109
|
+
def _isBlank(func: FunctionType) -> bool:
|
|
110
|
+
"""
|
|
111
|
+
A function under '@abstract' must have no implementation, and that
|
|
112
|
+
must be read off the function itself, on every version of Python.
|
|
113
|
+
|
|
114
|
+
Implementation
|
|
115
|
+
--------------
|
|
116
|
+
|
|
117
|
+
Python compiles a body of '...', of 'pass', of a docstring alone and
|
|
118
|
+
of 'return None' to a load of 'None' and a return, and nothing else.
|
|
119
|
+
The steps 'dis' gives are held to that: each is one of those two,
|
|
120
|
+
under any of the names the versions give them, and carries no value
|
|
121
|
+
but 'None'.
|
|
122
|
+
|
|
123
|
+
Parameters
|
|
124
|
+
----------
|
|
125
|
+
|
|
126
|
+
func : FunctionType
|
|
127
|
+
The function.
|
|
128
|
+
|
|
129
|
+
Returns
|
|
130
|
+
-------
|
|
131
|
+
|
|
132
|
+
bool
|
|
133
|
+
Whether the body of the function is blank.
|
|
134
|
+
"""
|
|
135
|
+
for step in dis.get_instructions(func):
|
|
136
|
+
if step.opname in _SKIPPED_STEPS:
|
|
137
|
+
continue
|
|
138
|
+
if step.opname not in _BLANK_STEPS:
|
|
139
|
+
return False
|
|
140
|
+
if step.argval is not None:
|
|
141
|
+
return False
|
|
142
|
+
return True
|
|
143
|
+
|
|
144
|
+
@staticmethod
|
|
145
|
+
def shapeOf(func: FunctionType, kind: Any) -> tuple:
|
|
146
|
+
"""
|
|
147
|
+
Whether a method written in a subclass can take what an abstract one
|
|
148
|
+
declares is decided by what each receives: how many arguments by
|
|
149
|
+
position, whether any number more, which by keyword alone, and
|
|
150
|
+
whether any keyword. A default plays no part: a parameter counts the
|
|
151
|
+
same with one or without.
|
|
152
|
+
|
|
153
|
+
Implementation
|
|
154
|
+
--------------
|
|
155
|
+
|
|
156
|
+
The counts are read off the code of the function. The first
|
|
157
|
+
positional parameter of a bound method or a classmethod is the
|
|
158
|
+
receiver and is left out; a function taking the receiver through
|
|
159
|
+
'*args' counts none, and one that cannot take a receiver at all
|
|
160
|
+
counts -1, which fits nothing.
|
|
161
|
+
|
|
162
|
+
Parameters
|
|
163
|
+
----------
|
|
164
|
+
|
|
165
|
+
func : FunctionType
|
|
166
|
+
The function.
|
|
167
|
+
|
|
168
|
+
kind : Any
|
|
169
|
+
'classmethod', 'staticmethod', or 'None' for a bound method.
|
|
170
|
+
|
|
171
|
+
Returns
|
|
172
|
+
-------
|
|
173
|
+
|
|
174
|
+
tuple
|
|
175
|
+
The number of positional parameters after the receiver, whether
|
|
176
|
+
the function takes '*args', the names of its keyword-only
|
|
177
|
+
parameters, and whether it takes '**kwargs'.
|
|
178
|
+
"""
|
|
179
|
+
code = func.__code__
|
|
180
|
+
count = code.co_argcount
|
|
181
|
+
starred = True if code.co_flags & _CO_VARARGS else False
|
|
182
|
+
doubled = True if code.co_flags & _CO_VARKEYWORDS else False
|
|
183
|
+
if kind is not staticmethod:
|
|
184
|
+
if count or not starred:
|
|
185
|
+
count -= 1
|
|
186
|
+
first = code.co_argcount
|
|
187
|
+
keyed = code.co_varnames[first:first + code.co_kwonlyargcount]
|
|
188
|
+
return count, starred, (*keyed,), doubled
|
|
189
|
+
|
|
190
|
+
@staticmethod
|
|
191
|
+
def fits(declared: tuple, written: tuple) -> bool:
|
|
192
|
+
"""
|
|
193
|
+
A method written in a subclass implements an abstract one when it can
|
|
194
|
+
receive everything the abstract one declares.
|
|
195
|
+
|
|
196
|
+
Implementation
|
|
197
|
+
--------------
|
|
198
|
+
|
|
199
|
+
A written function takes exactly as many arguments by position as it
|
|
200
|
+
has positional parameters, and with '*args' any number from there
|
|
201
|
+
up. Where the declaration itself takes any number, the written one
|
|
202
|
+
has to as well, from no more than the declared number. Each
|
|
203
|
+
keyword-only parameter declared needs the same one or '**kwargs',
|
|
204
|
+
and a declared '**kwargs' needs '**kwargs'.
|
|
205
|
+
|
|
206
|
+
Parameters
|
|
207
|
+
----------
|
|
208
|
+
|
|
209
|
+
declared : tuple
|
|
210
|
+
The shape declared, as 'shapeOf' gives one.
|
|
211
|
+
|
|
212
|
+
written : tuple
|
|
213
|
+
The shape of the function written.
|
|
214
|
+
|
|
215
|
+
Returns
|
|
216
|
+
-------
|
|
217
|
+
|
|
218
|
+
bool
|
|
219
|
+
Whether the written shape can take what the declared one takes.
|
|
220
|
+
"""
|
|
221
|
+
count, starred, keyed, doubled = declared
|
|
222
|
+
writtenCount, writtenStarred, writtenKeyed, writtenDoubled = written
|
|
223
|
+
if starred and not writtenStarred:
|
|
224
|
+
return False
|
|
225
|
+
if writtenStarred:
|
|
226
|
+
if writtenCount > count:
|
|
227
|
+
return False
|
|
228
|
+
elif writtenCount != count:
|
|
229
|
+
return False
|
|
230
|
+
if doubled and not writtenDoubled:
|
|
231
|
+
return False
|
|
232
|
+
for name in keyed:
|
|
233
|
+
if name not in writtenKeyed and not writtenDoubled:
|
|
234
|
+
return False
|
|
235
|
+
return True
|
|
236
|
+
|
|
237
|
+
@staticmethod
|
|
238
|
+
def describe(kind: Any, shape: tuple) -> str:
|
|
239
|
+
"""
|
|
240
|
+
The refusal of a wrong implementation must say what was declared and
|
|
241
|
+
what was written in the same words, so the two can be compared.
|
|
242
|
+
|
|
243
|
+
Parameters
|
|
244
|
+
----------
|
|
245
|
+
|
|
246
|
+
kind : Any
|
|
247
|
+
'classmethod', 'staticmethod', or 'None' for a bound method.
|
|
248
|
+
|
|
249
|
+
shape : tuple
|
|
250
|
+
A shape as 'shapeOf' gives one.
|
|
251
|
+
|
|
252
|
+
Returns
|
|
253
|
+
-------
|
|
254
|
+
|
|
255
|
+
str
|
|
256
|
+
The kind and what it takes, as "a method taking 2 by position".
|
|
257
|
+
"""
|
|
258
|
+
count, starred, keyed, doubled = shape
|
|
259
|
+
if kind is classmethod:
|
|
260
|
+
out = 'a classmethod taking %d' % count
|
|
261
|
+
elif kind is staticmethod:
|
|
262
|
+
out = 'a staticmethod taking %d' % count
|
|
263
|
+
else:
|
|
264
|
+
out = 'a method taking %d' % count
|
|
265
|
+
if starred:
|
|
266
|
+
out = '%s or more' % out
|
|
267
|
+
out = '%s by position' % out
|
|
268
|
+
for name in keyed:
|
|
269
|
+
out = "%s, '%s' by keyword" % (out, name)
|
|
270
|
+
if doubled:
|
|
271
|
+
out = '%s, any keyword' % out
|
|
272
|
+
return out
|
|
273
|
+
|
|
274
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
275
|
+
# GETTERS # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
276
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
277
|
+
|
|
278
|
+
def requireHost(self, mcls: type, className: str, name: str) -> None:
|
|
279
|
+
"""
|
|
280
|
+
An abstract method is a claim on the classes based on its class, and
|
|
281
|
+
only a namespace made for it holds them to the claim. Declared in
|
|
282
|
+
any other class, the method would be answered with nothing and no
|
|
283
|
+
subclass would ever be asked to write it, so the declaration must
|
|
284
|
+
fail as that class is created.
|
|
285
|
+
|
|
286
|
+
Implementation
|
|
287
|
+
--------------
|
|
288
|
+
|
|
289
|
+
The space hook of an abstract class sets '__abc_only__' on each
|
|
290
|
+
record of its body, as 'ABCHook' of 'worktoy.mcls' does. A record
|
|
291
|
+
whose flag is still 'False' was taken by no such hook.
|
|
292
|
+
|
|
293
|
+
Parameters
|
|
294
|
+
----------
|
|
295
|
+
|
|
296
|
+
mcls : type
|
|
297
|
+
The metaclass of the class whose body declares the method, for
|
|
298
|
+
the message.
|
|
299
|
+
|
|
300
|
+
className : str
|
|
301
|
+
The name of that class.
|
|
302
|
+
|
|
303
|
+
name : str
|
|
304
|
+
The name declared abstract.
|
|
305
|
+
|
|
306
|
+
Raises
|
|
307
|
+
------
|
|
308
|
+
|
|
309
|
+
TypeError
|
|
310
|
+
No space hook of an abstract class took the record.
|
|
311
|
+
"""
|
|
312
|
+
if self.__abc_only__:
|
|
313
|
+
return
|
|
314
|
+
infoSpec = """The class body of '%s' declares '%s' abstract, and its
|
|
315
|
+
metaclass '%s' is not based on 'ABCMeta', so no class based on it
|
|
316
|
+
would be held to writing '%s'. A class declaring abstract methods is
|
|
317
|
+
built by 'ABCMeta' of 'worktoy.mcls'."""
|
|
318
|
+
info = infoSpec % (className, name, mcls.__name__, name)
|
|
319
|
+
raise TypeError(textFmt(info))
|
|
320
|
+
|
|
321
|
+
def getShape(self, kind: Any) -> tuple:
|
|
322
|
+
"""
|
|
323
|
+
What the blank function declares depends on the kind of method it
|
|
324
|
+
is, which the layers around the record say, not the record.
|
|
325
|
+
|
|
326
|
+
Parameters
|
|
327
|
+
----------
|
|
328
|
+
|
|
329
|
+
kind : Any
|
|
330
|
+
'classmethod', 'staticmethod', or 'None' for a bound method.
|
|
331
|
+
|
|
332
|
+
Returns
|
|
333
|
+
-------
|
|
334
|
+
|
|
335
|
+
tuple
|
|
336
|
+
The shape of the blank function, as 'shapeOf' gives it.
|
|
337
|
+
"""
|
|
338
|
+
return self.shapeOf(self.__wrapped__, kind)
|
|
339
|
+
|
|
340
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
341
|
+
# CONSTRUCTORS # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
342
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
343
|
+
|
|
344
|
+
def __new__(cls, func: Any) -> Any:
|
|
345
|
+
"""
|
|
346
|
+
'@abstract' is written above a blank function, above or below
|
|
347
|
+
'@classmethod' or '@staticmethod', and at any position among the
|
|
348
|
+
'@overload' decorators of a stack. Whatever it receives, the one
|
|
349
|
+
function of the stack must end up declared abstract, and anything
|
|
350
|
+
that cannot be an abstract declaration must be refused at that line.
|
|
351
|
+
|
|
352
|
+
Implementation
|
|
353
|
+
--------------
|
|
354
|
+
|
|
355
|
+
The 'classmethod' and 'staticmethod' layers are taken off first. A
|
|
356
|
+
record of '@overload' underneath is asked to put a new 'abstract' in
|
|
357
|
+
the place of its function, and is returned as it came. A function is
|
|
358
|
+
checked and recorded, and returned under a layer of the kind it came
|
|
359
|
+
in.
|
|
360
|
+
|
|
361
|
+
Parameters
|
|
362
|
+
----------
|
|
363
|
+
|
|
364
|
+
func : Any
|
|
365
|
+
A blank function, a classmethod or staticmethod over one, or the
|
|
366
|
+
record of an '@overload' decorator below.
|
|
367
|
+
|
|
368
|
+
Returns
|
|
369
|
+
-------
|
|
370
|
+
|
|
371
|
+
Any
|
|
372
|
+
The record, under a layer of the kind it came in, or the record of
|
|
373
|
+
'@overload' it was given.
|
|
374
|
+
|
|
375
|
+
Raises
|
|
376
|
+
------
|
|
377
|
+
|
|
378
|
+
TypeException
|
|
379
|
+
What lies under the layers is no plain function, as a function
|
|
380
|
+
declared abstract already.
|
|
381
|
+
|
|
382
|
+
TypeError
|
|
383
|
+
The function has a body, or is a generator or coroutine function.
|
|
384
|
+
|
|
385
|
+
MixedMethodKind
|
|
386
|
+
The function is decorated as both a classmethod and a
|
|
387
|
+
staticmethod.
|
|
388
|
+
"""
|
|
389
|
+
# Local import: 'overload' loads after this file, which it imports.
|
|
390
|
+
from . import overload
|
|
391
|
+
kinds, inner = overload.peelMethodKinds(func)
|
|
392
|
+
if isinstance(inner, overload):
|
|
393
|
+
inner._makeAbstract(cls)
|
|
394
|
+
return func
|
|
395
|
+
if not isinstance(inner, FunctionType):
|
|
396
|
+
raise TypeException('func', inner, FunctionType)
|
|
397
|
+
name: str = inner.__name__
|
|
398
|
+
kind = overload.agreeMethodKind(kinds, name)
|
|
399
|
+
if inner.__code__.co_flags & _CO_GENERATORS:
|
|
400
|
+
infoSpec = """The function '%s' under '@abstract' is a generator or
|
|
401
|
+
a coroutine function. An abstract method is declared on an ordinary
|
|
402
|
+
function."""
|
|
403
|
+
raise TypeError(textFmt(infoSpec % name))
|
|
404
|
+
if not cls._isBlank(inner):
|
|
405
|
+
infoSpec = """The function '%s' under '@abstract' has a body. An
|
|
406
|
+
abstract method declares what a subclass has to write and runs
|
|
407
|
+
nothing itself, so its body is left blank: '...', 'pass' or a
|
|
408
|
+
docstring alone."""
|
|
409
|
+
raise TypeError(textFmt(infoSpec % name))
|
|
410
|
+
self = object.__new__(cls)
|
|
411
|
+
self.__wrapped__ = inner
|
|
412
|
+
self.__name__ = name
|
|
413
|
+
self.__qualname__ = inner.__qualname__
|
|
414
|
+
self.__module__ = inner.__module__
|
|
415
|
+
self.__doc__ = inner.__doc__
|
|
416
|
+
if kind is None:
|
|
417
|
+
return self
|
|
418
|
+
return kind(self)
|
|
419
|
+
|
|
420
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
421
|
+
# Python API # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
422
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
423
|
+
|
|
424
|
+
def __call__(self, *args, **kwargs) -> None:
|
|
425
|
+
"""
|
|
426
|
+
A call may reach an abstract method, as through 'super()' from the
|
|
427
|
+
method a subclass wrote, or on a class that is still abstract through
|
|
428
|
+
a classmethod or a staticmethod. The declaration has nothing to run.
|
|
429
|
+
|
|
430
|
+
Implementation
|
|
431
|
+
--------------
|
|
432
|
+
|
|
433
|
+
Whatever it is called with, it does nothing and returns 'None', as an
|
|
434
|
+
abstract method of Python's 'abc' with a blank body does.
|
|
435
|
+
"""
|
|
436
|
+
|
|
437
|
+
def __set_name__(self, owner: type, name: str) -> None:
|
|
438
|
+
"""
|
|
439
|
+
A record left in the body of a class that no hosting metaclass built
|
|
440
|
+
must fail as that class is created, not at the first call.
|
|
441
|
+
|
|
442
|
+
Implementation
|
|
443
|
+
--------------
|
|
444
|
+
|
|
445
|
+
Python calls this method for a record held in the dictionary of a
|
|
446
|
+
new class. 'requireHost' refuses a record whose '__abc_only__' no
|
|
447
|
+
space hook of an abstract class has set, and a record that passes
|
|
448
|
+
keeps the class as '__declaring_class__'. Before Python 3.12 the
|
|
449
|
+
refusal reaches the class statement wrapped in a 'RuntimeError'.
|
|
450
|
+
Python does not call this method for a record under a 'classmethod'
|
|
451
|
+
or 'staticmethod' layer, nor for one a dispatcher holds; in a class
|
|
452
|
+
built by 'BaseMeta' those are refused by 'LoadSpaceHook' instead.
|
|
453
|
+
In a plain Python class nothing asks a record under such a layer,
|
|
454
|
+
and it passes unnoticed: 'classmethod' and 'staticmethod' wrap
|
|
455
|
+
anything and pass no '__set_name__' on.
|
|
456
|
+
|
|
457
|
+
Parameters
|
|
458
|
+
----------
|
|
459
|
+
|
|
460
|
+
owner : type
|
|
461
|
+
The class being created.
|
|
462
|
+
|
|
463
|
+
name : str
|
|
464
|
+
The name the record is bound to.
|
|
465
|
+
|
|
466
|
+
Raises
|
|
467
|
+
------
|
|
468
|
+
|
|
469
|
+
TypeError
|
|
470
|
+
No space hook of an abstract class took the record.
|
|
471
|
+
"""
|
|
472
|
+
self.requireHost(type(owner), owner.__name__, name)
|
|
473
|
+
self.__declaring_class__ = owner
|
|
474
|
+
|
|
475
|
+
def __str__(self, ) -> str:
|
|
476
|
+
return """abstract '%s'""" % self.__name__
|
|
477
|
+
|
|
478
|
+
__repr__ = __str__
|
worktoy/dispatch/_overload.py
CHANGED
|
@@ -15,7 +15,7 @@ from ..utilities.combinatorics import Arrangements
|
|
|
15
15
|
from ..waitaminute import MissingVariable, TypeException
|
|
16
16
|
from ..waitaminute.dispatch import BareOverload
|
|
17
17
|
from ..waitaminute.meta import MixedMethodKind
|
|
18
|
-
from . import TypeSig, PermuterMethod
|
|
18
|
+
from . import TypeSig, PermuterMethod, abstract
|
|
19
19
|
from ._dispatcher import _MAX_EXPANDED_FLEX
|
|
20
20
|
|
|
21
21
|
if TYPE_CHECKING: # pragma: no cover, no mutate
|
|
@@ -63,7 +63,9 @@ class overload: # NOQA
|
|
|
63
63
|
'overload' object recording the function and the signature; it does no
|
|
64
64
|
dispatching. A decorator stacked above another adds its signature to
|
|
65
65
|
the 'overload' below it. The class methods 'flex', 'fallback' and
|
|
66
|
-
'finalize' declare the other roles the same way. A
|
|
66
|
+
'finalize' declare the other roles the same way. A function declared
|
|
67
|
+
with '@abstract' is recorded as any other, '@abstract' standing at any
|
|
68
|
+
position in the stack; see 'abstract'. A decorator given a
|
|
67
69
|
'classmethod' or 'staticmethod' object takes the function out of it and
|
|
68
70
|
keeps its kind; given one over the record of the decorator below, it
|
|
69
71
|
extends that record, which takes the kind. 'LoadSpaceHook' does the
|
|
@@ -353,6 +355,85 @@ class overload: # NOQA
|
|
|
353
355
|
def _extendLatest(self, sig: TypeSig) -> None:
|
|
354
356
|
self._addSigFunc(sig, self._getLatestFunc())
|
|
355
357
|
|
|
358
|
+
@staticmethod
|
|
359
|
+
def _refuseAbstractRole(role: str, name: str) -> Never:
|
|
360
|
+
"""
|
|
361
|
+
A fallback takes the calls nothing else accepts, and a finalizer
|
|
362
|
+
follows every call. Neither is something a class answers some calls
|
|
363
|
+
with and leaves others to a subclass, so neither is declared
|
|
364
|
+
abstract: a blank one would swallow every call it is there for.
|
|
365
|
+
|
|
366
|
+
Parameters
|
|
367
|
+
----------
|
|
368
|
+
|
|
369
|
+
role : str
|
|
370
|
+
'fallback' or 'finalizer'.
|
|
371
|
+
|
|
372
|
+
name : str
|
|
373
|
+
The name of the function.
|
|
374
|
+
|
|
375
|
+
Raises
|
|
376
|
+
------
|
|
377
|
+
|
|
378
|
+
TypeError
|
|
379
|
+
Always.
|
|
380
|
+
"""
|
|
381
|
+
infoSpec = """The %s '%s' is declared abstract. A fallback takes the
|
|
382
|
+
calls nothing else accepts and a finalizer follows every call, so
|
|
383
|
+
neither is left for a subclass to write."""
|
|
384
|
+
raise TypeError(textFmt(infoSpec % (role, name)))
|
|
385
|
+
|
|
386
|
+
def _makeAbstract(self, factory: Callable) -> None:
|
|
387
|
+
"""
|
|
388
|
+
'@abstract' may stand above an '@overload' decorator, where it
|
|
389
|
+
receives this record and not the function. The one function the
|
|
390
|
+
stack decorates must then become the abstract declaration, under
|
|
391
|
+
every signature recorded so far, so that the stack means the same
|
|
392
|
+
whichever position '@abstract' stands at.
|
|
393
|
+
|
|
394
|
+
Implementation
|
|
395
|
+
--------------
|
|
396
|
+
|
|
397
|
+
A record is one stack over one function, so every signature it holds
|
|
398
|
+
is for that function, one order of a flexible declaration through a
|
|
399
|
+
'PermuterMethod'. 'factory', the 'abstract' class, makes the
|
|
400
|
+
declaration from the function, refusing one that is not blank, and
|
|
401
|
+
the declaration takes the place of the function at every signature
|
|
402
|
+
and as the latest function, which a decorator stacked above
|
|
403
|
+
registers.
|
|
404
|
+
|
|
405
|
+
Parameters
|
|
406
|
+
----------
|
|
407
|
+
|
|
408
|
+
factory : Callable
|
|
409
|
+
The 'abstract' class.
|
|
410
|
+
|
|
411
|
+
Raises
|
|
412
|
+
------
|
|
413
|
+
|
|
414
|
+
TypeError
|
|
415
|
+
The record declares a fallback or a finalizer, or the function has
|
|
416
|
+
a body.
|
|
417
|
+
|
|
418
|
+
TypeException
|
|
419
|
+
The function is declared abstract already.
|
|
420
|
+
"""
|
|
421
|
+
name = self._getFunctionName(self._getLatestFunc())
|
|
422
|
+
if self.isFallback():
|
|
423
|
+
self._refuseAbstractRole('fallback', name)
|
|
424
|
+
if self.isFinalizer():
|
|
425
|
+
self._refuseAbstractRole('finalizer', name)
|
|
426
|
+
declared = factory(self._getLatestFunc())
|
|
427
|
+
concrete = self._getSigFuncDict()
|
|
428
|
+
self.__sig_func_dict__ = {sig: declared for sig in concrete}
|
|
429
|
+
self.__variadic_sig_func_list__ = [
|
|
430
|
+
(sig, declared) for sig, _ in self.getVariadics()
|
|
431
|
+
]
|
|
432
|
+
self.__flex_sig_func_list__ = [
|
|
433
|
+
(sig, declared) for sig, _ in self.getFlexes()
|
|
434
|
+
]
|
|
435
|
+
self.__latest_func__ = declared
|
|
436
|
+
|
|
356
437
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
357
438
|
# CONSTRUCTORS # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
358
439
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
@@ -496,9 +577,15 @@ class overload: # NOQA
|
|
|
496
577
|
return self
|
|
497
578
|
for arrangement in Arrangements(*types):
|
|
498
579
|
sig = TypeSig(*arrangement.values)
|
|
499
|
-
if
|
|
500
|
-
|
|
501
|
-
|
|
580
|
+
if isinstance(function, abstract):
|
|
581
|
+
# This branch is reached by the following:
|
|
582
|
+
# - The function is declared abstract and runs nothing, so no
|
|
583
|
+
# order of its arguments needs restoring
|
|
584
|
+
load = function
|
|
585
|
+
else:
|
|
586
|
+
if TYPE_CHECKING: # pragma: no cover, no mutate
|
|
587
|
+
assert isinstance(function, FunctionType)
|
|
588
|
+
load = PermuterMethod(function, arrangement)
|
|
502
589
|
self._addSigFunc(sig, load)
|
|
503
590
|
# A decorator stacked above registers the function itself, not the
|
|
504
591
|
# wrapper of the last arrangement.
|
|
@@ -551,6 +638,8 @@ class overload: # NOQA
|
|
|
551
638
|
The 'overload' of the stack, or a new one.
|
|
552
639
|
"""
|
|
553
640
|
self, function = cls._stackOn(func)
|
|
641
|
+
if isinstance(function, abstract):
|
|
642
|
+
cls._refuseAbstractRole('fallback', function.__name__)
|
|
554
643
|
self.__fallback_func__ = function
|
|
555
644
|
return self
|
|
556
645
|
|
|
@@ -573,6 +662,8 @@ class overload: # NOQA
|
|
|
573
662
|
The 'overload' of the stack, or a new one.
|
|
574
663
|
"""
|
|
575
664
|
self, function = cls._stackOn(func)
|
|
665
|
+
if isinstance(function, abstract):
|
|
666
|
+
cls._refuseAbstractRole('finalizer', function.__name__)
|
|
576
667
|
self.__finalizer_func__ = function
|
|
577
668
|
return self
|
|
578
669
|
|
|
@@ -658,7 +749,8 @@ class overload: # NOQA
|
|
|
658
749
|
--------------
|
|
659
750
|
|
|
660
751
|
A single entry that is a function, under any 'classmethod' or
|
|
661
|
-
'staticmethod' layers, or a record of
|
|
752
|
+
'staticmethod' layers, a function declared abstract, or a record of
|
|
753
|
+
another decorator, is refused.
|
|
662
754
|
|
|
663
755
|
Parameters
|
|
664
756
|
----------
|
|
@@ -678,7 +770,7 @@ class overload: # NOQA
|
|
|
678
770
|
if len(types) != 1:
|
|
679
771
|
return
|
|
680
772
|
_, entry = cls.peelMethodKinds(types[0])
|
|
681
|
-
if isinstance(entry, (FunctionType, cls)):
|
|
773
|
+
if isinstance(entry, (FunctionType, cls, abstract)):
|
|
682
774
|
raise BareOverload(decorator, types[0])
|
|
683
775
|
|
|
684
776
|
@staticmethod
|