omlish 0.0.0.dev284__py3-none-any.whl → 0.0.0.dev285__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.
- omlish/__about__.py +2 -2
- omlish/dataclasses/__init__.py +58 -60
- omlish/dataclasses/api/__init__.py +25 -0
- omlish/dataclasses/api/classes/__init__.py +0 -0
- omlish/dataclasses/api/classes/conversion.py +30 -0
- omlish/dataclasses/api/classes/decorator.py +145 -0
- omlish/dataclasses/api/classes/make.py +109 -0
- omlish/dataclasses/api/classes/metadata.py +133 -0
- omlish/dataclasses/api/classes/params.py +78 -0
- omlish/dataclasses/api/fields/__init__.py +0 -0
- omlish/dataclasses/api/fields/building.py +120 -0
- omlish/dataclasses/api/fields/constructor.py +56 -0
- omlish/dataclasses/api/fields/conversion.py +191 -0
- omlish/dataclasses/api/fields/metadata.py +94 -0
- omlish/dataclasses/concerns/__init__.py +17 -0
- omlish/dataclasses/concerns/abc.py +15 -0
- omlish/dataclasses/concerns/copy.py +63 -0
- omlish/dataclasses/concerns/doc.py +53 -0
- omlish/dataclasses/concerns/eq.py +60 -0
- omlish/dataclasses/concerns/fields.py +119 -0
- omlish/dataclasses/concerns/frozen.py +133 -0
- omlish/dataclasses/concerns/hash.py +165 -0
- omlish/dataclasses/concerns/init.py +453 -0
- omlish/dataclasses/concerns/matchargs.py +27 -0
- omlish/dataclasses/concerns/mro.py +16 -0
- omlish/dataclasses/concerns/order.py +87 -0
- omlish/dataclasses/concerns/override.py +98 -0
- omlish/dataclasses/concerns/params.py +14 -0
- omlish/dataclasses/concerns/replace.py +48 -0
- omlish/dataclasses/concerns/repr.py +95 -0
- omlish/dataclasses/{impl → concerns}/slots.py +25 -1
- omlish/dataclasses/debug.py +2 -0
- omlish/dataclasses/errors.py +115 -0
- omlish/dataclasses/generation/__init__.py +0 -0
- omlish/dataclasses/generation/base.py +38 -0
- omlish/dataclasses/generation/compilation.py +258 -0
- omlish/dataclasses/generation/execution.py +195 -0
- omlish/dataclasses/generation/globals.py +83 -0
- omlish/dataclasses/generation/idents.py +6 -0
- omlish/dataclasses/generation/mangling.py +18 -0
- omlish/dataclasses/generation/manifests.py +20 -0
- omlish/dataclasses/generation/ops.py +97 -0
- omlish/dataclasses/generation/plans.py +35 -0
- omlish/dataclasses/generation/processor.py +174 -0
- omlish/dataclasses/generation/registry.py +42 -0
- omlish/dataclasses/generation/utils.py +83 -0
- omlish/dataclasses/{impl/reflect.py → inspect.py} +53 -90
- omlish/dataclasses/{impl/internals.py → internals.py} +26 -32
- omlish/dataclasses/metaclass/__init__.py +0 -0
- omlish/dataclasses/metaclass/bases.py +69 -0
- omlish/dataclasses/metaclass/confer.py +65 -0
- omlish/dataclasses/metaclass/meta.py +115 -0
- omlish/dataclasses/metaclass/specs.py +38 -0
- omlish/dataclasses/processing/__init__.py +0 -0
- omlish/dataclasses/processing/base.py +83 -0
- omlish/dataclasses/processing/driving.py +45 -0
- omlish/dataclasses/processing/priority.py +13 -0
- omlish/dataclasses/processing/registry.py +81 -0
- omlish/dataclasses/reflection.py +81 -0
- omlish/dataclasses/specs.py +224 -0
- omlish/dataclasses/tools/__init__.py +0 -0
- omlish/dataclasses/{impl → tools}/as_.py +23 -8
- omlish/dataclasses/tools/iter.py +27 -0
- omlish/dataclasses/tools/modifiers.py +52 -0
- omlish/dataclasses/tools/replace.py +17 -0
- omlish/dataclasses/tools/repr.py +12 -0
- omlish/dataclasses/{static.py → tools/static.py} +25 -4
- omlish/dataclasses/utils.py +54 -109
- omlish/diag/__init__.py +4 -4
- omlish/inject/impl/origins.py +1 -1
- omlish/lang/cached/function.py +4 -2
- omlish/marshal/objects/dataclasses.py +3 -7
- omlish/marshal/objects/helpers.py +3 -3
- omlish/secrets/marshal.py +1 -1
- omlish/secrets/secrets.py +1 -1
- omlish/sql/queries/base.py +1 -1
- omlish/typedvalues/marshal.py +2 -2
- {omlish-0.0.0.dev284.dist-info → omlish-0.0.0.dev285.dist-info}/METADATA +1 -1
- {omlish-0.0.0.dev284.dist-info → omlish-0.0.0.dev285.dist-info}/RECORD +83 -43
- omlish/dataclasses/impl/LICENSE +0 -279
- omlish/dataclasses/impl/__init__.py +0 -33
- omlish/dataclasses/impl/api.py +0 -278
- omlish/dataclasses/impl/copy.py +0 -30
- omlish/dataclasses/impl/errors.py +0 -53
- omlish/dataclasses/impl/fields.py +0 -245
- omlish/dataclasses/impl/frozen.py +0 -93
- omlish/dataclasses/impl/hashing.py +0 -86
- omlish/dataclasses/impl/init.py +0 -199
- omlish/dataclasses/impl/main.py +0 -93
- omlish/dataclasses/impl/metaclass.py +0 -235
- omlish/dataclasses/impl/metadata.py +0 -75
- omlish/dataclasses/impl/order.py +0 -57
- omlish/dataclasses/impl/overrides.py +0 -53
- omlish/dataclasses/impl/params.py +0 -128
- omlish/dataclasses/impl/processing.py +0 -24
- omlish/dataclasses/impl/replace.py +0 -40
- omlish/dataclasses/impl/repr.py +0 -66
- omlish/dataclasses/impl/simple.py +0 -50
- omlish/dataclasses/impl/utils.py +0 -167
- {omlish-0.0.0.dev284.dist-info → omlish-0.0.0.dev285.dist-info}/WHEEL +0 -0
- {omlish-0.0.0.dev284.dist-info → omlish-0.0.0.dev285.dist-info}/entry_points.txt +0 -0
- {omlish-0.0.0.dev284.dist-info → omlish-0.0.0.dev285.dist-info}/licenses/LICENSE +0 -0
- {omlish-0.0.0.dev284.dist-info → omlish-0.0.0.dev285.dist-info}/top_level.txt +0 -0
omlish/dataclasses/impl/repr.py
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
import dataclasses as dc
|
2
|
-
import reprlib
|
3
|
-
import typing as ta
|
4
|
-
|
5
|
-
from .params import get_field_extras
|
6
|
-
from .processing import Processor
|
7
|
-
from .utils import Namespace
|
8
|
-
from .utils import create_fn
|
9
|
-
from .utils import set_new_attribute
|
10
|
-
|
11
|
-
|
12
|
-
def repr_fn(
|
13
|
-
fields: ta.Sequence[dc.Field],
|
14
|
-
globals: Namespace, # noqa
|
15
|
-
*,
|
16
|
-
repr_id: bool = False,
|
17
|
-
) -> ta.Callable:
|
18
|
-
locals: dict[str, ta.Any] = {} # noqa
|
19
|
-
|
20
|
-
fields = sorted(fields, key=lambda f: get_field_extras(f).repr_priority or 0)
|
21
|
-
|
22
|
-
prefix_src = '{self.__class__.__qualname__}'
|
23
|
-
if repr_id:
|
24
|
-
prefix_src += '@{hex(id(self))[2:]}'
|
25
|
-
|
26
|
-
if any(get_field_extras(f).repr_fn is not None for f in fields):
|
27
|
-
lst: list[str] = []
|
28
|
-
for f in fields:
|
29
|
-
if (fex := get_field_extras(f)).repr_fn is not None:
|
30
|
-
locals[fn_name := f'__repr_fn__{f.name}'] = fex.repr_fn
|
31
|
-
lst.append(f"if (r := {fn_name}(self.{f.name})) is not None: l.append(f'{f.name}={{r}}')")
|
32
|
-
else:
|
33
|
-
lst.append(f"l.append(f'{f.name}={{self.{f.name}!r}}')")
|
34
|
-
|
35
|
-
src = [
|
36
|
-
'l = []',
|
37
|
-
*lst,
|
38
|
-
f'return f"{prefix_src}({{", ".join(l)}})"',
|
39
|
-
]
|
40
|
-
|
41
|
-
else:
|
42
|
-
src = [
|
43
|
-
f'return f"{prefix_src}(' +
|
44
|
-
', '.join([f'{f.name}={{self.{f.name}!r}}' for f in fields]) +
|
45
|
-
')"',
|
46
|
-
]
|
47
|
-
|
48
|
-
fn = create_fn(
|
49
|
-
'__repr__',
|
50
|
-
('self',),
|
51
|
-
src,
|
52
|
-
globals=globals,
|
53
|
-
locals=locals,
|
54
|
-
)
|
55
|
-
|
56
|
-
return reprlib.recursive_repr()(fn)
|
57
|
-
|
58
|
-
|
59
|
-
class ReprProcessor(Processor):
|
60
|
-
def _process(self) -> None:
|
61
|
-
if not self._info.params.repr:
|
62
|
-
return
|
63
|
-
|
64
|
-
flds = [f for f in self._info.instance_fields if f.repr]
|
65
|
-
rfn = repr_fn(flds, self._info.globals, repr_id=self._info.params_extras.repr_id)
|
66
|
-
set_new_attribute(self._cls, '__repr__', rfn) # noqa
|
@@ -1,50 +0,0 @@
|
|
1
|
-
import inspect
|
2
|
-
|
3
|
-
from .init import get_init_fields
|
4
|
-
from .processing import Processor
|
5
|
-
from .utils import create_fn
|
6
|
-
from .utils import set_new_attribute
|
7
|
-
|
8
|
-
|
9
|
-
class EqProcessor(Processor):
|
10
|
-
def _process(self) -> None:
|
11
|
-
if not self._info.params.eq:
|
12
|
-
return
|
13
|
-
|
14
|
-
# flds = [f for f in self._info.instance_fields if f.compare]
|
15
|
-
# self_tuple = tuple_str('self', flds)
|
16
|
-
# other_tuple = tuple_str('other', flds)
|
17
|
-
# set_new_attribute(cls, '__eq__', _cmp_fn('__eq__', '==', self_tuple, other_tuple, globals=globals))
|
18
|
-
cmp_fields = [field for field in self._info.instance_fields if field.compare]
|
19
|
-
terms = [f'self.{field.name} == other.{field.name}' for field in cmp_fields]
|
20
|
-
field_comparisons = ' and '.join(terms) or 'True'
|
21
|
-
body = [
|
22
|
-
f'if self is other:',
|
23
|
-
f' return True',
|
24
|
-
f'if other.__class__ is self.__class__:',
|
25
|
-
f' return {field_comparisons}',
|
26
|
-
f'return NotImplemented',
|
27
|
-
]
|
28
|
-
func = create_fn('__eq__', ('self', 'other'), body, globals=self._info.globals)
|
29
|
-
set_new_attribute(self._cls, '__eq__', func)
|
30
|
-
|
31
|
-
|
32
|
-
class DocProcessor(Processor):
|
33
|
-
def _process(self) -> None:
|
34
|
-
if getattr(self._cls, '__doc__'):
|
35
|
-
return
|
36
|
-
|
37
|
-
try:
|
38
|
-
text_sig = str(inspect.signature(self._cls)).replace(' -> None', '')
|
39
|
-
except (TypeError, ValueError):
|
40
|
-
text_sig = ''
|
41
|
-
self._cls.__doc__ = (self._cls.__name__ + text_sig)
|
42
|
-
|
43
|
-
|
44
|
-
class MatchArgsProcessor(Processor):
|
45
|
-
def _process(self) -> None:
|
46
|
-
if not self._info.params.match_args:
|
47
|
-
return
|
48
|
-
|
49
|
-
ifs = get_init_fields(self._info.fields.values())
|
50
|
-
set_new_attribute(self._cls, '__match_args__', tuple(f.name for f in ifs.std))
|
omlish/dataclasses/impl/utils.py
DELETED
@@ -1,167 +0,0 @@
|
|
1
|
-
import dataclasses as dc
|
2
|
-
import textwrap
|
3
|
-
import types
|
4
|
-
import typing as ta
|
5
|
-
|
6
|
-
from ... import check
|
7
|
-
from ... import lang
|
8
|
-
|
9
|
-
|
10
|
-
T = ta.TypeVar('T')
|
11
|
-
Namespace: ta.TypeAlias = ta.MutableMapping[str, ta.Any]
|
12
|
-
|
13
|
-
|
14
|
-
def create_fn(
|
15
|
-
name: str,
|
16
|
-
args: ta.Sequence[str],
|
17
|
-
body: ta.Sequence[str],
|
18
|
-
*,
|
19
|
-
globals: Namespace | None = None, # noqa
|
20
|
-
locals: Namespace | None = None, # noqa
|
21
|
-
return_type: lang.Maybe[ta.Any] = lang.empty(),
|
22
|
-
) -> ta.Callable:
|
23
|
-
check.not_isinstance(args, str)
|
24
|
-
check.not_isinstance(body, str)
|
25
|
-
|
26
|
-
if locals is None:
|
27
|
-
locals = {} # noqa
|
28
|
-
return_annotation = ''
|
29
|
-
if return_type.present:
|
30
|
-
locals['__dataclass_return_type__'] = return_type()
|
31
|
-
return_annotation = '->__dataclass_return_type__'
|
32
|
-
args = ','.join(args)
|
33
|
-
body = '\n'.join(f' {b}' for b in body)
|
34
|
-
|
35
|
-
txt = f' def {name}({args}){return_annotation}:\n{body}'
|
36
|
-
|
37
|
-
local_vars = ', '.join(locals.keys())
|
38
|
-
txt = f'def __create_fn__({local_vars}):\n{txt}\n return {name}'
|
39
|
-
ns: dict[str, ta.Any] = {}
|
40
|
-
exec(txt, globals, ns) # type: ignore
|
41
|
-
return ns['__create_fn__'](**locals)
|
42
|
-
|
43
|
-
|
44
|
-
# TODO: https://github.com/python/cpython/commit/8945b7ff55b87d11c747af2dad0e3e4d631e62d6
|
45
|
-
class FuncBuilder:
|
46
|
-
def __init__(self, globals: Namespace) -> None: # noqa
|
47
|
-
super().__init__()
|
48
|
-
|
49
|
-
self.names: list[str] = []
|
50
|
-
self.src: list[str] = []
|
51
|
-
self.globals = globals
|
52
|
-
self.locals: Namespace = {}
|
53
|
-
self.overwrite_errors: dict[str, bool] = {}
|
54
|
-
self.unconditional_adds: dict[str, bool] = {}
|
55
|
-
|
56
|
-
def add_fn(
|
57
|
-
self,
|
58
|
-
name: str,
|
59
|
-
args: ta.Sequence[str],
|
60
|
-
body: ta.Sequence[str],
|
61
|
-
*,
|
62
|
-
locals: Namespace | None = None, # noqa
|
63
|
-
return_type: lang.Maybe[ta.Any] = lang.empty(),
|
64
|
-
overwrite_error: bool = False,
|
65
|
-
unconditional_add: bool = False,
|
66
|
-
decorator: str | None = None,
|
67
|
-
) -> None:
|
68
|
-
if locals is not None:
|
69
|
-
self.locals.update(locals)
|
70
|
-
|
71
|
-
# Keep track if this method is allowed to be overwritten if it already exists in the class. The error is
|
72
|
-
# method-specific, so keep it with the name. We'll use this when we generate all of the functions in the
|
73
|
-
# add_fns_to_class call. overwrite_error is either True, in which case we'll raise an error, or it's a string,
|
74
|
-
# in which case we'll raise an error and append this string.
|
75
|
-
if overwrite_error:
|
76
|
-
self.overwrite_errors[name] = overwrite_error
|
77
|
-
|
78
|
-
# Should this function always overwrite anything that's already in the class? The default is to not overwrite a
|
79
|
-
# function that already exists.
|
80
|
-
if unconditional_add:
|
81
|
-
self.unconditional_adds[name] = True
|
82
|
-
|
83
|
-
self.names.append(name)
|
84
|
-
|
85
|
-
if return_type.present:
|
86
|
-
self.locals[f'__dataclass_{name}_return_type__'] = return_type()
|
87
|
-
return_annotation = f' -> __dataclass_{name}_return_type__'
|
88
|
-
else:
|
89
|
-
return_annotation = ''
|
90
|
-
args = ', '.join(args)
|
91
|
-
body = textwrap.indent('\n'.join(body), ' ')
|
92
|
-
|
93
|
-
# Compute the text of the entire function, add it to the text we're generating.
|
94
|
-
deco_str = ' {decorator}\n' if decorator else ''
|
95
|
-
self.src.append(f'{deco_str} def {name}({args}){return_annotation}:\n{body}')
|
96
|
-
|
97
|
-
def add_fns_to_class(self, cls: type) -> None:
|
98
|
-
# The source to all of the functions we're generating.
|
99
|
-
fns_src = '\n'.join(self.src)
|
100
|
-
|
101
|
-
# The locals they use.
|
102
|
-
local_vars = ','.join(self.locals)
|
103
|
-
|
104
|
-
# The names of all of the functions, used for the return value of the outer function. Need to handle the
|
105
|
-
# 0-tuple specially.
|
106
|
-
if not self.names:
|
107
|
-
return_names = '()'
|
108
|
-
else:
|
109
|
-
return_names = f'({",".join(self.names)},)'
|
110
|
-
|
111
|
-
# txt is the entire function we're going to execute, including the bodies of the functions we're defining.
|
112
|
-
# Here's a greatly simplified version:
|
113
|
-
# def __create_fn__():
|
114
|
-
# def __init__(self, x, y):
|
115
|
-
# self.x = x
|
116
|
-
# self.y = y
|
117
|
-
# @recursive_repr
|
118
|
-
# def __repr__(self):
|
119
|
-
# return f'cls(x={self.x!r},y={self.y!r})'
|
120
|
-
# return __init__,__repr__
|
121
|
-
|
122
|
-
txt = f'def __create_fn__({local_vars}):\n{fns_src}\n return {return_names}'
|
123
|
-
ns: dict[str, ta.Any] = {}
|
124
|
-
exec(txt, self.globals, ns) # type: ignore
|
125
|
-
fns = ns['__create_fn__'](**self.locals)
|
126
|
-
|
127
|
-
# Now that we've generated the functions, assign them into cls.
|
128
|
-
for name, fn in zip(self.names, fns):
|
129
|
-
fn.__qualname__ = f'{cls.__qualname__}.{fn.__name__}'
|
130
|
-
if self.unconditional_adds.get(name, False):
|
131
|
-
setattr(cls, name, fn)
|
132
|
-
else:
|
133
|
-
already_exists = set_new_attribute(cls, name, fn)
|
134
|
-
|
135
|
-
# See if it's an error to overwrite this particular function.
|
136
|
-
if already_exists and (msg_extra := self.overwrite_errors.get(name)):
|
137
|
-
error_msg = (f'Cannot overwrite attribute {fn.__name__} in class {cls.__name__}')
|
138
|
-
if msg_extra:
|
139
|
-
error_msg = f'{error_msg} {msg_extra}'
|
140
|
-
|
141
|
-
raise TypeError(error_msg)
|
142
|
-
|
143
|
-
|
144
|
-
def set_qualname(cls: type, value: T) -> T:
|
145
|
-
if isinstance(value, types.FunctionType):
|
146
|
-
value.__qualname__ = f'{cls.__qualname__}.{value.__name__}'
|
147
|
-
return value
|
148
|
-
|
149
|
-
|
150
|
-
def set_new_attribute(cls: type, name: str, value: ta.Any) -> bool:
|
151
|
-
if name in cls.__dict__:
|
152
|
-
return True
|
153
|
-
set_qualname(cls, value)
|
154
|
-
setattr(cls, name, value)
|
155
|
-
return False
|
156
|
-
|
157
|
-
|
158
|
-
def tuple_str(obj_name: str, fields: ta.Iterable[dc.Field]) -> str:
|
159
|
-
# Return a string representing each field of obj_name as a tuple member. So, if fields is ['x', 'y'] and obj_name
|
160
|
-
# is "self", return "(self.x,self.y)".
|
161
|
-
|
162
|
-
# Special case for the 0-tuple.
|
163
|
-
if not fields:
|
164
|
-
return '()'
|
165
|
-
|
166
|
-
# Note the trailing comma, needed if this turns out to be a 1-tuple.
|
167
|
-
return f'({",".join([f"{obj_name}.{f.name}" for f in fields])},)'
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|