worktoy 1.3.0.dev1__py3-none-any.whl → 1.3.0.dev2__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 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.3.0-dev1'
51
+ __version__ = '1.3.0-dev2'
52
52
 
53
53
  from . import utilities
54
54
  from . import waitaminute
@@ -8,8 +8,9 @@ from __future__ import annotations
8
8
  from typing import TYPE_CHECKING, overload
9
9
 
10
10
  from ..core import MetaType
11
- from ..utilities import textFmt
11
+ from ..utilities import textFmt, typeCast
12
12
  from ..waitaminute import DeprecatedPromise
13
+ from ..waitaminute.dispatch import TypeCastException
13
14
  from ..waitaminute.keenum import KeeResolveError, KeeWriteOnceError
14
15
  from . import Kee
15
16
 
@@ -77,9 +78,10 @@ class KeeBase(metaclass=MetaType):
77
78
 
78
79
  index : int
79
80
  Deprecated: the position of the member among those of the class that
80
- declared it. Reading it, 'int(member)' and using the member as an
81
- index give a 'DeprecatedPromise', since the position belongs to the
82
- enumeration: 'Day[x]' gives the member at a position.
81
+ declared it. Reading it gives a 'DeprecatedPromise', since the
82
+ position belongs to the enumeration: 'Day[x]' gives the member at a
83
+ position. 'int(member)', and the member used as an index, go by the
84
+ value and not by the position; see '__int__' and '__index__'.
83
85
 
84
86
  value : Any
85
87
  The value the 'Kee' built.
@@ -218,21 +220,76 @@ class KeeBase(metaclass=MetaType):
218
220
 
219
221
  def __int__(self) -> int:
220
222
  """
221
- 'int(member)' gives the deprecated position, with a
222
- 'DeprecatedPromise'; see '_readIndex'.
223
+ A member stands for its value, so 'int(member)' must give the value
224
+ as an integer wherever the value is a whole number, as
225
+ 'int(Size.LARGE)' gives 3 for a member holding 3. Where the member
226
+ stands in its class plays no part. A value that is no whole number
227
+ has no integer to give, and the conversion must then fail as Python
228
+ fails 'int' of an object that is no number, without truncating a
229
+ fraction or parsing a text on the way.
230
+
231
+ Implementation
232
+ --------------
233
+
234
+ 'typeCast' decides, as for every conversion in the library: an
235
+ 'int', a whole 'float' and a real, whole 'complex' convert, and
236
+ nothing else does. Its refusal is raised again as a 'TypeError'
237
+ naming the member. The result is made a plain 'int', which Python
238
+ asks of '__int__'.
239
+
240
+ Returns
241
+ -------
242
+
243
+ int
244
+ The value of the member as an integer.
245
+
246
+ Raises
247
+ ------
248
+
249
+ TypeError
250
+ The value is no whole number.
223
251
  """
224
- info = textFmt(_POSITION)
225
- DeprecatedPromise('worktoy.keenum.KeeBase.__int__', info).warn()
226
- return self.__member_index__
252
+ try:
253
+ return int(typeCast(int, self.value))
254
+ except TypeCastException as typeCastException:
255
+ infoSpec = """Enumeration member '%s' converts to no 'int': its
256
+ value of type '%s' is no whole number!"""
257
+ info = infoSpec % (self, type(self.value).__name__)
258
+ raise TypeError(textFmt(info)) from typeCastException
227
259
 
228
260
  def __index__(self) -> int:
229
261
  """
230
- A member used as an index, as in 'items[member]', gives the
231
- deprecated position, with a 'DeprecatedPromise'; see '_readIndex'.
262
+ A member whose value is an integer must serve wherever Python takes
263
+ an integer without loss, as an index in 'items[member]' or a count
264
+ in 'range(member)', by that value. Python asks more of an index
265
+ than of 'int': a whole 'float' converts with 'int' and is still no
266
+ index, and the same must hold for a member holding one.
267
+
268
+ Implementation
269
+ --------------
270
+
271
+ A value that is an 'int' is returned as a plain 'int', which Python
272
+ asks of '__index__'; any other value raises 'TypeError'.
273
+
274
+ Returns
275
+ -------
276
+
277
+ int
278
+ The value of the member.
279
+
280
+ Raises
281
+ ------
282
+
283
+ TypeError
284
+ The value is no 'int'.
232
285
  """
