ducktools-classbuilder 0.2.1__py3-none-any.whl → 0.3.0__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.

Potentially problematic release.


This version of ducktools-classbuilder might be problematic. Click here for more details.

@@ -19,34 +19,12 @@
19
19
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
- __version__ = "v0.2.1"
22
+ __version__ = "v0.3.0"
23
23
 
24
24
  # Change this name if you make heavy modifications
25
25
  INTERNALS_DICT = "__classbuilder_internals__"
26
26
 
27
27
 
28
- def get_internals(cls):
29
- """
30
- Utility function to get the internals dictionary
31
- or return None.
32
-
33
- As generated classes will always have 'fields'
34
- and 'local_fields' attributes this will always
35
- evaluate as 'truthy' if this is a generated class.
36
-
37
- Generally you should use the helper get_flags and
38
- get_fields methods.
39
-
40
- Usage:
41
- if internals := get_internals(cls):
42
- ...
43
-
44
- :param cls: generated class
45
- :return: internals dictionary of the class or None
46
- """
47
- return getattr(cls, INTERNALS_DICT, None)
48
-
49
-
50
28
  def get_fields(cls, *, local=False):
51
29
  """
52
30
  Utility function to gather the fields dictionary
@@ -71,7 +49,9 @@ def get_flags(cls):
71
49
  return getattr(cls, INTERNALS_DICT)["flags"]
72
50
 
73
51
 
74
- def get_inst_fields(inst):
52
+ def _get_inst_fields(inst):
53
+ # This is an internal helper for constructing new
54
+ # 'Field' instances from existing ones.
75
55
  return {
76
56
  k: getattr(inst, k)
77
57
  for k in get_fields(type(inst))
@@ -105,7 +85,7 @@ class MethodMaker:
105
85
  self.code_generator = code_generator
106
86
 
107
87
  def __repr__(self):
108
- return f"<MethodMaker for {self.funcname} method>"
88
+ return f"<MethodMaker for {self.funcname!r} method>"
109
89
 
110
90
  def __get__(self, instance, cls):
111
91
  local_vars = {}
@@ -122,7 +102,7 @@ class MethodMaker:
122
102
  return method.__get__(instance, cls)
123
103
 
124
104
 
125
- def init_maker(cls, *, null=NOTHING):
105
+ def init_maker(cls, *, null=NOTHING, extra_code=None):
126
106
  fields = get_fields(cls)
127
107
  flags = get_flags(cls)
128
108
 
@@ -150,8 +130,17 @@ def init_maker(cls, *, null=NOTHING):
150
130
  assignments.append(assignment)
151
131
 
152
132
  args = ", ".join(arglist)
153
- assigns = "\n ".join(assignments)
154
- code = f"def __init__(self, {args}):\n" f" {assigns}\n"
133
+ assigns = "\n ".join(assignments) if assignments else "pass\n"
134
+ code = (
135
+ f"def __init__(self, {args}):\n"
136
+ f" {assigns}\n"
137
+ )
138
+ # Handle additional function calls
139
+ # Used for validate_field on fieldclasses
140
+ if extra_code:
141
+ for line in extra_code:
142
+ code += f" {line}\n"
143
+
155
144
  return code, globs
156
145
 
157
146
 
@@ -300,7 +289,7 @@ class Field:
300
289
  :param kwargs: Additional keyword arguments for subclasses
301
290
  :return: new field subclass instance
302
291
  """
303
- argument_dict = {**get_inst_fields(fld), **kwargs}
292
+ argument_dict = {**_get_inst_fields(fld), **kwargs}
304
293
 
305
294
  return cls(**argument_dict)
306
295
 
@@ -407,6 +396,14 @@ def slotclass(cls=None, /, *, methods=default_methods, syntax_check=True):
407
396
  return cls
408
397
 
409
398
 
399
+ def _field_init_func(cls):
400
+ # Fields need a different Nothing for their __init__ generation
401
+ # And an extra call to validate_field
402
+ field_nothing = _NothingType()
403
+ extra_calls = ["self.validate_field()"]
404
+ return init_maker(cls, null=field_nothing, extra_code=extra_calls)
405
+
406
+
410
407
  def fieldclass(cls):
411
408
  """
412
409
  This is a special decorator for making Field subclasses using __slots__.
@@ -417,18 +414,7 @@ def fieldclass(cls):
417
414
  :return: Modified subclass
418
415
  """
419
416
 
420
- # Fields need a way to call their validate method
421
- # So append it to the code from __init__.
422
- def field_init_func(cls_):
423
- code, globs = init_maker(cls_, null=field_nothing)
424
- code += " self.validate_field()\n"
425
- return code, globs
426
-
427
- field_nothing = _NothingType()
428
- field_init_desc = MethodMaker(
429
- "__init__",
430
- field_init_func,
431
- )
417
+ field_init_desc = MethodMaker("__init__", _field_init_func)
432
418
  field_methods = frozenset({field_init_desc, repr_desc, eq_desc})
433
419
 
