salix 0.1.0a1__tar.gz
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.
- salix-0.1.0a1/CODE_OF_CONDUCT.md +132 -0
- salix-0.1.0a1/LICENSE +21 -0
- salix-0.1.0a1/MANIFEST.in +10 -0
- salix-0.1.0a1/PKG-INFO +223 -0
- salix-0.1.0a1/README.md +194 -0
- salix-0.1.0a1/build_config.py +88 -0
- salix-0.1.0a1/pyproject.toml +141 -0
- salix-0.1.0a1/salix/__init__.pyi +37 -0
- salix-0.1.0a1/salix/py.typed +0 -0
- salix-0.1.0a1/salix.egg-info/PKG-INFO +223 -0
- salix-0.1.0a1/salix.egg-info/SOURCES.txt +51 -0
- salix-0.1.0a1/salix.egg-info/dependency_links.txt +1 -0
- salix-0.1.0a1/salix.egg-info/top_level.txt +1 -0
- salix-0.1.0a1/setup.cfg +4 -0
- salix-0.1.0a1/setup.py +24 -0
- salix-0.1.0a1/src/annotations.c +169 -0
- salix-0.1.0a1/src/annotations.h +9 -0
- salix-0.1.0a1/src/compare.c +123 -0
- salix-0.1.0a1/src/compare.h +5 -0
- salix-0.1.0a1/src/construct/binding.c +116 -0
- salix-0.1.0a1/src/construct/construct.c +1194 -0
- salix-0.1.0a1/src/construct/construct.h +75 -0
- salix-0.1.0a1/src/construct/defaults.c +281 -0
- salix-0.1.0a1/src/construct/exceptions.c +595 -0
- salix-0.1.0a1/src/construct.h +28 -0
- salix-0.1.0a1/src/fields.c +1069 -0
- salix-0.1.0a1/src/fields.h +27 -0
- salix-0.1.0a1/src/hash.c +37 -0
- salix-0.1.0a1/src/hash.h +5 -0
- salix-0.1.0a1/src/meta/bases.c +309 -0
- salix-0.1.0a1/src/meta/class/create.c +655 -0
- salix-0.1.0a1/src/meta/class/install.c +641 -0
- salix-0.1.0a1/src/meta/meta.c +412 -0
- salix-0.1.0a1/src/meta/meta.h +166 -0
- salix-0.1.0a1/src/meta/namespace.c +776 -0
- salix-0.1.0a1/src/meta/settle/comparison.c +322 -0
- salix-0.1.0a1/src/meta/settle/restore.c +207 -0
- salix-0.1.0a1/src/meta/settle/settle.c +898 -0
- salix-0.1.0a1/src/meta/settle/settle.h +87 -0
- salix-0.1.0a1/src/meta.h +3 -0
- salix-0.1.0a1/src/mixin/copy.c +969 -0
- salix-0.1.0a1/src/mixin/mixin.h +9 -0
- salix-0.1.0a1/src/mixin.c +469 -0
- salix-0.1.0a1/src/mixin.h +13 -0
- salix-0.1.0a1/src/options.c +503 -0
- salix-0.1.0a1/src/options.h +50 -0
- salix-0.1.0a1/src/owned.h +56 -0
- salix-0.1.0a1/src/repr.c +116 -0
- salix-0.1.0a1/src/repr.h +5 -0
- salix-0.1.0a1/src/result.h +6 -0
- salix-0.1.0a1/src/salix.c +281 -0
- salix-0.1.0a1/src/testing.h +26 -0
- salix-0.1.0a1/src/types.h +423 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
- Demonstrating empathy and kindness toward other people
|
|
21
|
+
- Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
- Giving and gracefully accepting constructive feedback
|
|
23
|
+
- Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
- Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
- The use of sexualized language or imagery, and sexual attention or advances
|
|
31
|
+
of any kind
|
|
32
|
+
- Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
- Public or private harassment
|
|
34
|
+
- Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
- Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards
|
|
42
|
+
of acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for
|
|
49
|
+
moderation decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official e-mail address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
jphutchins@gmail.com. All complaints will be reviewed and investigated
|
|
64
|
+
promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external
|
|
92
|
+
channels like social media. Violating these terms may lead to a temporary or
|
|
93
|
+
permanent ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited
|
|
103
|
+
interaction with those enforcing the Code of Conduct, is allowed during this
|
|
104
|
+
period. Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
salix-0.1.0a1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 JP Hutchins
|
|
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.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
include CODE_OF_CONDUCT.md
|
|
2
|
+
include build_config.py
|
|
3
|
+
recursive-include src *.h
|
|
4
|
+
|
|
5
|
+
# A workspace member directory that ships without its pyproject.toml makes the
|
|
6
|
+
# sdist uninstallable under uv. The members are development scopes; an sdist
|
|
7
|
+
# exists to build the wheel.
|
|
8
|
+
prune bench
|
|
9
|
+
prune tests
|
|
10
|
+
prune tools
|
salix-0.1.0a1/PKG-INFO
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: salix
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: A C-backed, inheritable Struct base class for Python — fast import, fast type creation
|
|
5
|
+
Author-email: JP Hutchins <jphutchins@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/JPHutchins/salix
|
|
8
|
+
Project-URL: Issues, https://github.com/JPHutchins/salix/issues
|
|
9
|
+
Keywords: struct,dataclass,slots,frozen,immutable,import-time,free-threading,c-extension
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: C
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
|
+
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# salix
|
|
31
|
+
|
|
32
|
+
A C-backed, inheritable `Struct` base class for Python, for programs that pay
|
|
33
|
+
for their imports.
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
>>> from salix import Struct
|
|
37
|
+
|
|
38
|
+
>>> class Point(Struct):
|
|
39
|
+
... x: float
|
|
40
|
+
... y: float = 0.0
|
|
41
|
+
|
|
42
|
+
>>> Point(1.0, 2.0)
|
|
43
|
+
Point(x=1.0, y=2.0)
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Install
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
pip install salix
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`nix/python-targets.nix` is the wheel matrix — Windows, macOS, and glibc
|
|
54
|
+
Linux on x86_64 and arm64, across the supported CPythons — and wheels are
|
|
55
|
+
published for its cells once the interpreter's ABI is frozen. A pre-release
|
|
56
|
+
interpreter's wheels are built and tested but held back until its rc, and a
|
|
57
|
+
configuration missing from the matrix has no wheel. salix supports CPython
|
|
58
|
+
3.10 through 3.15, including the free-threaded builds. On a machine PyPI has
|
|
59
|
+
no wheel for, pip compiles the sdist — which needs setuptools and a C
|
|
60
|
+
compiler — except on Windows, where MSVC cannot compile this source. PyPy
|
|
61
|
+
and GraalPy satisfy the version floor and have no wheels, and whether the
|
|
62
|
+
sdist builds there is untested.
|
|
63
|
+
|
|
64
|
+
## What salix is and is not
|
|
65
|
+
|
|
66
|
+
A struct's fields are the annotated names of its class body, and the
|
|
67
|
+
constructor takes them in that order. Instances are frozen by default: reads,
|
|
68
|
+
value equality, hashing, and a repr come with the class; writes do not.
|
|
69
|
+
A body-written `__init__` — inherited or not, and not `object.__init__` —
|
|
70
|
+
replaces the field constructor; writing `object.__init__` back restores the
|
|
71
|
+
generated one, and on a frozen struct the body fills fields with `set_field`
|
|
72
|
+
(see Caching a computed value — the example there runs in `__post_init__`),
|
|
73
|
+
since assignment raises. Inheritance extends the field list
|
|
74
|
+
— a subclass adds its fields after its base's — and the metadata reads back:
|
|
75
|
+
`_struct_fields_`,
|
|
76
|
+
`_struct_defaults_`, `_struct_annotations_`, and `_struct_metadata_`, with
|
|
77
|
+
`__`-prefixed spellings of the same four names answering the same values.
|
|
78
|
+
|
|
79
|
+
The keywords opt out of the defaults. `frozen=False` opens the record for
|
|
80
|
+
writes and drops the hashing — under the default `eq=True` a writable struct
|
|
81
|
+
is unhashable, and a body `__hash__` still stands. `eq=False` drops the value
|
|
82
|
+
equality and hashing, leaving identity — the exceptions are named in Caching
|
|
83
|
+
a computed value. `order=True` adds the field-order comparisons and requires
|
|
84
|
+
`eq=True`. `repr`, `match_args`, and `weakref` default to `True`, `True`, and
|
|
85
|
+
`False` and stand in for the `__repr__`, the pattern-matching signature, and
|
|
86
|
+
the `__weakref__` slot. The six are consumed by the machinery; a class keyword
|
|
87
|
+
it does not own reaches `__init_subclass__` in the usual way, and one the
|
|
88
|
+
hierarchy does not claim still fails the class.
|
|
89
|
+
|
|
90
|
+
salix is not the libraries that do more. `dataclasses` builds the same record
|
|
91
|
+
in pure Python. `attrs` layers converters, validators, and a plugin ecosystem
|
|
92
|
+
on top. `NamedTuple` is the tuple-shaped predecessor. `msgspec` is a
|
|
93
|
+
serialization library whose `Struct` validates. salix does the record and
|
|
94
|
+
nothing else — no serialization, no field-value validation, no coercion — in
|
|
95
|
+
C, which is where its import and type-creation times come from. Programs that
|
|
96
|
+
need those features need those libraries; salix is for the ones that only
|
|
97
|
+
need the record. `salix/__init__.pyi` is the whole of the API.
|
|
98
|
+
|
|
99
|
+
## Defaults
|
|
100
|
+
|
|
101
|
+
An empty default of `list`, `dict`, `set`, or `bytearray` is copied per
|
|
102
|
+
instance, not shared:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
>>> from salix import Struct
|
|
106
|
+
|
|
107
|
+
>>> class Box(Struct):
|
|
108
|
+
... items: list = []
|
|
109
|
+
|
|
110
|
+
>>> left, right = Box(), Box()
|
|
111
|
+
>>> left.items.append("left")
|
|
112
|
+
>>> right.items
|
|
113
|
+
[]
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
A default of the four (and their subclasses) is copied per instance: a
|
|
118
|
+
non-empty one is deep-copied, so an inner list in `x: tuple = (1, [])` is
|
|
119
|
+
not shared, and an empty one copies through the declared type's own
|
|
120
|
+
constructor (seeded with a base copy, so the constructor cannot retain or
|
|
121
|
+
mutate the class-body object), preserving a subclass and falling back to a
|
|
122
|
+
base-type copy when the constructor signature is not the iterable one — a
|
|
123
|
+
`defaultdict` falls back and becomes a plain `dict`, its factory dropped.
|
|
124
|
+
A value a deepcopy cannot carry (a writable memoryview — one uncarryable
|
|
125
|
+
element shares the whole container) or one whose hash fails by recursing
|
|
126
|
+
falls back to sharing the declared object. A type outside
|
|
127
|
+
the four that declares `__hash__ = None` is shared. A field without a
|
|
128
|
+
default cannot
|
|
129
|
+
follow one that has it: append defaulted fields, or default the new field
|
|
130
|
+
too. For the four, `__struct_defaults__` holds the
|
|
131
|
+
class's own copy, severed from whatever the class body named: appending to
|
|
132
|
+
the module-level object afterwards does not reach the class. The getters hand
|
|
133
|
+
the stored objects out, not copies: do not mutate what `__struct_defaults__`
|
|
134
|
+
returns.
|
|
135
|
+
|
|
136
|
+
## ClassVar and InitVar
|
|
137
|
+
|
|
138
|
+
A name annotated `ClassVar[...]` at the top level is a class variable:
|
|
139
|
+
with an assigned value it is kept in the class dict and excluded from
|
|
140
|
+
the field plan, the constructor, and every metadata table; without one
|
|
141
|
+
it is refused, because a class variable is a constant. An inherited
|
|
142
|
+
field name stays a field: the inheritance rule outranks the ClassVar
|
|
143
|
+
annotation, so re-annotating one re-declares the field. `InitVar[...]`
|
|
144
|
+
is refused, and so is a `ClassVar` nested inside another annotation.
|
|
145
|
+
A protocol is satisfied structurally, never inherited: a struct may not
|
|
146
|
+
list a Protocol among its bases, because the two metaclasses conflict.
|
|
147
|
+
The check walks the annotation's forms when it arrives as an object;
|
|
148
|
+
when it arrives as source text — a quoted string, or a
|
|
149
|
+
future-annotations string — the check is a spelling match, and the two
|
|
150
|
+
paths differ at the edges:
|
|
151
|
+
|
|
152
|
+
| annotation | object path | text path |
|
|
153
|
+
| --- | --- | --- |
|
|
154
|
+
| an alias of `ClassVar`, `CV[int]` | the form itself: kept with a value, refused without | accepted; the field swallows a positional |
|
|
155
|
+
| a type of yours actually named `ClassVar` | accepted | kept with a value, refused without |
|
|
156
|
+
| `Annotated[int, ClassVar]` | accepted | refused, though the `ClassVar` there is metadata |
|
|
157
|
+
| `Optional[Annotated[ClassVar[int], "m"]]` | refused | refused |
|
|
158
|
+
|
|
159
|
+
3.14 evaluates resolvable annotations by default, so there the alias
|
|
160
|
+
reaches the object path and is the form; a name whose root cannot be
|
|
161
|
+
resolved arrives as an object too, and is accepted, while any other
|
|
162
|
+
evaluation error still fails the class. Every row is pinned in
|
|
163
|
+
`tests/test_classvar_paths.py`.
|
|
164
|
+
|
|
165
|
+
## Caching a computed value
|
|
166
|
+
|
|
167
|
+
`functools.cached_property` caches into an instance `__dict__`; a struct has
|
|
168
|
+
none unless a non-struct base carries one, and without one a slotted
|
|
169
|
+
dataclass refuses it the same way. Two answers: `functools.cache` on the
|
|
170
|
+
method, or a field that `__post_init__` fills:
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
>>> import functools
|
|
174
|
+
>>> from salix import Struct, set_field
|
|
175
|
+
|
|
176
|
+
>>> class Computed(Struct):
|
|
177
|
+
... x: int
|
|
178
|
+
...
|
|
179
|
+
... @functools.cache
|
|
180
|
+
... def double(self) -> int:
|
|
181
|
+
... return self.x * 2
|
|
182
|
+
|
|
183
|
+
>>> Computed(3).double()
|
|
184
|
+
6
|
|
185
|
+
|
|
186
|
+
>>> class Filled(Struct):
|
|
187
|
+
... x: int
|
|
188
|
+
... double: int = 0
|
|
189
|
+
...
|
|
190
|
+
... def __post_init__(self) -> None:
|
|
191
|
+
... set_field(self, "double", self.x * 2)
|
|
192
|
+
|
|
193
|
+
>>> Filled(4).double
|
|
194
|
+
8
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Under the default eq=True the method cache keys on the value, not the
|
|
199
|
+
instance: value-equal structs share one entry, and a `set_field` that
|
|
200
|
+
changes the value misses — the cache then holds the old entry and the new
|
|
201
|
+
one. With eq=False the struct hashes by identity — unless the body defines
|
|
202
|
+
its own `__eq__` without a `__hash__`, which makes it unhashable, or its
|
|
203
|
+
own `__hash__`, which replaces the identity hash — and `set_field` after
|
|
204
|
+
a hit returns the stale cached value while the hash is unchanged; a body
|
|
205
|
+
`__hash__` over field values changes the hash and the lookup misses. The
|
|
206
|
+
cache refuses an unhashable struct — anything that makes `hash()` raise:
|
|
207
|
+
a body `__eq__` without a body `__hash__`, a body `__hash__` that
|
|
208
|
+
refuses, `frozen=False`, an unhashable field value, or equality inherited
|
|
209
|
+
from a struct base that defines `__eq__`. An `__init__` that is not
|
|
210
|
+
`object.__init__` — a body-written one, inherited or not — replaces the
|
|
211
|
+
constructor that runs `__post_init__`, so the field answer only works
|
|
212
|
+
without one. The table and the caching behaviors above are pinned in
|
|
213
|
+
`tests/test_classvar_paths.py`.
|
|
214
|
+
|
|
215
|
+
## Working in the repo
|
|
216
|
+
|
|
217
|
+
The contributor loop is documented in `CONTRIBUTING.md`, and community
|
|
218
|
+
standards in `CODE_OF_CONDUCT.md`. `uv run camas benchmark` says what it
|
|
219
|
+
costs.
|
|
220
|
+
|
|
221
|
+
## License
|
|
222
|
+
|
|
223
|
+
MIT
|
salix-0.1.0a1/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# salix
|
|
2
|
+
|
|
3
|
+
A C-backed, inheritable `Struct` base class for Python, for programs that pay
|
|
4
|
+
for their imports.
|
|
5
|
+
|
|
6
|
+
```python
|
|
7
|
+
>>> from salix import Struct
|
|
8
|
+
|
|
9
|
+
>>> class Point(Struct):
|
|
10
|
+
... x: float
|
|
11
|
+
... y: float = 0.0
|
|
12
|
+
|
|
13
|
+
>>> Point(1.0, 2.0)
|
|
14
|
+
Point(x=1.0, y=2.0)
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
pip install salix
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`nix/python-targets.nix` is the wheel matrix — Windows, macOS, and glibc
|
|
25
|
+
Linux on x86_64 and arm64, across the supported CPythons — and wheels are
|
|
26
|
+
published for its cells once the interpreter's ABI is frozen. A pre-release
|
|
27
|
+
interpreter's wheels are built and tested but held back until its rc, and a
|
|
28
|
+
configuration missing from the matrix has no wheel. salix supports CPython
|
|
29
|
+
3.10 through 3.15, including the free-threaded builds. On a machine PyPI has
|
|
30
|
+
no wheel for, pip compiles the sdist — which needs setuptools and a C
|
|
31
|
+
compiler — except on Windows, where MSVC cannot compile this source. PyPy
|
|
32
|
+
and GraalPy satisfy the version floor and have no wheels, and whether the
|
|
33
|
+
sdist builds there is untested.
|
|
34
|
+
|
|
35
|
+
## What salix is and is not
|
|
36
|
+
|
|
37
|
+
A struct's fields are the annotated names of its class body, and the
|
|
38
|
+
constructor takes them in that order. Instances are frozen by default: reads,
|
|
39
|
+
value equality, hashing, and a repr come with the class; writes do not.
|
|
40
|
+
A body-written `__init__` — inherited or not, and not `object.__init__` —
|
|
41
|
+
replaces the field constructor; writing `object.__init__` back restores the
|
|
42
|
+
generated one, and on a frozen struct the body fills fields with `set_field`
|
|
43
|
+
(see Caching a computed value — the example there runs in `__post_init__`),
|
|
44
|
+
since assignment raises. Inheritance extends the field list
|
|
45
|
+
— a subclass adds its fields after its base's — and the metadata reads back:
|
|
46
|
+
`_struct_fields_`,
|
|
47
|
+
`_struct_defaults_`, `_struct_annotations_`, and `_struct_metadata_`, with
|
|
48
|
+
`__`-prefixed spellings of the same four names answering the same values.
|
|
49
|
+
|
|
50
|
+
The keywords opt out of the defaults. `frozen=False` opens the record for
|
|
51
|
+
writes and drops the hashing — under the default `eq=True` a writable struct
|
|
52
|
+
is unhashable, and a body `__hash__` still stands. `eq=False` drops the value
|
|
53
|
+
equality and hashing, leaving identity — the exceptions are named in Caching
|
|
54
|
+
a computed value. `order=True` adds the field-order comparisons and requires
|
|
55
|
+
`eq=True`. `repr`, `match_args`, and `weakref` default to `True`, `True`, and
|
|
56
|
+
`False` and stand in for the `__repr__`, the pattern-matching signature, and
|
|
57
|
+
the `__weakref__` slot. The six are consumed by the machinery; a class keyword
|
|
58
|
+
it does not own reaches `__init_subclass__` in the usual way, and one the
|
|
59
|
+
hierarchy does not claim still fails the class.
|
|
60
|
+
|
|
61
|
+
salix is not the libraries that do more. `dataclasses` builds the same record
|
|
62
|
+
in pure Python. `attrs` layers converters, validators, and a plugin ecosystem
|
|
63
|
+
on top. `NamedTuple` is the tuple-shaped predecessor. `msgspec` is a
|
|
64
|
+
serialization library whose `Struct` validates. salix does the record and
|
|
65
|
+
nothing else — no serialization, no field-value validation, no coercion — in
|
|
66
|
+
C, which is where its import and type-creation times come from. Programs that
|
|
67
|
+
need those features need those libraries; salix is for the ones that only
|
|
68
|
+
need the record. `salix/__init__.pyi` is the whole of the API.
|
|
69
|
+
|
|
70
|
+
## Defaults
|
|
71
|
+
|
|
72
|
+
An empty default of `list`, `dict`, `set`, or `bytearray` is copied per
|
|
73
|
+
instance, not shared:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
>>> from salix import Struct
|
|
77
|
+
|
|
78
|
+
>>> class Box(Struct):
|
|
79
|
+
... items: list = []
|
|
80
|
+
|
|
81
|
+
>>> left, right = Box(), Box()
|
|
82
|
+
>>> left.items.append("left")
|
|
83
|
+
>>> right.items
|
|
84
|
+
[]
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
A default of the four (and their subclasses) is copied per instance: a
|
|
89
|
+
non-empty one is deep-copied, so an inner list in `x: tuple = (1, [])` is
|
|
90
|
+
not shared, and an empty one copies through the declared type's own
|
|
91
|
+
constructor (seeded with a base copy, so the constructor cannot retain or
|
|
92
|
+
mutate the class-body object), preserving a subclass and falling back to a
|
|
93
|
+
base-type copy when the constructor signature is not the iterable one — a
|
|
94
|
+
`defaultdict` falls back and becomes a plain `dict`, its factory dropped.
|
|
95
|
+
A value a deepcopy cannot carry (a writable memoryview — one uncarryable
|
|
96
|
+
element shares the whole container) or one whose hash fails by recursing
|
|
97
|
+
falls back to sharing the declared object. A type outside
|
|
98
|
+
the four that declares `__hash__ = None` is shared. A field without a
|
|
99
|
+
default cannot
|
|
100
|
+
follow one that has it: append defaulted fields, or default the new field
|
|
101
|
+
too. For the four, `__struct_defaults__` holds the
|
|
102
|
+
class's own copy, severed from whatever the class body named: appending to
|
|
103
|
+
the module-level object afterwards does not reach the class. The getters hand
|
|
104
|
+
the stored objects out, not copies: do not mutate what `__struct_defaults__`
|
|
105
|
+
returns.
|
|
106
|
+
|
|
107
|
+
## ClassVar and InitVar
|
|
108
|
+
|
|
109
|
+
A name annotated `ClassVar[...]` at the top level is a class variable:
|
|
110
|
+
with an assigned value it is kept in the class dict and excluded from
|
|
111
|
+
the field plan, the constructor, and every metadata table; without one
|
|
112
|
+
it is refused, because a class variable is a constant. An inherited
|
|
113
|
+
field name stays a field: the inheritance rule outranks the ClassVar
|
|
114
|
+
annotation, so re-annotating one re-declares the field. `InitVar[...]`
|
|
115
|
+
is refused, and so is a `ClassVar` nested inside another annotation.
|
|
116
|
+
A protocol is satisfied structurally, never inherited: a struct may not
|
|
117
|
+
list a Protocol among its bases, because the two metaclasses conflict.
|
|
118
|
+
The check walks the annotation's forms when it arrives as an object;
|
|
119
|
+
when it arrives as source text — a quoted string, or a
|
|
120
|
+
future-annotations string — the check is a spelling match, and the two
|
|
121
|
+
paths differ at the edges:
|
|
122
|
+
|
|
123
|
+
| annotation | object path | text path |
|
|
124
|
+
| --- | --- | --- |
|
|
125
|
+
| an alias of `ClassVar`, `CV[int]` | the form itself: kept with a value, refused without | accepted; the field swallows a positional |
|
|
126
|
+
| a type of yours actually named `ClassVar` | accepted | kept with a value, refused without |
|
|
127
|
+
| `Annotated[int, ClassVar]` | accepted | refused, though the `ClassVar` there is metadata |
|
|
128
|
+
| `Optional[Annotated[ClassVar[int], "m"]]` | refused | refused |
|
|
129
|
+
|
|
130
|
+
3.14 evaluates resolvable annotations by default, so there the alias
|
|
131
|
+
reaches the object path and is the form; a name whose root cannot be
|
|
132
|
+
resolved arrives as an object too, and is accepted, while any other
|
|
133
|
+
evaluation error still fails the class. Every row is pinned in
|
|
134
|
+
`tests/test_classvar_paths.py`.
|
|
135
|
+
|
|
136
|
+
## Caching a computed value
|
|
137
|
+
|
|
138
|
+
`functools.cached_property` caches into an instance `__dict__`; a struct has
|
|
139
|
+
none unless a non-struct base carries one, and without one a slotted
|
|
140
|
+
dataclass refuses it the same way. Two answers: `functools.cache` on the
|
|
141
|
+
method, or a field that `__post_init__` fills:
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
>>> import functools
|
|
145
|
+
>>> from salix import Struct, set_field
|
|
146
|
+
|
|
147
|
+
>>> class Computed(Struct):
|
|
148
|
+
... x: int
|
|
149
|
+
...
|
|
150
|
+
... @functools.cache
|
|
151
|
+
... def double(self) -> int:
|
|
152
|
+
... return self.x * 2
|
|
153
|
+
|
|
154
|
+
>>> Computed(3).double()
|
|
155
|
+
6
|
|
156
|
+
|
|
157
|
+
>>> class Filled(Struct):
|
|
158
|
+
... x: int
|
|
159
|
+
... double: int = 0
|
|
160
|
+
...
|
|
161
|
+
... def __post_init__(self) -> None:
|
|
162
|
+
... set_field(self, "double", self.x * 2)
|
|
163
|
+
|
|
164
|
+
>>> Filled(4).double
|
|
165
|
+
8
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Under the default eq=True the method cache keys on the value, not the
|
|
170
|
+
instance: value-equal structs share one entry, and a `set_field` that
|
|
171
|
+
changes the value misses — the cache then holds the old entry and the new
|
|
172
|
+
one. With eq=False the struct hashes by identity — unless the body defines
|
|
173
|
+
its own `__eq__` without a `__hash__`, which makes it unhashable, or its
|
|
174
|
+
own `__hash__`, which replaces the identity hash — and `set_field` after
|
|
175
|
+
a hit returns the stale cached value while the hash is unchanged; a body
|
|
176
|
+
`__hash__` over field values changes the hash and the lookup misses. The
|
|
177
|
+
cache refuses an unhashable struct — anything that makes `hash()` raise:
|
|
178
|
+
a body `__eq__` without a body `__hash__`, a body `__hash__` that
|
|
179
|
+
refuses, `frozen=False`, an unhashable field value, or equality inherited
|
|
180
|
+
from a struct base that defines `__eq__`. An `__init__` that is not
|
|
181
|
+
`object.__init__` — a body-written one, inherited or not — replaces the
|
|
182
|
+
constructor that runs `__post_init__`, so the field answer only works
|
|
183
|
+
without one. The table and the caching behaviors above are pinned in
|
|
184
|
+
`tests/test_classvar_paths.py`.
|
|
185
|
+
|
|
186
|
+
## Working in the repo
|
|
187
|
+
|
|
188
|
+
The contributor loop is documented in `CONTRIBUTING.md`, and community
|
|
189
|
+
standards in `CODE_OF_CONDUCT.md`. `uv run camas benchmark` says what it
|
|
190
|
+
costs.
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
MIT
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Final, NamedTuple
|
|
4
|
+
|
|
5
|
+
if sys.version_info >= (3, 11):
|
|
6
|
+
import tomllib
|
|
7
|
+
else:
|
|
8
|
+
import tomli as tomllib
|
|
9
|
+
|
|
10
|
+
VERSION: Final = tomllib.loads(
|
|
11
|
+
Path(__file__).resolve().with_name("pyproject.toml").read_text(encoding="utf-8")
|
|
12
|
+
)["project"]["version"]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class BuildConfig(NamedTuple):
|
|
16
|
+
sources: tuple[str, ...]
|
|
17
|
+
c_flags: tuple[str, ...]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
BUILD: Final = BuildConfig(
|
|
21
|
+
sources=(
|
|
22
|
+
"src/salix.c",
|
|
23
|
+
"src/annotations.c",
|
|
24
|
+
"src/compare.c",
|
|
25
|
+
"src/construct/construct.c",
|
|
26
|
+
"src/construct/binding.c",
|
|
27
|
+
"src/construct/defaults.c",
|
|
28
|
+
"src/construct/exceptions.c",
|
|
29
|
+
"src/fields.c",
|
|
30
|
+
"src/hash.c",
|
|
31
|
+
"src/meta/meta.c",
|
|
32
|
+
"src/meta/bases.c",
|
|
33
|
+
"src/meta/namespace.c",
|
|
34
|
+
"src/meta/class/create.c",
|
|
35
|
+
"src/meta/class/install.c",
|
|
36
|
+
"src/meta/settle/settle.c",
|
|
37
|
+
"src/meta/settle/comparison.c",
|
|
38
|
+
"src/meta/settle/restore.c",
|
|
39
|
+
"src/mixin.c",
|
|
40
|
+
"src/mixin/copy.c",
|
|
41
|
+
"src/options.c",
|
|
42
|
+
"src/repr.c",
|
|
43
|
+
),
|
|
44
|
+
# -Wno-unused-parameter: CPython slot signatures are fixed by the API and
|
|
45
|
+
# routinely ignore an argument.
|
|
46
|
+
# -std=c2x, not -std=c23: they select the same language mode -- both give
|
|
47
|
+
# __STDC_VERSION__ 202311 -- but c2x is accepted by GCC 9+ and Clang 9+
|
|
48
|
+
# while c23 needs GCC 14+ or Clang 18+. An sdist has to compile on whatever
|
|
49
|
+
# the machine has, and Ubuntu 24.04 LTS still ships GCC 13.
|
|
50
|
+
c_flags=(
|
|
51
|
+
f"-DSALIX_VERSION={VERSION}",
|
|
52
|
+
"-std=c2x",
|
|
53
|
+
"-O2",
|
|
54
|
+
"-Wdouble-promotion",
|
|
55
|
+
"-Wall",
|
|
56
|
+
"-Wextra",
|
|
57
|
+
"-Wno-unused-parameter",
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# setup.py passes these after CFLAGS, so an sdist build cannot override them.
|
|
62
|
+
# One new warning in a future compiler would then turn `pip install salix` into
|
|
63
|
+
# a failure, on the machines that have no wheel and must compile.
|
|
64
|
+
STRICT: Final = ("-Werror",)
|
|
65
|
+
|
|
66
|
+
# 84% of a published payload was DWARF that nothing on the far end reads. Not
|
|
67
|
+
# in c_flags, so a local build stays debuggable.
|
|
68
|
+
SHIPPED: Final = ("-g0",)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
if __name__ == "__main__":
|
|
72
|
+
import sys
|
|
73
|
+
|
|
74
|
+
match sys.argv[1:]:
|
|
75
|
+
case ["sources"]:
|
|
76
|
+
print("\n".join(BUILD.sources))
|
|
77
|
+
case ["c-flags"]:
|
|
78
|
+
print("\n".join(BUILD.c_flags))
|
|
79
|
+
case ["c-flags", "--strict"]:
|
|
80
|
+
print("\n".join(BUILD.c_flags + STRICT))
|
|
81
|
+
case ["c-flags", "--strict", "--shipped"]:
|
|
82
|
+
print("\n".join(BUILD.c_flags + STRICT + SHIPPED))
|
|
83
|
+
case ["c-flags", "--shipped", *_]:
|
|
84
|
+
raise SystemExit("build_config.py: --shipped only follows --strict")
|
|
85
|
+
case _:
|
|
86
|
+
raise SystemExit(
|
|
87
|
+
"usage: build_config.py {sources|c-flags [--strict [--shipped]]}"
|
|
88
|
+
)
|