233
- info = textFmt(_POSITION)
234
- DeprecatedPromise('worktoy.keenum.KeeBase.__index__', info).warn()
235
- return self.__member_index__
286
+ value = self.value
287
+ if isinstance(value, int):
288
+ return int(value)
289
+ infoSpec = """Enumeration member '%s' serves as no index: its value
290
+ of type '%s' is no 'int'!"""
291
+ info = infoSpec % (self, type(value).__name__)
292
+ raise TypeError(textFmt(info))
236
293
 
237
294
  def __hash__(self) -> int:
238
295
  """
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: worktoy
3
- Version: 1.3.0.dev1
3
+ Version: 1.3.0.dev2
4
4
  Summary: Declarative structure for Python that enforces types at runtime and enables true function overloading
5
5
  Author-email: Asger Jon Vistisen <asgerjon2@gmail.com>
6
6
  License-Expression: Apache-2.0
@@ -32,7 +32,7 @@ Dynamic: license-file
32
32
  [![PyPI version](https://img.shields.io/pypi/v/worktoy)](https://pypi.org/project/worktoy/)
33
33
  [![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
34
34
 
35
- # worktoy v1.3.0-dev1 (development build)
35
+ # worktoy v1.3.0-dev2 (development build)
36
36
 
37
37
  **worktoy** provides utilities for Python development focused on
38
38
  reducing boilerplate while improving type safety and readability. Each
@@ -48,6 +48,7 @@ to 3.15.
48
48
  - ['Trust-Me-Bro'-Typing](#trust-me-bro-typing)
49
49
  - [The Python Parsing Situation Is Crazy](#the-python-parsing-situation-is-crazy)
50
50
  - ["Show Don't Tell" Is for Stories, not for Code!](#show-dont-tell-is-for-stories-not-for-code)
51
+ - [Abstract Classes That Check the Homework](#abstract-classes-that-check-the-homework)
51
52
  - [Static Discipline](#static-discipline)
52
53
  - [Contributing](#contributing)
53
54
  - [License](#license)
@@ -285,6 +286,141 @@ The `r` attribute is declared first. Next, the `@r.GET` declares that the
285
286
  method implementing the get operation comes next. The structure is
286
287
  visible separately from the behaviour.
287
288
 
289
+ ## Abstract Classes That Check the Homework
290
+
291
+ Python's `abc` lets a class leave methods for its subclasses to write:
292
+
293
+ ```python
294
+ from abc import ABC, abstractmethod
295
+
296
+
297
+ class Model(ABC):
298
+ @abstractmethod
299
+ def rowCount(self) -> int:
300
+ pass
301
+
302
+
303
+ class Names(Model):
304
+ def rowCount(self, parent) -> int: # one argument too many
305
+ return 3
306
+ ```
307
+
308
+ `abc` checks names, nothing else. `Names` has something called
309
+ `rowCount`, so `Names()` is created without a word, and the mistake waits
310
+ for the first call: `rowCount() missing 1 required positional argument`,
311
+ far from the class that made it. And `abc` has never heard of overloads,
312
+ so it cannot ask a subclass for one particular signature.
313
+
314
+ With **worktoy**, the base is `ABCObject` and the decorator is
315
+ `@abstract`:
316
+
317
+ ```python
318
+ from worktoy.mcls import ABCObject
319
+ from worktoy.dispatch import abstract
320
+
321
+
322
+ class Model(ABCObject):
323
+ @abstract
324
+ def rowCount(self) -> int: ...
325
+
326
+
327
+ class Names(Model): # WrongImplementation, right here
328
+ def rowCount(self, parent) -> int:
329
+ return 3
330
+ ```
331
+
332
+ What a subclass writes is held to what was declared, as the class is
333
+ created: the same kind of method, taking what the abstract one takes.
334
+ `Names` fails at its class statement with `WrongImplementation`. Until
335
+ every abstract method is written, making an object is refused with
336
+ `MissingImplementation`, naming what is left to write. A subclass may
337
+ write some of them, all or none, and may declare abstract methods of its
338
+ own.
339
+
340
+ A base class can also demand particular type signatures of an
341
+ overloaded name, two or more of them, and a subclass has to write every
342
+ one:
343
+
344
+ ```python
345
+ from worktoy.dispatch import overload
346
+
347
+
348
+ class Source(ABCObject):
349
+ @overload(str, int)
350
+ @abstract
351
+ def data(self, index: str, role: int) -> object: ...
352
+
353
+ @overload(bytes)
354
+ @abstract
355
+ def data(self, index: bytes) -> object: ...
356
+
357
+
358
+ class Text(Source):
359
+ @overload(str, int)
360
+ def data(self, index: str, role: int) -> object:
361
+ return index * role
362
+ ```
363
+
364
+ `Text` writes `data(str, int)` but not `data(bytes)`, so it is still
365
+ abstract: `Text()` is refused, and a subclass writing `data(bytes)`
366
+ completes it. One plain function at `data` taking `*args` would write
367
+ both signatures at once.
368
+
369
+ Where this pays off is a family of classes sharing one job. Take the
370
+ `Point` from above. Every transform must accept a `Point`, a pair of
371
+ floats or a complex number:
372
+
373
+ ```python
374
+ class Transform(ABCObject):
375
+ @overload(Point)
376
+ @abstract
377
+ def apply(self, point: Point) -> Point: ...
378
+
379
+ @overload(float, float)
380
+ @abstract
381
+ def apply(self, x: float, y: float) -> Point: ...
382
+
383
+ @abstract # above '@overload' or below it, either works
384
+ @overload(complex)
385
+ def apply(self, z: complex) -> Point: ...
386
+
387
+
388
+ class PointTransform(Transform):
389
+ @overload(float, float)
390
+ def apply(self, x: float, y: float) -> Point:
391
+ return self.apply(Point(x, y))
392
+
393
+ @overload(complex)
394
+ def apply(self, z: complex) -> Point:
395
+ return self.apply(Point(z))
396
+
397
+
398
+ class Shift(PointTransform):
399
+ dx = AttriBox[float](0.0)
400
+ dy = AttriBox[float](0.0)
401
+
402
+ @overload(Point)
403
+ def apply(self, point: Point) -> Point:
404
+ return Point(point.x + self.dx, point.y + self.dy)
405
+ ```
406
+
407
+ `Transform` demands three signatures. `PointTransform` writes two of
408
+ them in terms of the third, once and for all, and is still abstract:
409
+ `PointTransform()` is refused, since `apply(Point)` is not implemented.
410
+ A concrete transform then has a single thing left to say, how it moves
411
+ a `Point`, and gets the other two for free:
412
+
413
+ ```python
414
+ shift = Shift()
415
+ shift.dx, shift.dy = 1.0, 2.0
416
+ shift.apply(Point(0.0, 0.0)).y == 2.0
417
+ shift.apply(3.0, 4.0).x == 4.0
418
+ shift.apply(3 + 4j).y == 6.0
419
+ ```
420
+
421
+ `ABCObject` is `BaseObject` built by worktoy's own `ABCMeta`, which is no
422
+ relative of the one in `abc`.
423
+
288
424
  ## Static Discipline
289
425
 
290
426
  In plain Python, attributes assigned in `__init__` are closer to
@@ -1,4 +1,4 @@
1
- worktoy/__init__.py,sha256=IBgDlywIwr0jwkOfslaSYTZefiE7ApUv-vhpv1Sr0Uo,2899
1
+ worktoy/__init__.py,sha256=ISg9Jq_ZdKg-wCNCNitU-AeH-VqpM8caY8fWY5fi9rQ,2899
2
2
  worktoy/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  worktoy/core/__init__.py,sha256=dUNFlZa37R95JPn_6spjQDhI_A3LBJV34NGiyElV5gs,604
4
4
  worktoy/core/_instance_owner.py,sha256=Nb0j0Wp8uXROUTLMVGWS_Krl6mlUcelo24vu07U4_bo,2144
@@ -54,7 +54,7 @@ worktoy/keenum/_kee_flags_space.py,sha256=QnNAltkysTD5V65YwoaVuvn8o0N3qgpCsh1jmc
54
54
  worktoy/keenum/_kee_member.py,sha256=iy8bBsS8BsqSZAKRxZR-m6yD2WhhgDpumLGqAudNuOA,10806
55
55
  worktoy/keenum/_kee_meta.py,sha256=en48TJA-uigpK-8ubkq-COVQMOi0kULAU6tHTgBepUM,27807
56
56
  worktoy/keenum/_kee_meta_meta.py,sha256=1lkCQ6Je48FXXrR6oZPACPn0YX5T8033N89oCiXefB0,4586
57
- worktoy/keenum/_kee_num.py,sha256=4KL4uan42cXix5VdgTc2noEwwOjK-_PlD2U-p1MsLkU,10255
57
+ worktoy/keenum/_kee_num.py,sha256=olfJ5Js1Yhh8Us6jGLdAL0TJS22PqK5n0x9ovTnPDfY,12097
58
58
  worktoy/keenum/_kee_space.py,sha256=gGnnGE8MAnYKnoo1RohM2sZxpWIBKCdKlBTaQjeZXhw,8573
59
59
  worktoy/keenum/_kee_space_hook.py,sha256=5ASpbjUz079MI25oPa5_B63mrFhmVnF0_IMMENfSMMw,8819
60
60
  worktoy/lorem_ipsum/__init__.py,sha256=vRLsp7gpyTqqx0PzS9aQlYiA6JnCG-Z6awHgztJQFX4,1088
@@ -201,8 +201,8 @@ worktoy/work_test/samplers/_lorem_text.py,sha256=KQgBKQ5iLS_lU7HGvfoLTVsFyDcm-JR
201
201
  worktoy/work_test/samplers/_sampler_meta.py,sha256=9AodXKw3Ww-Eu1qzqJsRm-AbYolgON9TKA5fdfBZ9bY,1216
202
202
  worktoy/work_test/samplers/_symbolic_sampler.py,sha256=VgheImGi_-WiUYa1o-Htu1OSjoOqTzx8PycDzf-es4c,3042
203
203
  worktoy/work_test/samplers/_word_sampler.py,sha256=9K5ib8Wt2APUEMh2ZGMRNSkHjnUpl_HDl8OdFSgw1BU,1044
204
- worktoy-1.3.0.dev1.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
205
- worktoy-1.3.0.dev1.dist-info/METADATA,sha256=0ZLD_TZ1cA1dS5WYzvMN4N_3RizWHtGygH5vwsgSUjk,10656
206
- worktoy-1.3.0.dev1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
207
- worktoy-1.3.0.dev1.dist-info/top_level.txt,sha256=6yO8xnr7WakfINq91qO3rs5ECubDOGvAQ9ztVgoG0j4,8
208
- worktoy-1.3.0.dev1.dist-info/RECORD,,
204
+ worktoy-1.3.0.dev2.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
205
+ worktoy-1.3.0.dev2.dist-info/METADATA,sha256=cou2kx97wm4Jspqk0cWJlFyLsK9j1sH6AoFqeYqMAwE,14474
206
+ worktoy-1.3.0.dev2.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
207
+ worktoy-1.3.0.dev2.dist-info/top_level.txt,sha256=6yO8xnr7WakfINq91qO3rs5ECubDOGvAQ9ztVgoG0j4,8
208
+ worktoy-1.3.0.dev2.dist-info/RECORD,,