434
420
  cls = builder(
@@ -6,13 +6,11 @@ _py_type = type # Alias for type where it is used as a name
6
6
  __version__: str
7
7
  INTERNALS_DICT: str
8
8
 
9
- def get_internals(cls) -> dict[str, typing.Any] | None: ...
10
-
11
9
  def get_fields(cls: type, *, local: bool = False) -> dict[str, Field]: ...
12
10
 
13
11
  def get_flags(cls:type) -> dict[str, bool]: ...
14
12
 
15
- def get_inst_fields(inst: typing.Any) -> dict[str, typing.Any]: ...
13
+ def _get_inst_fields(inst: typing.Any) -> dict[str, typing.Any]: ...
16
14
 
17
15
  class _NothingType:
18
16
  ...
@@ -32,6 +30,7 @@ def init_maker(
32
30
  cls: type,
33
31
  *,
34
32
  null: _NothingType = NOTHING,
33
+ extra_code: None | list[str] = None
35
34
  ) -> tuple[str, dict[str, typing.Any]]: ...
36
35
  def repr_maker(cls: type) -> tuple[str, dict[str, typing.Any]]: ...
37
36
  def eq_maker(cls: type) -> tuple[str, dict[str, typing.Any]]: ...
@@ -6,7 +6,7 @@ from collections.abc import Callable
6
6
  from . import (
7
7
  INTERNALS_DICT, NOTHING,
8
8
  Field, MethodMaker, SlotFields as SlotFields,
9
- builder, fieldclass, get_internals, slot_gatherer
9
+ builder, fieldclass, get_flags, get_fields, slot_gatherer
10
10
  )
11
11
 
12
12
  # noinspection PyUnresolvedReferences
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ducktools-classbuilder
3
- Version: 0.2.1
3
+ Version: 0.3.0
4
4
  Summary: Toolkit for creating class boilerplate generators
5
5
  Author: David C Ellis
6
6
  License: MIT License
@@ -0,0 +1,10 @@
1
+ ducktools/classbuilder/__init__.py,sha256=Vw5RjzzIkXGYXKOmFWTxPsIAM4ceg74Q1Oku4fwF0ek,13505
2
+ ducktools/classbuilder/__init__.pyi,sha256=hgcEylMAHgfonOt2fH9OCE3gU60mYkiOUe7e8mTwNVs,2857
3
+ ducktools/classbuilder/prefab.py,sha256=gJJCtTAbQLgIoo79jRWVp09r3u8kJjcqXp4G4FTu9j8,29887
4
+ ducktools/classbuilder/prefab.pyi,sha256=vcaEpBxyVsOTwkGBkVgmbpn8DPqQqFnAU4xTKPnpOd8,3977
5
+ ducktools/classbuilder/py.typed,sha256=la67KBlbjXN-_-DfGNcdOcjYumVpKG_Tkw-8n5dnGB4,8
6
+ ducktools_classbuilder-0.3.0.dist-info/LICENSE.md,sha256=6Thz9Dbw8R4fWInl6sGl8Rj3UnKnRbDwrc6jZerpugQ,1070
7
+ ducktools_classbuilder-0.3.0.dist-info/METADATA,sha256=sAWw76LgN3HruCS-SXURrrAj2A6MmhsyDJwMSvnWW2w,9280
8
+ ducktools_classbuilder-0.3.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
9
+ ducktools_classbuilder-0.3.0.dist-info/top_level.txt,sha256=uSDLtio3ZFqdwcsMJ2O5yhjB4Q3ytbBWbA8rJREganc,10
10
+ ducktools_classbuilder-0.3.0.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- ducktools/classbuilder/__init__.py,sha256=yZOiy84vo8z0iotOgY1rFwBBngveh1cwoWjcQdRndpw,13747
2
- ducktools/classbuilder/__init__.pyi,sha256=-QHJhPn4EjI4XW4OI74MKNg2tGKJiM3w0ELSOvPstAw,2877
3
- ducktools/classbuilder/prefab.py,sha256=gJJCtTAbQLgIoo79jRWVp09r3u8kJjcqXp4G4FTu9j8,29887
4
- ducktools/classbuilder/prefab.pyi,sha256=GsllqqZ_Wz6i_DenKv-pAbtES3sPanC0g8O7fkNTnvs,3969
5
- ducktools/classbuilder/py.typed,sha256=la67KBlbjXN-_-DfGNcdOcjYumVpKG_Tkw-8n5dnGB4,8
6
- ducktools_classbuilder-0.2.1.dist-info/LICENSE.md,sha256=6Thz9Dbw8R4fWInl6sGl8Rj3UnKnRbDwrc6jZerpugQ,1070
7
- ducktools_classbuilder-0.2.1.dist-info/METADATA,sha256=kV--mUdR1y0NmszwMobZdnNvigtepG_WF_axupjf6_4,9280
8
- ducktools_classbuilder-0.2.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
9
- ducktools_classbuilder-0.2.1.dist-info/top_level.txt,sha256=uSDLtio3ZFqdwcsMJ2O5yhjB4Q3ytbBWbA8rJREganc,10
10
- ducktools_classbuilder-0.2.1.dist-info/RECORD,,