ducktools-classbuilder 0.12.1__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.
@@ -0,0 +1,1252 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2024 David C Ellis
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ # In this module there are some internal bits of circular logic.
24
+ #
25
+ # 'Field' needs to exist in order to be used in gatherers, but is itself a
26
+ # partially constructed class. These constructed attributes are placed on
27
+ # 'Field' post construction.
28
+ #
29
+ # The 'SlotMakerMeta' metaclass generates 'Field' instances to go in __slots__
30
+ # but is also the metaclass used to construct 'Field'.
31
+ # Field itself sidesteps this by defining __slots__ to avoid that branch.
32
+
33
+ import os
34
+ import sys
35
+
36
+ try:
37
+ # Use the internal C module if it is available
38
+ from _types import ( # type: ignore
39
+ MemberDescriptorType as _MemberDescriptorType,
40
+ MappingProxyType as _MappingProxyType
41
+ )
42
+ except ImportError:
43
+ from types import (
44
+ MemberDescriptorType as _MemberDescriptorType,
45
+ MappingProxyType as _MappingProxyType,
46
+ )
47
+
48
+ from .annotations import get_ns_annotations, is_classvar
49
+ from ._version import __version__, __version_tuple__ # noqa: F401
50
+
51
+ # Change this name if you make heavy modifications
52
+ INTERNALS_DICT = "__classbuilder_internals__"
53
+ META_GATHERER_NAME = "_meta_gatherer"
54
+ GATHERED_DATA = "__classbuilder_gathered_fields__"
55
+
56
+ # If testing, make Field classes frozen to make sure attributes are not
57
+ # overwritten. When running this is a performance penalty so it is not required.
58
+ _UNDER_TESTING = os.environ.get("PYTEST_VERSION") is not None
59
+
60
+
61
+ def get_fields(cls, *, local=False):
62
+ """
63
+ Utility function to gather the fields dictionary
64
+ from the class internals.
65
+
66
+ :param cls: generated class
67
+ :param local: get only fields that were not inherited
68
+ :return: dictionary of keys and Field attribute info
69
+ """
70
+ key = "local_fields" if local else "fields"
71
+ try:
72
+ return getattr(cls, INTERNALS_DICT)[key]
73
+ except AttributeError:
74
+ raise TypeError(f"{cls} is not a classbuilder generated class")
75
+
76
+
77
+ def get_flags(cls):
78
+ """
79
+ Utility function to gather the flags dictionary
80
+ from the class internals.
81
+
82
+ :param cls: generated class
83
+ :return: dictionary of keys and flag values
84
+ """
85
+ try:
86
+ return getattr(cls, INTERNALS_DICT)["flags"]
87
+ except AttributeError:
88
+ raise TypeError(f"{cls} is not a classbuilder generated class")
89
+
90
+
91
+ def get_methods(cls):
92
+ """
93
+ Utility function to gather the set of methods
94
+ from the class internals.
95
+
96
+ :param cls: generated class
97
+ :return: dict of generated methods attached to the class by name
98
+ """
99
+ try:
100
+ return getattr(cls, INTERNALS_DICT)["methods"]
101
+ except AttributeError:
102
+ raise TypeError(f"{cls} is not a classbuilder generated class")
103
+
104
+
105
+ def get_generated_code(cls):
106
+ """
107
+ Retrieve the source code, globals and annotations of all generated methods
108
+ as they would be generated for a specific class.
109
+
110
+ :param cls: generated class
111
+ :return: dict of generated method names and the GeneratedCode objects for the class
112
+ """
113
+ methods = get_methods(cls)
114
+ source = {
115
+ name: method.code_generator(cls)
116
+ for name, method in methods.items()
117
+ }
118
+
119
+ return source
120
+
121
+
122
+ def print_generated_code(cls):
123
+ """
124
+ Print out all of the generated source code that will be executed for this class
125
+
126
+ This function is useful when checking that your code generators are writing source
127
+ code as expected.
128
+
129
+ :param cls: generated class
130
+ """
131
+ import textwrap
132
+
133
+ source = get_generated_code(cls)
134
+
135
+ source_list = []
136
+ globs_list = []
137
+ annotation_list = []
138
+
139
+ for name, method in source.items():
140
+ source_list.append(method.source_code)
141
+ if method.globs:
142
+ globs_list.append(f"{name}: {method.globs}")
143
+ if method.annotations:
144
+ annotation_list.append(f"{name}: {method.annotations}")
145
+
146
+ print("Source:")
147
+ print(textwrap.indent("\n".join(source_list), " "))
148
+ if globs_list:
149
+ print("\nGlobals:")
150
+ print(textwrap.indent("\n".join(globs_list), " "))
151
+ if annotation_list:
152
+ print("\nAnnotations:")
153
+ print(textwrap.indent("\n".join(annotation_list), " "))
154
+
155
+
156
+ def build_completed(ns):
157
+ """
158
+ Utility function to determine if a class has completed the construction
159
+ process.
160
+
161
+ :param ns: class namespace
162
+ :return: True if built, False otherwise
163
+ """
164
+ try:
165
+ return ns[INTERNALS_DICT]["build_complete"]
166
+ except KeyError:
167
+ return False
168
+
169
+
170
+ def _get_inst_fields(inst):
171
+ # This is an internal helper for constructing new
172
+ # 'Field' instances from existing ones.
173
+ return {
174
+ k: getattr(inst, k)
175
+ for k in get_fields(type(inst))
176
+ }
177
+
178
+
179
+ # As 'None' can be a meaningful value we need a sentinel value
180
+ # to use to show no value has been provided.
181
+ class _NothingType:
182
+ def __init__(self, custom=None):
183
+ self.custom = custom
184
+ def __repr__(self):
185
+ if self.custom:
186
+ return f"<{self.custom} NOTHING OBJECT>"
187
+ return "<NOTHING OBJECT>"
188
+
189
+
190
+ NOTHING = _NothingType()
191
+ FIELD_NOTHING = _NothingType("FIELD")
192
+
193
+
194
+ # KW_ONLY sentinel 'type' to use to indicate all subsequent attributes are
195
+ # keyword only
196
+ # noinspection PyPep8Naming
197
+ class _KW_ONLY_META(type):
198
+ def __repr__(self):
199
+ return "<KW_ONLY Sentinel>"
200
+
201
+
202
+ class KW_ONLY(metaclass=_KW_ONLY_META):
203
+ """
204
+ Sentinel Class to indicate that variables declared after
205
+ this sentinel are to be converted to KW_ONLY arguments.
206
+ """
207
+
208
+
209
+ class GeneratedCode:
210
+ """
211
+ This class provides a return value for the generated output from source code
212
+ generators.
213
+ """
214
+ __slots__ = ("source_code", "globs", "annotations")
215
+
216
+ def __init__(self, source_code, globs, annotations=None):
217
+ self.source_code = source_code
218
+ self.globs = globs
219
+ self.annotations = annotations
220
+
221
+ def __repr__(self):
222
+ first_source_line = self.source_code.split("\n")[0]
223
+ return (
224
+ f"GeneratorOutput(source_code='{first_source_line} ...', "
225
+ f"globs={self.globs!r}, annotations={self.annotations!r})"
226
+ )
227
+
228
+ def __eq__(self, other):
229
+ if self.__class__ is other.__class__:
230
+ return (
231
+ self.source_code,
232
+ self.globs,
233
+ self.annotations,
234
+ ) == (
235
+ other.source_code,
236
+ other.globs,
237
+ other.annotations,
238
+ )
239
+ return NotImplemented
240
+
241
+
242
+ class MethodMaker:
243
+ """
244
+ The descriptor class to place where methods should be generated.
245
+ This delays the actual generation and `exec` until the method is needed.
246
+
247
+ This is used to convert a code generator that returns code and a globals
248
+ dictionary into a descriptor to assign on a generated class.
249
+ """
250
+ def __init__(self, funcname, code_generator):
251
+ """
252
+ :param funcname: name of the generated function eg `__init__`
253
+ :param code_generator: code generator function to operate on a class.
254
+ """
255
+ self.funcname = funcname
256
+ self.code_generator = code_generator
257
+
258
+ def __repr__(self):
259
+ return f"<MethodMaker for {self.funcname!r} method>"
260
+
261
+ def __get__(self, inst, cls):
262
+ local_vars = {}
263
+
264
+ # This can be called via super().funcname(...) in which case the class
265
+ # may not be the correct one. If this is the correct class
266
+ # it should have this descriptor in the class dict under
267
+ # the correct funcname.
268
+ # Otherwise is should be found in the MRO of the class.
269
+ if cls.__dict__.get(self.funcname) is self:
270
+ gen_cls = cls
271
+ else:
272
+ for c in cls.__mro__[1:]: # skip 'cls' as special cased
273
+ if c.__dict__.get(self.funcname) is self:
274
+ gen_cls = c
275
+ break
276
+ else: # pragma: no cover
277
+ # This should only be reached if called with incorrect arguments
278
+ # manually
279
+ raise AttributeError(
280
+ f"Could not find {self!r} in class {cls.__name__!r} MRO."
281
+ )
282
+
283
+ gen = self.code_generator(gen_cls, self.funcname)
284
+ exec(gen.source_code, gen.globs, local_vars)
285
+ method = local_vars.get(self.funcname)
286
+
287
+ try:
288
+ method.__qualname__ = f"{gen_cls.__qualname__}.{self.funcname}"
289
+ except AttributeError:
290
+ # This might be a property or some other special
291
+ # descriptor. Don't try to rename.
292
+ pass
293
+
294
+ # Apply annotations
295
+ if gen.annotations is not None:
296
+ method.__annotations__ = gen.annotations
297
+
298
+ # Replace this descriptor on the class with the generated function
299
+ setattr(gen_cls, self.funcname, method)
300
+
301
+ # Use 'get' to return the generated function as a bound method
302
+ # instead of as a regular function for first usage.
303
+ return method.__get__(inst, cls)
304
+
305
+
306
+ class _SignatureMaker:
307
+ # 'inspect.signature' calls the `__get__` method of the `__init__` methodmaker with
308
+ # the wrong arguments.
309
+ # Instead of __get__(None, cls) or __get__(inst, type(inst))
310
+ # it uses __get__(cls, type(cls)).
311
+ #
312
+ # If this is done before `__init__` has been generated then
313
+ # help(cls) will fail along with inspect.signature(cls)
314
+ # This signature maker descriptor is placed to override __signature__ and force
315
+ # the `__init__` signature to be generated first if the signature is requested.
316
+ def __get__(self, instance, cls=None):
317
+ if cls is None:
318
+ cls = type(instance)
319
+
320
+ # force generation of `__init__` function
321
+ _ = cls.__init__
322
+
323
+ if instance is None:
324
+ raise AttributeError(
325
+ f"type object {cls.__name__!r} "
326
+ "has no attribute '__signature__'"
327
+ )
328
+ else:
329
+ raise AttributeError(
330
+ f"{cls.__name__!r} object"
331
+ "has no attribute '__signature__'"
332
+ )
333
+
334
+
335
+ signature_maker = _SignatureMaker()
336
+
337
+
338
+ def get_init_generator(null=NOTHING, extra_code=None):
339
+ def cls_init_maker(cls, funcname="__init__"):
340
+ fields = get_fields(cls)
341
+ flags = get_flags(cls)
342
+
343
+ arglist = []
344
+ kw_only_arglist = []
345
+ assignments = []
346
+ globs = {}
347
+
348
+ kw_only_flag = flags.get("kw_only", False)
349
+
350
+ for k, v in fields.items():
351
+ if v.init:
352
+ if v.default is not null:
353
+ globs[f"_{k}_default"] = v.default
354
+ arg = f"{k}=_{k}_default"
355
+ assignment = f"self.{k} = {k}"
356
+ elif v.default_factory is not null:
357
+ globs[f"_{k}_factory"] = v.default_factory
358
+ arg = f"{k}=None"
359
+ assignment = f"self.{k} = _{k}_factory() if {k} is None else {k}"
360
+ else:
361
+ arg = f"{k}"
362
+ assignment = f"self.{k} = {k}"
363
+
364
+ if kw_only_flag or v.kw_only:
365
+ kw_only_arglist.append(arg)
366
+ else:
367
+ arglist.append(arg)
368
+
369
+ assignments.append(assignment)
370
+ else:
371
+ if v.default is not null:
372
+ globs[f"_{k}_default"] = v.default
373
+ assignment = f"self.{k} = _{k}_default"
374
+ assignments.append(assignment)
375
+ elif v.default_factory is not null:
376
+ globs[f"_{k}_factory"] = v.default_factory
377
+ assignment = f"self.{k} = _{k}_factory()"
378
+ assignments.append(assignment)
379
+
380
+ pos_args = ", ".join(arglist)
381
+ kw_args = ", ".join(kw_only_arglist)
382
+ if pos_args and kw_args:
383
+ args = f"{pos_args}, *, {kw_args}"
384
+ elif kw_args:
385
+ args = f"*, {kw_args}"
386
+ else:
387
+ args = pos_args
388
+
389
+ assigns = "\n ".join(assignments) if assignments else "pass\n"
390
+ code = (
391
+ f"def {funcname}(self, {args}):\n"
392
+ f" {assigns}\n"
393
+ )
394
+ # Handle additional function calls
395
+ # Used for validate_field on fieldclasses
396
+ if extra_code:
397
+ for line in extra_code:
398
+ code += f" {line}\n"
399
+
400
+ return GeneratedCode(code, globs)
401
+
402
+ return cls_init_maker
403
+
404
+
405
+ init_generator = get_init_generator()
406
+
407
+
408
+ def get_repr_generator(recursion_safe=False, eval_safe=False):
409
+ """
410
+
411
+ :param recursion_safe: use reprlib.recursive_repr
412
+ :param eval_safe: if the repr is known not to eval correctly,
413
+ generate a repr which will intentionally
414
+ not evaluate.
415
+ :return:
416
+ """
417
+ def cls_repr_generator(cls, funcname="__repr__"):
418
+ fields = get_fields(cls)
419
+
420
+ globs = {}
421
+ will_eval = True
422
+ valid_names = []
423
+
424
+ for name, fld in fields.items():
425
+ if fld.repr:
426
+ valid_names.append(name)
427
+
428
+ if will_eval and (fld.init ^ fld.repr):
429
+ will_eval = False
430
+
431
+ content = ", ".join(
432
+ f"{name}={{self.{name}!r}}"
433
+ for name in valid_names
434
+ )
435
+
436
+ if recursion_safe:
437
+ import reprlib
438
+ globs["_recursive_repr"] = reprlib.recursive_repr()
439
+ recursion_func = "@_recursive_repr\n"
440
+ else:
441
+ recursion_func = ""
442
+
443
+ if eval_safe and will_eval is False:
444
+ if content:
445
+ code = (
446
+ f"{recursion_func}"
447
+ f"def {funcname}(self):\n"
448
+ f" return f'<generated class {{type(self).__qualname__}}; {content}>'\n"
449
+ )
450
+ else:
451
+ code = (
452
+ f"{recursion_func}"
453
+ f"def {funcname}(self):\n"
454
+ f" return f'<generated class {{type(self).__qualname__}}>'\n"
455
+ )
456
+ else:
457
+ code = (
458
+ f"{recursion_func}"
459
+ f"def {funcname}(self):\n"
460
+ f" return f'{{type(self).__qualname__}}({content})'\n"
461
+ )
462
+
463
+ return GeneratedCode(code, globs)
464
+ return cls_repr_generator
465
+
466
+
467
+ repr_generator = get_repr_generator()
468
+
469
+
470
+ def eq_generator(cls, funcname="__eq__"):
471
+ class_comparison = "self.__class__ is other.__class__"
472
+ field_names = [
473
+ name
474
+ for name, attrib in get_fields(cls).items()
475
+ if attrib.compare
476
+ ]
477
+
478
+ if field_names:
479
+ instance_comparison = "\n and ".join(
480
+ f"self.{name} == other.{name}" for name in field_names
481
+ )
482
+ else:
483
+ instance_comparison = "True"
484
+
485
+ code = (
486
+ f"def {funcname}(self, other):\n"
487
+ f" return (\n"
488
+ f" {instance_comparison}\n"
489
+ f" ) if {class_comparison} else NotImplemented\n"
490
+ )
491
+ globs = {}
492
+
493
+ return GeneratedCode(code, globs)
494
+
495
+
496
+ def replace_generator(cls, funcname="__replace__"):
497
+ # Generate the replace method for built classes
498
+ # unlike the dataclasses implementation this is generated
499
+ attribs = get_fields(cls)
500
+
501
+ # This is essentially the as_dict generator for prefabs
502
+ # except based on attrib.init instead of .serialize
503
+ vals = ", ".join(
504
+ f"'{name}': self.{name}"
505
+ for name, attrib in attribs.items()
506
+ if attrib.init
507
+ )
508
+ init_dict = f"{{{vals}}}"
509
+
510
+ code = (
511
+ f"def {funcname}(self, /, **changes):\n"
512
+ f" new_kwargs = {init_dict}\n"
513
+ f" new_kwargs |= changes\n"
514
+ f" return self.__class__(**new_kwargs)\n"
515
+ )
516
+ globs = {}
517
+ return GeneratedCode(code, globs)
518
+
519
+
520
+ def frozen_setattr_generator(cls, funcname="__setattr__"):
521
+ globs = {}
522
+ field_names = set(get_fields(cls))
523
+ flags = get_flags(cls)
524
+
525
+ globs["__field_names"] = field_names
526
+
527
+ # Better to be safe and use the method that works in both cases
528
+ # if somehow slotted has not been set.
529
+ if flags.get("slotted", True):
530
+ globs["__setattr_func"] = object.__setattr__
531
+ setattr_method = "__setattr_func(self, name, value)"
532
+ hasattr_check = "hasattr(self, name)"
533
+ else:
534
+ setattr_method = "self.__dict__[name] = value"
535
+ hasattr_check = "name in self.__dict__"
536
+
537
+ body = (
538
+ f" if {hasattr_check} or name not in __field_names:\n"
539
+ f' raise TypeError(\n'
540
+ f' f"{{type(self).__name__!r}} object does not support "'
541
+ f' f"attribute assignment"\n'
542
+ f' )\n'
543
+ f" else:\n"
544
+ f" {setattr_method}\n"
545
+ )
546
+ code = f"def {funcname}(self, name, value):\n{body}"
547
+
548
+ return GeneratedCode(code, globs)
549
+
550
+
551
+ def frozen_delattr_generator(cls, funcname="__delattr__"):
552
+ body = (
553
+ ' raise TypeError(\n'
554
+ ' f"{type(self).__name__!r} object "\n'
555
+ ' f"does not support attribute deletion"\n'
556
+ ' )\n'
557
+ )
558
+ code = f"def {funcname}(self, name):\n{body}"
559
+ globs = {}
560
+ return GeneratedCode(code, globs)
561
+
562
+
563
+ # As only the __get__ method refers to the class we can use the same
564
+ # Descriptor instances for every class.
565
+ init_maker = MethodMaker("__init__", init_generator)
566
+ repr_maker = MethodMaker("__repr__", repr_generator)
567
+ eq_maker = MethodMaker("__eq__", eq_generator)
568
+ replace_maker = MethodMaker("__replace__", replace_generator)
569
+ frozen_setattr_maker = MethodMaker("__setattr__", frozen_setattr_generator)
570
+ frozen_delattr_maker = MethodMaker("__delattr__", frozen_delattr_generator)
571
+ default_methods = frozenset({init_maker, repr_maker, eq_maker})
572
+
573
+ # Special `__init__` maker for 'Field' subclasses - needs its own NOTHING option
574
+ _field_init_maker = MethodMaker(
575
+ funcname="__init__",
576
+ code_generator=get_init_generator(
577
+ null=FIELD_NOTHING,
578
+ extra_code=["self.validate_field()"],
579
+ )
580
+ )
581
+
582
+
583
+ def builder(cls=None, /, *, gatherer, methods, flags=None, fix_signature=True):
584
+ """
585
+ The main builder for class generation
586
+
587
+ If the GATHERED_DATA attribute exists on the class it will be used instead of
588
+ the provided gatherer.
589
+
590
+ :param cls: Class to be analysed and have methods generated
591
+ :param gatherer: Function to gather field information
592
+ :type gatherer: Callable[[type], tuple[dict[str, Field], dict[str, Any]]]
593
+ :param methods: MethodMakers to add to the class
594
+ :type methods: set[MethodMaker]
595
+ :param flags: additional flags to store in the internals dictionary
596
+ for use by method generators.
597
+ :type flags: None | dict[str, bool]
598
+ :param fix_signature: Add a __signature__ attribute to work-around an issue with
599
+ inspect.signature incorrectly handling __init__ descriptors.
600
+ :type fix_signature: bool
601
+ :return: The modified class (the class itself is modified, but this is expected).
602
+ """
603
+ # Handle `None` to make wrapping with a decorator easier.
604
+ if cls is None:
605
+ return lambda cls_: builder(
606
+ cls_,
607
+ gatherer=gatherer,
608
+ methods=methods,
609
+ flags=flags,
610
+ fix_signature=fix_signature,
611
+ )
612
+
613
+ # Get from the class dict to avoid getting an inherited internals dict
614
+ internals = cls.__dict__.get(INTERNALS_DICT, {})
615
+ setattr(cls, INTERNALS_DICT, internals)
616
+
617
+ # Update or add flags to internals dict
618
+ flag_dict = internals.get("flags", {})
619
+ if flags is not None:
620
+ flag_dict |= flags
621
+ internals["flags"] = flag_dict
622
+
623
+ cls_gathered = cls.__dict__.get(GATHERED_DATA)
624
+
625
+ if cls_gathered:
626
+ cls_fields, modifications = cls_gathered
627
+ else:
628
+ cls_fields, modifications = gatherer(cls)
629
+
630
+ for name, value in modifications.items():
631
+ if value is NOTHING:
632
+ delattr(cls, name)
633
+ else:
634
+ setattr(cls, name, value)
635
+
636
+ internals["local_fields"] = cls_fields
637
+
638
+ mro = cls.__mro__[:-1] # skip 'object' base class
639
+ if mro == (cls,): # special case of no inheritance.
640
+ fields = cls_fields.copy()
641
+ else:
642
+ fields = {}
643
+ for c in reversed(mro):
644
+ try:
645
+ fields |= get_fields(c, local=True)
646
+ except (TypeError, KeyError):
647
+ pass
648
+
649
+ internals["fields"] = fields
650
+
651
+ # Assign all of the method generators
652
+ internal_methods = {}
653
+ for method in methods:
654
+ setattr(cls, method.funcname, method)
655
+ internal_methods[method.funcname] = method
656
+
657
+ if "__eq__" in internal_methods and "__hash__" not in internal_methods:
658
+ # If an eq method has been defined and a hash method has not
659
+ # Then the class is not frozen unless the user has
660
+ # defined a hash method
661
+ if "__hash__" not in cls.__dict__:
662
+ setattr(cls, "__hash__", None)
663
+
664
+ internals["methods"] = _MappingProxyType(internal_methods)
665
+
666
+ # Fix for inspect.signature(cls)
667
+ if fix_signature:
668
+ setattr(cls, "__signature__", signature_maker)
669
+
670
+ # Add attribute indicating build completed
671
+ internals["build_complete"] = True
672
+
673
+ return cls
674
+
675
+
676
+ # Slot gathering tools
677
+ # Subclass of dict to be identifiable by isinstance checks
678
+ # For anything more complicated this could be made into a Mapping
679
+ class SlotFields(dict):
680
+ """
681
+ A plain dict subclass.
682
+
683
+ For declaring slotfields there are no additional features required
684
+ other than recognising that this is intended to be used as a class
685
+ generating dict and isn't a regular dictionary that ended up in
686
+ `__slots__`.
687
+
688
+ This should be replaced on `__slots__` after fields have been gathered.
689
+ """
690
+ def __repr__(self):
691
+ return f"SlotFields({super().__repr__()})"
692
+
693
+
694
+ # Tool to convert annotations to slots as a metaclass
695
+ class SlotMakerMeta(type):
696
+ """
697
+ Metaclass to convert annotations or Field(...) attributes to slots.
698
+
699
+ Will not convert `ClassVar` hinted values.
700
+ """
701
+ def __new__(
702
+ cls,
703
+ name,
704
+ bases,
705
+ ns,
706
+ slots=True,
707
+ gatherer=None,
708
+ ignore_annotations=None,
709
+ **kwargs
710
+ ):
711
+ # Slot makers should inherit flags
712
+ for base in bases:
713
+ try:
714
+ flags = getattr(base, INTERNALS_DICT)["flags"].copy()
715
+ except (AttributeError, KeyError):
716
+ pass
717
+ else:
718
+ break
719
+ else:
720
+ flags = {"ignore_annotations": False}
721
+
722
+ # Set up flags as these may be needed early
723
+ if ignore_annotations is not None:
724
+ flags["ignore_annotations"] = ignore_annotations
725
+
726
+ # Assign flags to internals
727
+ ns[INTERNALS_DICT] = {"flags": flags}
728
+
729
+ # This should only run if slots=True is declared
730
+ # and __slots__ have not already been defined
731
+ if slots and "__slots__" not in ns:
732
+ # Check if a different gatherer has been set in any base classes
733
+ # Default to unified gatherer
734
+ if gatherer is None:
735
+ gatherer = ns.get(META_GATHERER_NAME, None)
736
+ if not gatherer:
737
+ for base in bases:
738
+ if g := getattr(base, META_GATHERER_NAME, None):
739
+ gatherer = g
740
+ break
741
+
742
+ if not gatherer:
743
+ gatherer = unified_gatherer
744
+
745
+ # Set the gatherer in the namespace
746
+ ns[META_GATHERER_NAME] = gatherer
747
+
748
+ # Obtain slots from annotations or attributes
749
+ cls_fields, cls_modifications = gatherer(ns)
750
+ for k, v in cls_modifications.items():
751
+ if v is NOTHING:
752
+ ns.pop(k)
753
+ else:
754
+ ns[k] = v
755
+
756
+ slot_values = {}
757
+ fields = {}
758
+
759
+ for k, v in cls_fields.items():
760
+ slot_values[k] = v.doc
761
+ if k not in {"__weakref__", "__dict__"}:
762
+ fields[k] = v
763
+
764
+ # Special case cached_property
765
+ # if a cached property is used add a slot for __dict__
766
+ if (
767
+ (functools := sys.modules.get("functools"))
768
+ and "__dict__" not in slot_values
769
+ ):
770
+ # Check __dict__ isn't already in a parent class
771
+ for base in bases:
772
+ if "__dict__" in base.__dict__:
773
+ break
774
+ else:
775
+ for v in ns.values():
776
+ if isinstance(v, functools.cached_property):
777
+ slot_values["__dict__"] = None
778
+ break
779
+
780
+ # Place slots *after* everything else to be safe
781
+ ns["__slots__"] = slot_values
782
+
783
+ # Place pre-gathered field data - modifications are already applied
784
+ modifications = {}
785
+ ns[GATHERED_DATA] = fields, modifications
786
+
787
+ else:
788
+ if gatherer is not None:
789
+ ns[META_GATHERER_NAME] = gatherer
790
+
791
+ new_cls = super().__new__(cls, name, bases, ns, **kwargs)
792
+
793
+ return new_cls
794
+
795
+
796
+ # This class is set up before fields as it will be used to generate the Fields
797
+ # for Field itself so Field can have generated __eq__, __repr__ and other methods
798
+ class GatheredFields:
799
+ """
800
+ Helper class to store gathered field data
801
+ """
802
+ __slots__ = ("fields", "modifications")
803
+
804
+ def __init__(self, fields, modifications):
805
+ self.fields = fields
806
+ self.modifications = modifications
807
+
808
+ def __eq__(self, other):
809
+ if type(self) is type(other):
810
+ return self.fields == other.fields and self.modifications == other.modifications
811
+
812
+ def __repr__(self):
813
+ return f"{type(self).__name__}(fields={self.fields!r}, modifications={self.modifications!r})"
814
+
815
+ def __call__(self, cls_dict):
816
+ # cls_dict will be provided, but isn't needed
817
+ return self.fields, self.modifications
818
+
819
+
820
+ # The Field class can finally be defined.
821
+ # The __init__ method has to be written manually so Fields can be created
822
+ # However after this, the other methods can be generated.
823
+ class Field(metaclass=SlotMakerMeta):
824
+ """
825
+ A basic class to handle the assignment of defaults/factories with
826
+ some metadata.
827
+
828
+ Intended to be extendable by subclasses for additional features.
829
+
830
+ Note: When run under `pytest`, Field instances are Frozen.
831
+
832
+ When subclassing, passing `frozen=True` will make your subclass frozen.
833
+
834
+ :param default: Standard default value to be used for attributes with this field.
835
+ :param default_factory: A zero-argument function to be called to generate a
836
+ default value, useful for mutable obects like lists.
837
+ :param type: The type of the attribute to be assigned by this field.
838
+ :param doc: The documentation for the attribute that appears when calling
839
+ help(...) on the class. (Only in slotted classes).
840
+ :param init: Include in the class __init__ parameters.
841
+ :param repr: Include in the class __repr__.
842
+ :param compare: Include in the class __eq__.
843
+ :param kw_only: Make this a keyword only parameter in __init__.
844
+ """
845
+
846
+ # Plain slots are required as part of bootstrapping
847
+ # This prevents SlotMakerMeta from trying to generate 'Field's
848
+ __slots__ = (
849
+ "default",
850
+ "default_factory",
851
+ "type",
852
+ "doc",
853
+ "init",
854
+ "repr",
855
+ "compare",
856
+ "kw_only",
857
+ )
858
+
859
+ # noinspection PyShadowingBuiltins
860
+ def __init__(
861
+ self,
862
+ *,
863
+ default=NOTHING,
864
+ default_factory=NOTHING,
865
+ type=NOTHING,
866
+ doc=None,
867
+ init=True,
868
+ repr=True,
869
+ compare=True,
870
+ kw_only=False,
871
+ ):
872
+ # The init function for 'Field' cannot be generated
873
+ # as 'Field' needs to exist first.
874
+ # repr and comparison functions are generated as these
875
+ # do not need to exist to create initial Fields.
876
+
877
+ self.default = default
878
+ self.default_factory = default_factory
879
+ self.type = type
880
+ self.doc = doc
881
+
882
+ self.init = init
883
+ self.repr = repr
884
+ self.compare = compare
885
+ self.kw_only = kw_only
886
+
887
+ self.validate_field()
888
+
889
+ def __init_subclass__(cls, frozen=False, ignore_annotations=False):
890
+ # Subclasses of Field can be created as if they are dataclasses
891
+ field_methods = {_field_init_maker, repr_maker, eq_maker}
892
+ if frozen or _UNDER_TESTING:
893
+ field_methods |= {frozen_setattr_maker, frozen_delattr_maker}
894
+
895
+ builder(
896
+ cls,
897
+ gatherer=unified_gatherer,
898
+ methods=field_methods,
899
+ flags={
900
+ "slotted": True,
901
+ "kw_only": True,
902
+ "frozen": frozen or _UNDER_TESTING,
903
+ "ignore_annotations": ignore_annotations,
904
+ }
905
+ )
906
+
907
+ def validate_field(self):
908
+ cls_name = self.__class__.__name__
909
+ if type(self.default) is not _NothingType and type(self.default_factory) is not _NothingType:
910
+ raise AttributeError(
911
+ f"{cls_name} cannot define both a default value and a default factory."
912
+ )
913
+
914
+ @classmethod
915
+ def from_field(cls, fld, /, **kwargs):
916
+ """
917
+ Create an instance of field or subclass from another field.
918
+
919
+ This is intended to be used to convert a base
920
+ Field into a subclass.
921
+
922
+ :param fld: field class to convert
923
+ :param kwargs: Additional keyword arguments for subclasses
924
+ :return: new field subclass instance
925
+ """
926
+ argument_dict = {**_get_inst_fields(fld), **kwargs}
927
+
928
+ return cls(**argument_dict)
929
+
930
+
931
+ def _build_field():
932
+ # Complete the construction of the Field class
933
+ field_docs = {
934
+ "default": "Standard default value to be used for attributes with this field.",
935
+ "default_factory":
936
+ "A zero-argument function to be called to generate a default value, "
937
+ "useful for mutable obects like lists.",
938
+ "type": "The type of the attribute to be assigned by this field.",
939
+ "doc":
940
+ "The documentation for the attribute that appears when calling "
941
+ "help(...) on the class. (Only in slotted classes).",
942
+ "init": "Include this attribute in the class __init__ parameters.",
943
+ "repr": "Include this attribute in the class __repr__",
944
+ "compare": "Include this attribute in the class __eq__ method",
945
+ "kw_only": "Make this a keyword only parameter in __init__",
946
+ }
947
+
948
+ fields = {
949
+ "default": Field(default=NOTHING, doc=field_docs["default"]),
950
+ "default_factory": Field(default=NOTHING, doc=field_docs["default_factory"]),
951
+ "type": Field(default=NOTHING, doc=field_docs["type"]),
952
+ "doc": Field(default=None, doc=field_docs["doc"]),
953
+ "init": Field(default=True, doc=field_docs["init"]),
954
+ "repr": Field(default=True, doc=field_docs["repr"]),
955
+ "compare": Field(default=True, doc=field_docs["compare"]),
956
+ "kw_only": Field(default=False, doc=field_docs["kw_only"]),
957
+ }
958
+ modifications = {"__slots__": field_docs}
959
+
960
+ field_methods = {repr_maker, eq_maker}
961
+ if _UNDER_TESTING:
962
+ field_methods |= {frozen_setattr_maker, frozen_delattr_maker}
963
+
964
+ builder(
965
+ Field,
966
+ gatherer=GatheredFields(fields, modifications),
967
+ methods=field_methods,
968
+ flags={"slotted": True, "kw_only": True},
969
+ )
970
+
971
+
972
+ _build_field()
973
+ del _build_field
974
+
975
+
976
+ def make_slot_gatherer(field_type=Field):
977
+ """
978
+ Create a new annotation gatherer that will work with `Field` instances
979
+ of the creators definition.
980
+
981
+ :param field_type: The `Field` classes to be used when gathering fields
982
+ :return: A slot gatherer that will check for and generate Fields of
983
+ the type field_type.
984
+ """
985
+ def field_slot_gatherer(cls_or_ns):
986
+ """
987
+ Gather field information for class generation based on __slots__
988
+
989
+ :param cls_or_ns: Class to gather field information from (or class namespace)
990
+ :return: dict of field_name: Field(...) and modifications to be performed by the builder
991
+ """
992
+ if isinstance(cls_or_ns, (_MappingProxyType, dict)):
993
+ cls_dict = cls_or_ns
994
+ else:
995
+ cls_dict = cls_or_ns.__dict__
996
+
997
+ try:
998
+ cls_slots = cls_dict["__slots__"]
999
+ except KeyError:
1000
+ raise AttributeError(
1001
+ "__slots__ must be defined as an instance of SlotFields "
1002
+ "in order to generate a slotclass"
1003
+ )
1004
+
1005
+ if not isinstance(cls_slots, SlotFields):
1006
+ raise TypeError(
1007
+ "__slots__ must be an instance of SlotFields "
1008
+ "in order to generate a slotclass"
1009
+ )
1010
+
1011
+ cls_fields = {}
1012
+ slot_replacement = {}
1013
+
1014
+ for k, v in cls_slots.items():
1015
+ # Special case __dict__ and __weakref__
1016
+ # They should be included in the final `__slots__`
1017
+ # But ignored as a value.
1018
+ if k in {"__dict__", "__weakref__"}:
1019
+ slot_replacement[k] = None
1020
+ continue
1021
+
1022
+ if isinstance(v, field_type):
1023
+ attrib = v
1024
+ else:
1025
+ # Plain values treated as defaults
1026
+ attrib = field_type(default=v)
1027
+
1028
+ slot_replacement[k] = attrib.doc
1029
+ cls_fields[k] = attrib
1030
+
1031
+ # Send the modifications to the builder for what should be changed
1032
+ # On the class.
1033
+ # In this case, slots with documentation and new annotations.
1034
+ modifications = {
1035
+ "__slots__": slot_replacement,
1036
+ }
1037
+
1038
+ return cls_fields, modifications
1039
+
1040
+ return field_slot_gatherer
1041
+
1042
+
1043
+ def make_annotation_gatherer(
1044
+ field_type=Field,
1045
+ leave_default_values=False,
1046
+ ):
1047
+ """
1048
+ Create a new annotation gatherer that will work with `Field` instances
1049
+ of the creators definition.
1050
+
1051
+ :param field_type: The `Field` classes to be used when gathering fields
1052
+ :param leave_default_values: Set to True if the gatherer should leave
1053
+ default values in place as class variables.
1054
+ :return: An annotation gatherer with these settings.
1055
+ """
1056
+ def field_annotation_gatherer(cls_or_ns, *, cls_annotations=None):
1057
+ # cls_annotations are included as the unified gatherer may already have
1058
+ # obtained the annotations, this prevents the method being called twice
1059
+
1060
+ if isinstance(cls_or_ns, (_MappingProxyType, dict)):
1061
+ cls = None
1062
+ cls_dict = cls_or_ns
1063
+ else:
1064
+ cls = cls_or_ns
1065
+ cls_dict = cls_or_ns.__dict__
1066
+
1067
+ # This should really be dict[str, field_type] but static analysis
1068
+ # doesn't understand this.
1069
+ cls_fields: dict[str, Field] = {}
1070
+ modifications = {}
1071
+
1072
+ if cls_annotations is None:
1073
+ cls_annotations = get_ns_annotations(cls_dict, cls=cls)
1074
+
1075
+ kw_flag = False
1076
+
1077
+ for k, v in cls_annotations.items():
1078
+ # Ignore ClassVar
1079
+ if is_classvar(v):
1080
+ continue
1081
+
1082
+ if v is KW_ONLY or (isinstance(v, str) and "KW_ONLY" in v):
1083
+ if kw_flag:
1084
+ raise SyntaxError("KW_ONLY sentinel may only appear once.")
1085
+ kw_flag = True
1086
+ continue
1087
+
1088
+ attrib = cls_dict.get(k, NOTHING)
1089
+
1090
+ if attrib is not NOTHING:
1091
+ if isinstance(attrib, field_type):
1092
+ kw_only = attrib.kw_only or kw_flag
1093
+
1094
+ attrib = field_type.from_field(attrib, type=v, kw_only=kw_only)
1095
+
1096
+ if attrib.default is not NOTHING and leave_default_values:
1097
+ modifications[k] = attrib.default
1098
+ else:
1099
+ # NOTHING sentinel indicates a value should be removed
1100
+ modifications[k] = NOTHING
1101
+ elif not isinstance(attrib, _MemberDescriptorType):
1102
+ attrib = field_type(default=attrib, type=v, kw_only=kw_flag)
1103
+ if not leave_default_values:
1104
+ modifications[k] = NOTHING
1105
+ else:
1106
+ attrib = field_type(type=v, kw_only=kw_flag)
1107
+ else:
1108
+ attrib = field_type(type=v, kw_only=kw_flag)
1109
+
1110
+ cls_fields[k] = attrib
1111
+
1112
+ return cls_fields, modifications
1113
+
1114
+ return field_annotation_gatherer
1115
+
1116
+
1117
+ def make_field_gatherer(
1118
+ field_type=Field,
1119
+ leave_default_values=False,
1120
+ ):
1121
+ def field_attribute_gatherer(cls_or_ns):
1122
+ if isinstance(cls_or_ns, (_MappingProxyType, dict)):
1123
+ cls_dict = cls_or_ns
1124
+ else:
1125
+ cls_dict = cls_or_ns.__dict__
1126
+
1127
+ cls_attributes = {
1128
+ k: v
1129
+ for k, v in cls_dict.items()
1130
+ if isinstance(v, field_type)
1131
+ }
1132
+
1133
+ cls_modifications = {}
1134
+
1135
+ for name in cls_attributes.keys():
1136
+ attrib = cls_attributes[name]
1137
+ if leave_default_values:
1138
+ cls_modifications[name] = attrib.default
1139
+ else:
1140
+ cls_modifications[name] = NOTHING
1141
+
1142
+ return cls_attributes, cls_modifications
1143
+ return field_attribute_gatherer
1144
+
1145
+
1146
+ def make_unified_gatherer(
1147
+ field_type=Field,
1148
+ leave_default_values=False,
1149
+ ):
1150
+ """
1151
+ Create a gatherer that will work via first slots, then
1152
+ Field(...) class attributes and finally annotations if
1153
+ no unannotated Field(...) attributes are present.
1154
+
1155
+ :param field_type: The field class to use for gathering
1156
+ :param leave_default_values: leave default values in place
1157
+ :return: gatherer function
1158
+ """
1159
+ slot_g = make_slot_gatherer(field_type)
1160
+ anno_g = make_annotation_gatherer(field_type, leave_default_values)
1161
+ attrib_g = make_field_gatherer(field_type, leave_default_values)
1162
+
1163
+ def field_unified_gatherer(cls_or_ns):
1164
+ if isinstance(cls_or_ns, (_MappingProxyType, dict)):
1165
+ cls_dict = cls_or_ns
1166
+ cls = None
1167
+ else:
1168
+ cls_dict = cls_or_ns.__dict__
1169
+ cls = cls_or_ns
1170
+
1171
+ cls_slots = cls_dict.get("__slots__")
1172
+
1173
+ if isinstance(cls_slots, SlotFields):
1174
+ return slot_g(cls_dict)
1175
+
1176
+ # Get ignore_annotations flag
1177
+ ignore_annotations = cls_dict.get(INTERNALS_DICT, {}).get("flags", {}).get("ignore_annotations", False)
1178
+
1179
+ if ignore_annotations:
1180
+ return attrib_g(cls_dict)
1181
+ else:
1182
+ # To choose between annotation and attribute gatherers
1183
+ # compare sets of names.
1184
+ cls_annotations = get_ns_annotations(cls_dict, cls=cls)
1185
+ cls_attributes = {
1186
+ k: v for k, v in cls_dict.items() if isinstance(v, field_type)
1187
+ }
1188
+
1189
+ cls_annotation_names = cls_annotations.keys()
1190
+ cls_attribute_names = cls_attributes.keys()
1191
+
1192
+ if set(cls_annotation_names).issuperset(set(cls_attribute_names)):
1193
+ # All `Field` values have annotations, so use annotation gatherer
1194
+ # Pass the original cls_or_ns object along with the already gathered annotations
1195
+
1196
+ return anno_g(cls_or_ns, cls_annotations=cls_annotations)
1197
+
1198
+ return attrib_g(cls_dict)
1199
+
1200
+ return field_unified_gatherer
1201
+
1202
+
1203
+ slot_gatherer = make_slot_gatherer()
1204
+ annotation_gatherer = make_annotation_gatherer()
1205
+
1206
+ # The unified gatherer used for slot classes must remove default
1207
+ # values for slots to work correctly.
1208
+ unified_gatherer = make_unified_gatherer()
1209
+
1210
+
1211
+ def check_argument_order(cls):
1212
+ """
1213
+ Raise a SyntaxError if the argument order will be invalid for a generated
1214
+ `__init__` function.
1215
+
1216
+ :param cls: class being built
1217
+ """
1218
+ fields = get_fields(cls)
1219
+ used_default = False
1220
+ for k, v in fields.items():
1221
+ if v.kw_only or (not v.init):
1222
+ continue
1223
+
1224
+ if v.default is NOTHING and v.default_factory is NOTHING:
1225
+ if used_default:
1226
+ raise SyntaxError(
1227
+ f"non-default argument {k!r} follows default argument"
1228
+ )
1229
+ else:
1230
+ used_default = True
1231
+
1232
+
1233
+ # Class Decorators
1234
+ def slotclass(cls=None, /, *, methods=default_methods, syntax_check=True):
1235
+ """
1236
+ Example of class builder in action using __slots__ to find fields.
1237
+
1238
+ :param cls: Class to be analysed and modified
1239
+ :param methods: MethodMakers to be added to the class
1240
+ :param syntax_check: check there are no arguments without defaults
1241
+ after arguments with defaults.
1242
+ :return: Modified class
1243
+ """
1244
+ if not cls:
1245
+ return lambda cls_: slotclass(cls_, methods=methods, syntax_check=syntax_check)
1246
+
1247
+ cls = builder(cls, gatherer=slot_gatherer, methods=methods, flags={"slotted": True})
1248
+
1249
+ if syntax_check:
1250
+ check_argument_order(cls)
1251
+
1252
+ return cls