dcast 0.1.0__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.
- dcast-0.1.0/LICENSE +21 -0
- dcast-0.1.0/MANIFEST.in +3 -0
- dcast-0.1.0/PKG-INFO +155 -0
- dcast-0.1.0/README.md +128 -0
- dcast-0.1.0/csrc/cast.c +579 -0
- dcast-0.1.0/csrc/cast.h +14 -0
- dcast-0.1.0/csrc/module.c +54 -0
- dcast-0.1.0/csrc/spec.c +384 -0
- dcast-0.1.0/csrc/spec.h +68 -0
- dcast-0.1.0/csrc/validate.c +425 -0
- dcast-0.1.0/csrc/validate.h +14 -0
- dcast-0.1.0/pyproject.toml +47 -0
- dcast-0.1.0/setup.cfg +4 -0
- dcast-0.1.0/setup.py +21 -0
- dcast-0.1.0/src/dcast/__init__.py +33 -0
- dcast-0.1.0/src/dcast/__init__.pyi +38 -0
- dcast-0.1.0/src/dcast/py.typed +0 -0
- dcast-0.1.0/src/dcast/spec.py +90 -0
- dcast-0.1.0/src/dcast.egg-info/PKG-INFO +155 -0
- dcast-0.1.0/src/dcast.egg-info/SOURCES.txt +23 -0
- dcast-0.1.0/src/dcast.egg-info/dependency_links.txt +1 -0
- dcast-0.1.0/src/dcast.egg-info/requires.txt +3 -0
- dcast-0.1.0/src/dcast.egg-info/top_level.txt +1 -0
- dcast-0.1.0/tests/test_cast.py +159 -0
- dcast-0.1.0/tests/test_validate.py +142 -0
dcast-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dupspirit
|
|
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.
|
dcast-0.1.0/MANIFEST.in
ADDED
dcast-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dcast
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lightweight Python data validation and conversion using CPython abi3
|
|
5
|
+
Author-email: dupspirit <dupspirit@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/dupspirit/dcast
|
|
8
|
+
Project-URL: Repository, https://github.com/dupspirit/dcast
|
|
9
|
+
Project-URL: Issues, https://github.com/dupspirit/dcast/issues
|
|
10
|
+
Keywords: validation,casting,dataclass,abi3,typing
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: C
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
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 :: Implementation :: CPython
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# dcast
|
|
29
|
+
|
|
30
|
+
A Lightweight Python data validation and casting library built as a CPython (`abi3`) C extension with zero external dependencies.
|
|
31
|
+
|
|
32
|
+
When full-featured frameworks like `pydantic` are heavier than needed, `dcast` focuses on validating and casting native Python types—with first-class support for `dataclass`.
|
|
33
|
+
|
|
34
|
+
*Built with assistance from GitHub Copilot and Gemini.*
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
Requires Python 3.10+.
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install dcast
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
To build from source (requires a C compiler):
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install -e ".[dev]"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Quick Start
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from dataclasses import dataclass, field
|
|
54
|
+
from dcast import validate, cast, Caster
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass
|
|
58
|
+
class User:
|
|
59
|
+
name: str
|
|
60
|
+
age: int
|
|
61
|
+
tags: list[str] = field(default_factory=list)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# Strict by default (exact types required)
|
|
65
|
+
validate({"name": "Alice", "age": 30}, User) # True
|
|
66
|
+
validate({"name": "Alice", "age": "30"}, User) # False
|
|
67
|
+
|
|
68
|
+
user = cast({"name": "Alice", "age": 30}, User)
|
|
69
|
+
# User(name='Alice', age=30, tags=[])
|
|
70
|
+
|
|
71
|
+
# Reusable non-strict caster (avoids repeating strict=False)
|
|
72
|
+
lax = Caster(strict=False)
|
|
73
|
+
user = lax.cast({"name": "Alice", "age": "30"}, User)
|
|
74
|
+
# User(name='Alice', age=30, tags=[])
|
|
75
|
+
|
|
76
|
+
user = lax.cast(["Bob", "25", ("dev",)], User)
|
|
77
|
+
# User(name='Bob', age=25, tags=['dev'])
|
|
78
|
+
|
|
79
|
+
# Invalid input raises ValidationError (subclass of ValueError)
|
|
80
|
+
lax.cast({"name": "Alice", "age": "not-an-int"}, User, strict=False)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Features
|
|
84
|
+
|
|
85
|
+
### `dcast.validate(value, target, strict=True) -> bool`
|
|
86
|
+
|
|
87
|
+
Return `True` if `value` matches `target`.
|
|
88
|
+
|
|
89
|
+
**Parameters**
|
|
90
|
+
- `value` (`object`): Input value to check.
|
|
91
|
+
- `target` (`Any`): Target type annotation or `dataclass`.
|
|
92
|
+
- `strict` (`bool`, default `True`): Require exact types with no coercion.
|
|
93
|
+
|
|
94
|
+
**Returns**
|
|
95
|
+
- `bool`: `True` if `value` is valid for `target`, otherwise `False`.
|
|
96
|
+
|
|
97
|
+
**Raises**
|
|
98
|
+
- `TypeError`: If `target` is not a supported type.
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
### `dcast.cast(value, target, strict=True) -> T`
|
|
103
|
+
|
|
104
|
+
Cast `value` to `target`, constructing containers and `dataclass` instances as needed.
|
|
105
|
+
|
|
106
|
+
*Call `dcast.cast(...)` via the module namespace to avoid shadowing `typing.cast`.*
|
|
107
|
+
|
|
108
|
+
**Parameters**
|
|
109
|
+
- `value` (`object`): Input value to cast.
|
|
110
|
+
- `target` (`type[T] | Any`): Target type annotation or `dataclass`.
|
|
111
|
+
- `strict` (`bool`, default `True`): Require exact types with no coercion.
|
|
112
|
+
|
|
113
|
+
**Returns**
|
|
114
|
+
- `T`: The validated or coerced instance of `target`.
|
|
115
|
+
|
|
116
|
+
**Raises**
|
|
117
|
+
- `dcast.ValidationError`: If `value` cannot be cast to `target`.
|
|
118
|
+
- `TypeError`: If `target` is not a supported type.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
### `dcast.Caster(strict=True)`
|
|
123
|
+
|
|
124
|
+
Reusable validator and caster with pre-configured defaults.
|
|
125
|
+
|
|
126
|
+
**Parameters**
|
|
127
|
+
- `strict` (`bool`, default `True`): Default strictness used by `.validate()` and `.cast()`.
|
|
128
|
+
|
|
129
|
+
**Methods**
|
|
130
|
+
- `validate(value, target, strict=None) -> bool`: Calls `dcast.validate` with `self.strict` unless `strict` is overridden.
|
|
131
|
+
- `cast(value, target, strict=None) -> T`: Calls `dcast.cast` with `self.strict` unless `strict` is overridden.
|
|
132
|
+
|
|
133
|
+
## Supported Types & Coercion Rules
|
|
134
|
+
|
|
135
|
+
| Target | `strict=True` | `strict=False` |
|
|
136
|
+
| --- | --- | --- |
|
|
137
|
+
| `Any` / `object` | Any value | Any value |
|
|
138
|
+
| `None` / `NoneType` | `None` only | `None` only |
|
|
139
|
+
| `bool` | `bool` only | `bool`, `0`/`1`, `"true"`/`"false"`/`"1"`/`"0"` |
|
|
140
|
+
| `int` | `int` (excludes `bool`) | `int`, `bool`, whole `float` (`3.0`), integer `str` |
|
|
141
|
+
| `float` | `float` only | `float`, `int` (excludes `bool`), numeric `str` |
|
|
142
|
+
| `str` | `str` only | `str`, `int`, `float`, `bool` |
|
|
143
|
+
| `list` / `list[T]` | `list` with matching items | `list` or `tuple` with castable items |
|
|
144
|
+
| `dict` / `dict[K, V]` | `dict` with matching entries | `dict` with castable entries |
|
|
145
|
+
| `Union` / `Optional` / `\|` | First matching branch | Strict pass first, then non-strict pass |
|
|
146
|
+
| `dataclass` | Instance or `dict` / `Mapping` | Instance, `dict` / `Mapping`, or `list` / `tuple` |
|
|
147
|
+
|
|
148
|
+
Recursive `dataclass` definitions (e.g. `Node.children: list[Node]`) are supported.
|
|
149
|
+
|
|
150
|
+
## Key Goals
|
|
151
|
+
|
|
152
|
+
- **Fast**: Type annotations are compiled once in Python and executed in C (`Py_LIMITED_API` / `abi3`).
|
|
153
|
+
- **Zero dependencies**: Relies only on the Python standard library.
|
|
154
|
+
- **Native Python first**: Built around standard Python types and `dataclass`.
|
|
155
|
+
- **Simple & predictable**: Minimal surface area with clear strict vs. non-strict behavior.
|
dcast-0.1.0/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# dcast
|
|
2
|
+
|
|
3
|
+
A Lightweight Python data validation and casting library built as a CPython (`abi3`) C extension with zero external dependencies.
|
|
4
|
+
|
|
5
|
+
When full-featured frameworks like `pydantic` are heavier than needed, `dcast` focuses on validating and casting native Python types—with first-class support for `dataclass`.
|
|
6
|
+
|
|
7
|
+
*Built with assistance from GitHub Copilot and Gemini.*
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Requires Python 3.10+.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install dcast
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
To build from source (requires a C compiler):
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install -e ".[dev]"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from dataclasses import dataclass, field
|
|
27
|
+
from dcast import validate, cast, Caster
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class User:
|
|
32
|
+
name: str
|
|
33
|
+
age: int
|
|
34
|
+
tags: list[str] = field(default_factory=list)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# Strict by default (exact types required)
|
|
38
|
+
validate({"name": "Alice", "age": 30}, User) # True
|
|
39
|
+
validate({"name": "Alice", "age": "30"}, User) # False
|
|
40
|
+
|
|
41
|
+
user = cast({"name": "Alice", "age": 30}, User)
|
|
42
|
+
# User(name='Alice', age=30, tags=[])
|
|
43
|
+
|
|
44
|
+
# Reusable non-strict caster (avoids repeating strict=False)
|
|
45
|
+
lax = Caster(strict=False)
|
|
46
|
+
user = lax.cast({"name": "Alice", "age": "30"}, User)
|
|
47
|
+
# User(name='Alice', age=30, tags=[])
|
|
48
|
+
|
|
49
|
+
user = lax.cast(["Bob", "25", ("dev",)], User)
|
|
50
|
+
# User(name='Bob', age=25, tags=['dev'])
|
|
51
|
+
|
|
52
|
+
# Invalid input raises ValidationError (subclass of ValueError)
|
|
53
|
+
lax.cast({"name": "Alice", "age": "not-an-int"}, User, strict=False)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Features
|
|
57
|
+
|
|
58
|
+
### `dcast.validate(value, target, strict=True) -> bool`
|
|
59
|
+
|
|
60
|
+
Return `True` if `value` matches `target`.
|
|
61
|
+
|
|
62
|
+
**Parameters**
|
|
63
|
+
- `value` (`object`): Input value to check.
|
|
64
|
+
- `target` (`Any`): Target type annotation or `dataclass`.
|
|
65
|
+
- `strict` (`bool`, default `True`): Require exact types with no coercion.
|
|
66
|
+
|
|
67
|
+
**Returns**
|
|
68
|
+
- `bool`: `True` if `value` is valid for `target`, otherwise `False`.
|
|
69
|
+
|
|
70
|
+
**Raises**
|
|
71
|
+
- `TypeError`: If `target` is not a supported type.
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
### `dcast.cast(value, target, strict=True) -> T`
|
|
76
|
+
|
|
77
|
+
Cast `value` to `target`, constructing containers and `dataclass` instances as needed.
|
|
78
|
+
|
|
79
|
+
*Call `dcast.cast(...)` via the module namespace to avoid shadowing `typing.cast`.*
|
|
80
|
+
|
|
81
|
+
**Parameters**
|
|
82
|
+
- `value` (`object`): Input value to cast.
|
|
83
|
+
- `target` (`type[T] | Any`): Target type annotation or `dataclass`.
|
|
84
|
+
- `strict` (`bool`, default `True`): Require exact types with no coercion.
|
|
85
|
+
|
|
86
|
+
**Returns**
|
|
87
|
+
- `T`: The validated or coerced instance of `target`.
|
|
88
|
+
|
|
89
|
+
**Raises**
|
|
90
|
+
- `dcast.ValidationError`: If `value` cannot be cast to `target`.
|
|
91
|
+
- `TypeError`: If `target` is not a supported type.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
### `dcast.Caster(strict=True)`
|
|
96
|
+
|
|
97
|
+
Reusable validator and caster with pre-configured defaults.
|
|
98
|
+
|
|
99
|
+
**Parameters**
|
|
100
|
+
- `strict` (`bool`, default `True`): Default strictness used by `.validate()` and `.cast()`.
|
|
101
|
+
|
|
102
|
+
**Methods**
|
|
103
|
+
- `validate(value, target, strict=None) -> bool`: Calls `dcast.validate` with `self.strict` unless `strict` is overridden.
|
|
104
|
+
- `cast(value, target, strict=None) -> T`: Calls `dcast.cast` with `self.strict` unless `strict` is overridden.
|
|
105
|
+
|
|
106
|
+
## Supported Types & Coercion Rules
|
|
107
|
+
|
|
108
|
+
| Target | `strict=True` | `strict=False` |
|
|
109
|
+
| --- | --- | --- |
|
|
110
|
+
| `Any` / `object` | Any value | Any value |
|
|
111
|
+
| `None` / `NoneType` | `None` only | `None` only |
|
|
112
|
+
| `bool` | `bool` only | `bool`, `0`/`1`, `"true"`/`"false"`/`"1"`/`"0"` |
|
|
113
|
+
| `int` | `int` (excludes `bool`) | `int`, `bool`, whole `float` (`3.0`), integer `str` |
|
|
114
|
+
| `float` | `float` only | `float`, `int` (excludes `bool`), numeric `str` |
|
|
115
|
+
| `str` | `str` only | `str`, `int`, `float`, `bool` |
|
|
116
|
+
| `list` / `list[T]` | `list` with matching items | `list` or `tuple` with castable items |
|
|
117
|
+
| `dict` / `dict[K, V]` | `dict` with matching entries | `dict` with castable entries |
|
|
118
|
+
| `Union` / `Optional` / `\|` | First matching branch | Strict pass first, then non-strict pass |
|
|
119
|
+
| `dataclass` | Instance or `dict` / `Mapping` | Instance, `dict` / `Mapping`, or `list` / `tuple` |
|
|
120
|
+
|
|
121
|
+
Recursive `dataclass` definitions (e.g. `Node.children: list[Node]`) are supported.
|
|
122
|
+
|
|
123
|
+
## Key Goals
|
|
124
|
+
|
|
125
|
+
- **Fast**: Type annotations are compiled once in Python and executed in C (`Py_LIMITED_API` / `abi3`).
|
|
126
|
+
- **Zero dependencies**: Relies only on the Python standard library.
|
|
127
|
+
- **Native Python first**: Built around standard Python types and `dataclass`.
|
|
128
|
+
- **Simple & predictable**: Minimal surface area with clear strict vs. non-strict behavior.
|