conformly 0.7.3__tar.gz → 0.8.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.
- conformly-0.8.0/PKG-INFO +117 -0
- conformly-0.8.0/README.md +84 -0
- {conformly-0.7.3 → conformly-0.8.0}/pyproject.toml +1 -1
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/__init__.py +3 -1
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/api/_utils.py +7 -1
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/constraints/numeric.py +6 -4
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/fields/__init__.py +3 -1
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/orchestration.py +100 -23
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/registry.py +12 -0
- conformly-0.8.0/src/conformly/_internal/generator/types/_utils.py +84 -0
- conformly-0.8.0/src/conformly/_internal/generator/types/bytes.py +25 -0
- conformly-0.8.0/src/conformly/_internal/generator/types/dictionaries.py +137 -0
- conformly-0.8.0/src/conformly/_internal/generator/types/float.py +119 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/integer.py +11 -3
- conformly-0.8.0/src/conformly/_internal/generator/types/list.py +135 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/string.py +42 -30
- conformly-0.8.0/src/conformly/_internal/generator/types/temporal.py +59 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/tuple.py +33 -33
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/adapters/attrs.py +6 -1
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/adapters/pydantic.py +1 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/adapters/typeddict.py +9 -3
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/core/element.py +0 -2
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/core/field.py +5 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/extractors/constraints.py +6 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/models.py +6 -1
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/planner/field.py +130 -38
- conformly-0.8.0/src/conformly/_internal/planner/model.py +18 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/planner/session.py +17 -4
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/models.py +9 -2
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/resolve.py +73 -57
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/__init__.py +6 -0
- conformly-0.8.0/src/conformly/_internal/resolver/semantics/_domains.py +35 -0
- conformly-0.8.0/src/conformly/_internal/resolver/semantics/_numeric.py +15 -0
- conformly-0.8.0/src/conformly/_internal/resolver/semantics/_regex.py +9 -0
- conformly-0.8.0/src/conformly/_internal/resolver/semantics/bytes.py +48 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/factory.py +6 -0
- conformly-0.8.0/src/conformly/_internal/resolver/semantics/temporal.py +139 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/types/primitives.py +5 -0
- conformly-0.8.0/src/conformly.egg-info/PKG-INFO +117 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly.egg-info/SOURCES.txt +7 -0
- conformly-0.7.3/PKG-INFO +0 -756
- conformly-0.7.3/README.md +0 -723
- conformly-0.7.3/src/conformly/_internal/generator/types/_utils.py +0 -63
- conformly-0.7.3/src/conformly/_internal/generator/types/dictionaries.py +0 -122
- conformly-0.7.3/src/conformly/_internal/generator/types/float.py +0 -89
- conformly-0.7.3/src/conformly/_internal/generator/types/list.py +0 -112
- conformly-0.7.3/src/conformly/_internal/planner/model.py +0 -9
- conformly-0.7.3/src/conformly.egg-info/PKG-INFO +0 -756
- {conformly-0.7.3 → conformly-0.8.0}/LICENSE +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/setup.cfg +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/api/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/api/_errors.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/api/_path_proxy.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/api/case.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/api/cases.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/api/path.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/constraints/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/constraints/base.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/constraints/collections.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/constraints/enum.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/constraints/keys.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/constraints/mapping.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/constraints/string.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/fields/registry.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/fields/special_types.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/_errors.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/context.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/protocol.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/boolean.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/email.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/enum.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/ipv4.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/ipv6.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/ipvany.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/url.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/generator/types/uuid.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/adapters/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/adapters/bootstrap.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/adapters/dataclass.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/adapters/protocol.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/adapters/registry.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/api.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/core/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/extractors/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/parser/extractors/types.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/planner/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/planner/_errors.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/_validators.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/base.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/boolean.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/dict.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/enum.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/list.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/numeric.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/object.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/string.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/tuple.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/resolver/semantics/uuid.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/tracer.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/types/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/types/constants.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/types/strategies.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/_internal/types/values.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/exceptions.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/py.typed +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly/tracer/__init__.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly.egg-info/dependency_links.txt +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly.egg-info/requires.txt +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/src/conformly.egg-info/top_level.txt +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/tests/test_collection.py +0 -0
- {conformly-0.7.3 → conformly-0.8.0}/tests/test_path.py +0 -0
conformly-0.8.0/PKG-INFO
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: conformly
|
|
3
|
+
Version: 0.8.0
|
|
4
|
+
Summary: Generate valid & invalid test data from your typed schemas
|
|
5
|
+
Author-email: Nikita Shabanov <nik.shabanov2024@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/nashabanov/conformly
|
|
8
|
+
Project-URL: Repository, https://github.com/nashabanov/conformly.git
|
|
9
|
+
Project-URL: Issues, https://github.com/nashabanov/conformly/issues
|
|
10
|
+
Keywords: testing,fixtures,test-data,dataclasses,validation
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Software Development :: Testing
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: rstr>=3.2.2
|
|
23
|
+
Provides-Extra: pydantic
|
|
24
|
+
Requires-Dist: pydantic<3.0.0,>=2.0.0; extra == "pydantic"
|
|
25
|
+
Requires-Dist: email-validator>=2.0.0; extra == "pydantic"
|
|
26
|
+
Provides-Extra: attrs
|
|
27
|
+
Requires-Dist: attrs>=23.0.0; extra == "attrs"
|
|
28
|
+
Provides-Extra: all
|
|
29
|
+
Requires-Dist: pydantic<3.0.0,>=2.0.0; extra == "all"
|
|
30
|
+
Requires-Dist: email-validator>=2.0.0; extra == "all"
|
|
31
|
+
Requires-Dist: attrs>=23.0.0; extra == "all"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# conformly
|
|
35
|
+
|
|
36
|
+
[](https://github.com/nashabanov/conformly/actions/workflows/ci.yaml)
|
|
37
|
+
[](https://codecov.io/github/nashabanov/conformly)
|
|
38
|
+
[](https://pypi.org/project/conformly/)
|
|
39
|
+
[](https://pypi.org/project/conformly/)
|
|
40
|
+
|
|
41
|
+
[](https://opensource.org/licenses/MIT)
|
|
42
|
+
[](https://github.com/nashabanov/conformly/releases)
|
|
43
|
+
[](http://mypy-lang.org/)
|
|
44
|
+
[](https://github.com/astral-sh/ruff)
|
|
45
|
+
|
|
46
|
+
**Declarative test data generator for Python.**
|
|
47
|
+
|
|
48
|
+
Define constraints once in type annotations — generate both valid and minimal invalid test data automatically.
|
|
49
|
+
No factories, no hardcoded fixtures, no drift when schema changes.
|
|
50
|
+
|
|
51
|
+
Conformly turns typed schemas into dictionaries for fixtures and validation tests.
|
|
52
|
+
Use the same schema for happy-path payloads and targeted negative cases.
|
|
53
|
+
|
|
54
|
+
## Key capabilities
|
|
55
|
+
|
|
56
|
+
- **Constraint-driven generation** from type annotations and model metadata.
|
|
57
|
+
- **Minimal invalid cases** targeting one field and a selected violation.
|
|
58
|
+
- **Schema as the single source of truth** for test data.
|
|
59
|
+
- Dataclasses and TypedDicts, with optional Pydantic and attrs integrations.
|
|
60
|
+
- Nested models, collections, reproducible seeds, and field overrides.
|
|
61
|
+
|
|
62
|
+
## Installation
|
|
63
|
+
|
|
64
|
+
Requires Python 3.12 or newer.
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install conformly
|
|
68
|
+
# Optional integrations:
|
|
69
|
+
pip install 'conformly[pydantic]'
|
|
70
|
+
pip install 'conformly[attrs]'
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Quickstart
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from dataclasses import dataclass
|
|
77
|
+
from typing import Annotated
|
|
78
|
+
|
|
79
|
+
from conformly import GreaterOrEqual, MaxLength, MinLength, V, case, path
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass
|
|
83
|
+
class CreateUser:
|
|
84
|
+
username: Annotated[str, MinLength(3), MaxLength(32)]
|
|
85
|
+
age: Annotated[int, GreaterOrEqual(18)]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
payload = case(CreateUser)
|
|
89
|
+
assert 3 <= len(payload["username"]) <= 32
|
|
90
|
+
assert payload["age"] >= 18
|
|
91
|
+
|
|
92
|
+
invalid = case(
|
|
93
|
+
CreateUser, valid=False, strategy=path("age").violate(V.BELOW_MIN)
|
|
94
|
+
)
|
|
95
|
+
assert invalid["age"] < 18
|
|
96
|
+
assert 3 <= len(invalid["username"]) <= 32
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Results are dictionaries, not model instances. Invalid generation targets one
|
|
100
|
+
field; only the selected violation is guaranteed on that target. Defaults and
|
|
101
|
+
overrides are trusted rather than validated. See the
|
|
102
|
+
[invalid generation contract](https://github.com/nashabanov/conformly/blob/main/docs/strategies.md#invalid-generation-contract).
|
|
103
|
+
|
|
104
|
+
## Documentation and examples
|
|
105
|
+
|
|
106
|
+
Start with [Getting started](https://github.com/nashabanov/conformly/blob/main/docs/getting-started.md), browse the
|
|
107
|
+
[documentation index](https://github.com/nashabanov/conformly/blob/main/docs/README.md), or run the [examples](https://github.com/nashabanov/conformly/blob/main/examples/README.md).
|
|
108
|
+
The [API reference](https://github.com/nashabanov/conformly/blob/main/docs/api-reference.md) lists parameters and defaults.
|
|
109
|
+
|
|
110
|
+
## Development and contributing
|
|
111
|
+
|
|
112
|
+
See [CONTRIBUTING.md](https://github.com/nashabanov/conformly/blob/main/CONTRIBUTING.md) for setup and checks, and
|
|
113
|
+
[CHANGELOG.md](https://github.com/nashabanov/conformly/blob/main/CHANGELOG.md) for release notes and migration guidance.
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
MIT — see [LICENSE](https://github.com/nashabanov/conformly/blob/main/LICENSE).
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# conformly
|
|
2
|
+
|
|
3
|
+
[](https://github.com/nashabanov/conformly/actions/workflows/ci.yaml)
|
|
4
|
+
[](https://codecov.io/github/nashabanov/conformly)
|
|
5
|
+
[](https://pypi.org/project/conformly/)
|
|
6
|
+
[](https://pypi.org/project/conformly/)
|
|
7
|
+
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
[](https://github.com/nashabanov/conformly/releases)
|
|
10
|
+
[](http://mypy-lang.org/)
|
|
11
|
+
[](https://github.com/astral-sh/ruff)
|
|
12
|
+
|
|
13
|
+
**Declarative test data generator for Python.**
|
|
14
|
+
|
|
15
|
+
Define constraints once in type annotations — generate both valid and minimal invalid test data automatically.
|
|
16
|
+
No factories, no hardcoded fixtures, no drift when schema changes.
|
|
17
|
+
|
|
18
|
+
Conformly turns typed schemas into dictionaries for fixtures and validation tests.
|
|
19
|
+
Use the same schema for happy-path payloads and targeted negative cases.
|
|
20
|
+
|
|
21
|
+
## Key capabilities
|
|
22
|
+
|
|
23
|
+
- **Constraint-driven generation** from type annotations and model metadata.
|
|
24
|
+
- **Minimal invalid cases** targeting one field and a selected violation.
|
|
25
|
+
- **Schema as the single source of truth** for test data.
|
|
26
|
+
- Dataclasses and TypedDicts, with optional Pydantic and attrs integrations.
|
|
27
|
+
- Nested models, collections, reproducible seeds, and field overrides.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
Requires Python 3.12 or newer.
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install conformly
|
|
35
|
+
# Optional integrations:
|
|
36
|
+
pip install 'conformly[pydantic]'
|
|
37
|
+
pip install 'conformly[attrs]'
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Quickstart
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from dataclasses import dataclass
|
|
44
|
+
from typing import Annotated
|
|
45
|
+
|
|
46
|
+
from conformly import GreaterOrEqual, MaxLength, MinLength, V, case, path
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass
|
|
50
|
+
class CreateUser:
|
|
51
|
+
username: Annotated[str, MinLength(3), MaxLength(32)]
|
|
52
|
+
age: Annotated[int, GreaterOrEqual(18)]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
payload = case(CreateUser)
|
|
56
|
+
assert 3 <= len(payload["username"]) <= 32
|
|
57
|
+
assert payload["age"] >= 18
|
|
58
|
+
|
|
59
|
+
invalid = case(
|
|
60
|
+
CreateUser, valid=False, strategy=path("age").violate(V.BELOW_MIN)
|
|
61
|
+
)
|
|
62
|
+
assert invalid["age"] < 18
|
|
63
|
+
assert 3 <= len(invalid["username"]) <= 32
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Results are dictionaries, not model instances. Invalid generation targets one
|
|
67
|
+
field; only the selected violation is guaranteed on that target. Defaults and
|
|
68
|
+
overrides are trusted rather than validated. See the
|
|
69
|
+
[invalid generation contract](https://github.com/nashabanov/conformly/blob/main/docs/strategies.md#invalid-generation-contract).
|
|
70
|
+
|
|
71
|
+
## Documentation and examples
|
|
72
|
+
|
|
73
|
+
Start with [Getting started](https://github.com/nashabanov/conformly/blob/main/docs/getting-started.md), browse the
|
|
74
|
+
[documentation index](https://github.com/nashabanov/conformly/blob/main/docs/README.md), or run the [examples](https://github.com/nashabanov/conformly/blob/main/examples/README.md).
|
|
75
|
+
The [API reference](https://github.com/nashabanov/conformly/blob/main/docs/api-reference.md) lists parameters and defaults.
|
|
76
|
+
|
|
77
|
+
## Development and contributing
|
|
78
|
+
|
|
79
|
+
See [CONTRIBUTING.md](https://github.com/nashabanov/conformly/blob/main/CONTRIBUTING.md) for setup and checks, and
|
|
80
|
+
[CHANGELOG.md](https://github.com/nashabanov/conformly/blob/main/CHANGELOG.md) for release notes and migration guidance.
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT — see [LICENSE](https://github.com/nashabanov/conformly/blob/main/LICENSE).
|
|
@@ -18,7 +18,7 @@ from ._internal.constraints import (
|
|
|
18
18
|
Pattern,
|
|
19
19
|
UniqueItems,
|
|
20
20
|
)
|
|
21
|
-
from ._internal.fields import Email, IPv4, IPv6, IPvAny
|
|
21
|
+
from ._internal.fields import Email, HttpUrl, IPv4, IPv6, IPvAny, Url
|
|
22
22
|
from ._internal.types import ViolationType as V
|
|
23
23
|
from .exceptions import ConformlyError
|
|
24
24
|
|
|
@@ -27,6 +27,7 @@ __all__ = [
|
|
|
27
27
|
"Email",
|
|
28
28
|
"GreaterOrEqual",
|
|
29
29
|
"GreaterThan",
|
|
30
|
+
"HttpUrl",
|
|
30
31
|
"IPv4",
|
|
31
32
|
"IPv6",
|
|
32
33
|
"IPvAny",
|
|
@@ -40,6 +41,7 @@ __all__ = [
|
|
|
40
41
|
"OneOf",
|
|
41
42
|
"Pattern",
|
|
42
43
|
"UniqueItems",
|
|
44
|
+
"Url",
|
|
43
45
|
"V",
|
|
44
46
|
"case",
|
|
45
47
|
"cases",
|
|
@@ -62,7 +62,13 @@ def plan_tasks(
|
|
|
62
62
|
|
|
63
63
|
if split_by_violations:
|
|
64
64
|
return [
|
|
65
|
-
|
|
65
|
+
plan_violation_task(
|
|
66
|
+
model,
|
|
67
|
+
path,
|
|
68
|
+
allow_type_mismatch,
|
|
69
|
+
allow_structural_violations,
|
|
70
|
+
forced_violation=violation,
|
|
71
|
+
)
|
|
66
72
|
for path in paths
|
|
67
73
|
for violation in plan_violation_task(
|
|
68
74
|
model, path, allow_type_mismatch, allow_structural_violations
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
from dataclasses import dataclass
|
|
2
|
+
from datetime import date, datetime, time, timedelta
|
|
2
3
|
|
|
3
4
|
from .base import Constraint
|
|
4
5
|
|
|
5
6
|
TNum = int | float
|
|
7
|
+
TBound = TNum | datetime | date | time | timedelta
|
|
6
8
|
|
|
7
9
|
|
|
8
10
|
@dataclass(frozen=True, slots=True)
|
|
9
11
|
class GreaterThan(Constraint):
|
|
10
|
-
value:
|
|
12
|
+
value: TBound
|
|
11
13
|
|
|
12
14
|
def __repr__(self) -> str:
|
|
13
15
|
return f"GreaterThan(value={self.value})"
|
|
@@ -15,7 +17,7 @@ class GreaterThan(Constraint):
|
|
|
15
17
|
|
|
16
18
|
@dataclass(frozen=True, slots=True)
|
|
17
19
|
class GreaterOrEqual(Constraint):
|
|
18
|
-
value:
|
|
20
|
+
value: TBound
|
|
19
21
|
|
|
20
22
|
def __repr__(self) -> str:
|
|
21
23
|
return f"GreaterOrEqual(value={self.value})"
|
|
@@ -23,7 +25,7 @@ class GreaterOrEqual(Constraint):
|
|
|
23
25
|
|
|
24
26
|
@dataclass(frozen=True, slots=True)
|
|
25
27
|
class LessThan(Constraint):
|
|
26
|
-
value:
|
|
28
|
+
value: TBound
|
|
27
29
|
|
|
28
30
|
def __repr__(self) -> str:
|
|
29
31
|
return f"LessThan(value={self.value})"
|
|
@@ -31,7 +33,7 @@ class LessThan(Constraint):
|
|
|
31
33
|
|
|
32
34
|
@dataclass(frozen=True, slots=True)
|
|
33
35
|
class LessOrEqual(Constraint):
|
|
34
|
-
value:
|
|
36
|
+
value: TBound
|
|
35
37
|
|
|
36
38
|
def __repr__(self) -> str:
|
|
37
39
|
return f"LessOrEqual(value={self.value})"
|
|
@@ -4,7 +4,7 @@ from .registry import (
|
|
|
4
4
|
SPECIAL_STRINGS,
|
|
5
5
|
SPECIAL_TYPE_TO_KIND,
|
|
6
6
|
)
|
|
7
|
-
from .special_types import Email, IPv4, IPv6, IPvAny, SpecialString
|
|
7
|
+
from .special_types import Email, HttpUrl, IPv4, IPv6, IPvAny, SpecialString, Url
|
|
8
8
|
|
|
9
9
|
__all__ = [
|
|
10
10
|
"SPECIAL_KINDS",
|
|
@@ -12,8 +12,10 @@ __all__ = [
|
|
|
12
12
|
"SPECIAL_STRINGS",
|
|
13
13
|
"SPECIAL_TYPE_TO_KIND",
|
|
14
14
|
"Email",
|
|
15
|
+
"HttpUrl",
|
|
15
16
|
"IPv4",
|
|
16
17
|
"IPv6",
|
|
17
18
|
"IPvAny",
|
|
18
19
|
"SpecialString",
|
|
20
|
+
"Url",
|
|
19
21
|
]
|
|
@@ -5,12 +5,19 @@ from .context import GenerationContext
|
|
|
5
5
|
from .registry import choose_mismatch_kind, get_generator
|
|
6
6
|
|
|
7
7
|
from conformly._internal.planner import PlannedTask
|
|
8
|
+
from conformly._internal.planner.model import CollectionTarget
|
|
8
9
|
from conformly._internal.resolver import (
|
|
9
10
|
ResolvedField,
|
|
10
11
|
ResolvedModel,
|
|
11
12
|
create_minimal_semantic,
|
|
12
13
|
)
|
|
13
|
-
from conformly._internal.resolver.semantics import
|
|
14
|
+
from conformly._internal.resolver.semantics import (
|
|
15
|
+
DictSemantic,
|
|
16
|
+
FieldSemantics,
|
|
17
|
+
ListSemantic,
|
|
18
|
+
ObjectSemantic,
|
|
19
|
+
TupleSemantic,
|
|
20
|
+
)
|
|
14
21
|
from conformly._internal.tracer import Tracer, ValueSource
|
|
15
22
|
from conformly._internal.types import UNSET, FieldKind, FieldPath, ViolationType
|
|
16
23
|
|
|
@@ -43,6 +50,8 @@ def generate_invalid(
|
|
|
43
50
|
violations=task.allowed_violations,
|
|
44
51
|
overrides=overrides,
|
|
45
52
|
tracer=tracer,
|
|
53
|
+
collection_target=task.collection_target,
|
|
54
|
+
collection_steps=task.collection_steps,
|
|
46
55
|
)
|
|
47
56
|
|
|
48
57
|
|
|
@@ -55,6 +64,8 @@ def _built_dict_with_violations(
|
|
|
55
64
|
overrides: dict[FieldPath, Any] | None = None,
|
|
56
65
|
tracer: Tracer | None = None,
|
|
57
66
|
path_names: tuple[str, ...] = (),
|
|
67
|
+
collection_target: CollectionTarget | None = None,
|
|
68
|
+
collection_steps: tuple[tuple[FieldPath, CollectionTarget], ...] = (),
|
|
58
69
|
) -> dict[str, Any]:
|
|
59
70
|
result: dict[str, Any] = {}
|
|
60
71
|
target_index = target_path[depth]
|
|
@@ -88,7 +99,9 @@ def _built_dict_with_violations(
|
|
|
88
99
|
|
|
89
100
|
if tracer:
|
|
90
101
|
tracer.set_target_path(".".join((*path_names, field.name)))
|
|
91
|
-
value = generate_field(
|
|
102
|
+
value = generate_field(
|
|
103
|
+
ctx, field, violations, overrides, tracer, collection_target
|
|
104
|
+
)
|
|
92
105
|
if value is not UNSET:
|
|
93
106
|
result[field.name] = value
|
|
94
107
|
|
|
@@ -100,7 +113,7 @@ def _built_dict_with_violations(
|
|
|
100
113
|
field=field.name,
|
|
101
114
|
)
|
|
102
115
|
|
|
103
|
-
|
|
116
|
+
nested_value = _built_dict_with_violations(
|
|
104
117
|
ctx=ctx,
|
|
105
118
|
model=field.nested_model,
|
|
106
119
|
target_path=target_path,
|
|
@@ -109,8 +122,27 @@ def _built_dict_with_violations(
|
|
|
109
122
|
overrides=overrides,
|
|
110
123
|
tracer=tracer,
|
|
111
124
|
path_names=(*path_names, field.name),
|
|
125
|
+
collection_target=collection_target,
|
|
126
|
+
collection_steps=collection_steps,
|
|
112
127
|
)
|
|
113
128
|
|
|
129
|
+
if isinstance(field.semantic, (ListSemantic, DictSemantic)):
|
|
130
|
+
step = next(
|
|
131
|
+
(t for p, t in collection_steps if p == target_path[: depth + 1]),
|
|
132
|
+
None,
|
|
133
|
+
)
|
|
134
|
+
if step is None:
|
|
135
|
+
raise generation_error(
|
|
136
|
+
"Nested collection requires a planned target",
|
|
137
|
+
code="missing_collection_target",
|
|
138
|
+
)
|
|
139
|
+
container = _generate_nested_collection(
|
|
140
|
+
ctx, field.semantic, step, nested_value
|
|
141
|
+
)
|
|
142
|
+
result[field.name] = container
|
|
143
|
+
else:
|
|
144
|
+
result[field.name] = nested_value
|
|
145
|
+
|
|
114
146
|
else:
|
|
115
147
|
key, value = _generate_extra_field_value(ctx)
|
|
116
148
|
suffix = 1
|
|
@@ -137,6 +169,7 @@ def generate_field(
|
|
|
137
169
|
violations: tuple[ViolationType, ...] | None = None,
|
|
138
170
|
overrides: dict[FieldPath, Any] | None = None,
|
|
139
171
|
tracer: Tracer | None = None,
|
|
172
|
+
collection_target: CollectionTarget | None = None,
|
|
140
173
|
) -> Any:
|
|
141
174
|
if violations is None:
|
|
142
175
|
if overrides and field.path in overrides:
|
|
@@ -160,7 +193,7 @@ def generate_field(
|
|
|
160
193
|
return value
|
|
161
194
|
|
|
162
195
|
if field.nested_model and not isinstance(
|
|
163
|
-
field.semantic, (ListSemantic, TupleSemantic)
|
|
196
|
+
field.semantic, (ListSemantic, DictSemantic, TupleSemantic)
|
|
164
197
|
):
|
|
165
198
|
return generate_valid(ctx, field.nested_model, overrides)
|
|
166
199
|
|
|
@@ -170,31 +203,50 @@ def generate_field(
|
|
|
170
203
|
tracer.set_violation(violation)
|
|
171
204
|
tracer.set_value_source(ValueSource.GENERATED)
|
|
172
205
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
mismatch_semantic = create_minimal_semantic(mismatch_kind)
|
|
176
|
-
value = get_generator(mismatch_kind).generate_value(
|
|
177
|
-
ctx, mismatch_semantic, None
|
|
178
|
-
)
|
|
179
|
-
if tracer:
|
|
180
|
-
tracer.set_generated_value(value)
|
|
181
|
-
return value
|
|
182
|
-
|
|
183
|
-
if violation == ViolationType.MISSING_FIELD:
|
|
184
|
-
if tracer:
|
|
185
|
-
tracer.set_generated_value(UNSET)
|
|
186
|
-
return UNSET
|
|
187
|
-
|
|
188
|
-
value = get_generator(field.semantic.kind).generate_value(
|
|
189
|
-
ctx, field.semantic, violation
|
|
206
|
+
value = generate_semantic_value(
|
|
207
|
+
ctx, field.semantic, field.nested_model, violations, collection_target
|
|
190
208
|
)
|
|
191
|
-
|
|
192
209
|
if tracer:
|
|
193
210
|
tracer.set_generated_value(value)
|
|
194
|
-
|
|
195
211
|
return value
|
|
196
212
|
|
|
197
213
|
|
|
214
|
+
def generate_semantic_value(
|
|
215
|
+
ctx: GenerationContext,
|
|
216
|
+
semantic: FieldSemantics,
|
|
217
|
+
nested_model: ResolvedModel | None,
|
|
218
|
+
violations: tuple[ViolationType, ...] | None,
|
|
219
|
+
collection_target: CollectionTarget | None = None,
|
|
220
|
+
) -> Any:
|
|
221
|
+
"""Generate directly from resolved semantics, without synthetic field models."""
|
|
222
|
+
violation = _choose_violation(violations)
|
|
223
|
+
if violation == ViolationType.TYPE_MISMATCH:
|
|
224
|
+
mismatch_kind = choose_mismatch_kind(semantic.kind)
|
|
225
|
+
return get_generator(mismatch_kind).generate_value(
|
|
226
|
+
ctx, create_minimal_semantic(mismatch_kind), None
|
|
227
|
+
)
|
|
228
|
+
if violation == ViolationType.MISSING_FIELD:
|
|
229
|
+
return UNSET
|
|
230
|
+
if nested_model is not None and isinstance(semantic, ObjectSemantic):
|
|
231
|
+
if violation is not None:
|
|
232
|
+
raise generation_error(
|
|
233
|
+
"Nested model violation requires a field target",
|
|
234
|
+
code="missing_nested_target",
|
|
235
|
+
)
|
|
236
|
+
return generate_valid(ctx, nested_model)
|
|
237
|
+
from .types import dictionaries, list as lists, tuple as tuples
|
|
238
|
+
|
|
239
|
+
if isinstance(semantic, ListSemantic):
|
|
240
|
+
return lists.generate_value(ctx, semantic, violation, target=collection_target)
|
|
241
|
+
if isinstance(semantic, DictSemantic):
|
|
242
|
+
return dictionaries.generate_value(
|
|
243
|
+
ctx, semantic, violation, target=collection_target
|
|
244
|
+
)
|
|
245
|
+
if isinstance(semantic, TupleSemantic):
|
|
246
|
+
return tuples.generate_value(ctx, semantic, violation, target=collection_target)
|
|
247
|
+
return get_generator(semantic.kind).generate_value(ctx, semantic, violation)
|
|
248
|
+
|
|
249
|
+
|
|
198
250
|
def _choose_violation(
|
|
199
251
|
violations: tuple[ViolationType, ...] | None,
|
|
200
252
|
) -> ViolationType | None:
|
|
@@ -209,3 +261,28 @@ def _generate_extra_field_value(ctx: GenerationContext) -> tuple[str, Any]:
|
|
|
209
261
|
value = get_generator(semantic.kind).generate_value(ctx, semantic, None)
|
|
210
262
|
key = "extra"
|
|
211
263
|
return (key, value)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def _generate_nested_collection(
|
|
267
|
+
ctx: GenerationContext,
|
|
268
|
+
semantic: ListSemantic | DictSemantic,
|
|
269
|
+
target: CollectionTarget,
|
|
270
|
+
nested_value: dict[str, Any],
|
|
271
|
+
) -> Any:
|
|
272
|
+
from .types._utils import calculate_valid_collection_range, unique_collection_range
|
|
273
|
+
from .types.dictionaries import _generate_dict_of_length
|
|
274
|
+
from .types.list import _generate_list_of_length
|
|
275
|
+
|
|
276
|
+
lower, upper = calculate_valid_collection_range(semantic)
|
|
277
|
+
if isinstance(semantic, DictSemantic):
|
|
278
|
+
lower, upper = unique_collection_range(
|
|
279
|
+
lower, upper, semantic.key_semantic, collection="dict"
|
|
280
|
+
)
|
|
281
|
+
length = ctx.rng.randint(lower, upper)
|
|
282
|
+
return _generate_dict_of_length(
|
|
283
|
+
ctx, semantic, length, target=target, target_value=nested_value
|
|
284
|
+
)
|
|
285
|
+
length = ctx.rng.randint(lower, upper)
|
|
286
|
+
result = _generate_list_of_length(ctx, semantic, length - 1)
|
|
287
|
+
result.insert(target.index, nested_value)
|
|
288
|
+
return result
|
|
@@ -2,6 +2,7 @@ from ._errors import generation_error
|
|
|
2
2
|
from .protocol import TypeGeneratorProtocol
|
|
3
3
|
from .types import (
|
|
4
4
|
boolean,
|
|
5
|
+
bytes,
|
|
5
6
|
dictionaries,
|
|
6
7
|
email,
|
|
7
8
|
enum,
|
|
@@ -12,6 +13,7 @@ from .types import (
|
|
|
12
13
|
ipvany,
|
|
13
14
|
list,
|
|
14
15
|
string,
|
|
16
|
+
temporal,
|
|
15
17
|
tuple,
|
|
16
18
|
url,
|
|
17
19
|
uuid,
|
|
@@ -21,6 +23,7 @@ from conformly._internal.types import FieldKind
|
|
|
21
23
|
|
|
22
24
|
_GENERATORS: dict[FieldKind, TypeGeneratorProtocol] = {
|
|
23
25
|
FieldKind.STRING: string,
|
|
26
|
+
FieldKind.BYTES: bytes,
|
|
24
27
|
FieldKind.BOOLEAN: boolean,
|
|
25
28
|
FieldKind.FLOAT: float,
|
|
26
29
|
FieldKind.INTEGER: integer,
|
|
@@ -33,12 +36,17 @@ _GENERATORS: dict[FieldKind, TypeGeneratorProtocol] = {
|
|
|
33
36
|
FieldKind.DICT: dictionaries,
|
|
34
37
|
FieldKind.TUPLE: tuple,
|
|
35
38
|
FieldKind.UUID: uuid,
|
|
39
|
+
FieldKind.DATETIME: temporal,
|
|
40
|
+
FieldKind.DATE: temporal,
|
|
41
|
+
FieldKind.TIME: temporal,
|
|
42
|
+
FieldKind.TIMEDELTA: temporal,
|
|
36
43
|
FieldKind.URL: url,
|
|
37
44
|
FieldKind.HTTPURL: url,
|
|
38
45
|
}
|
|
39
46
|
|
|
40
47
|
_MISMATCH_MAPPING: dict[FieldKind, FieldKind] = {
|
|
41
48
|
FieldKind.STRING: FieldKind.INTEGER,
|
|
49
|
+
FieldKind.BYTES: FieldKind.INTEGER,
|
|
42
50
|
FieldKind.BOOLEAN: FieldKind.STRING,
|
|
43
51
|
FieldKind.INTEGER: FieldKind.STRING,
|
|
44
52
|
FieldKind.FLOAT: FieldKind.STRING,
|
|
@@ -51,6 +59,10 @@ _MISMATCH_MAPPING: dict[FieldKind, FieldKind] = {
|
|
|
51
59
|
FieldKind.LIST: FieldKind.INTEGER,
|
|
52
60
|
FieldKind.DICT: FieldKind.STRING,
|
|
53
61
|
FieldKind.UUID: FieldKind.FLOAT,
|
|
62
|
+
FieldKind.DATETIME: FieldKind.STRING,
|
|
63
|
+
FieldKind.DATE: FieldKind.STRING,
|
|
64
|
+
FieldKind.TIME: FieldKind.STRING,
|
|
65
|
+
FieldKind.TIMEDELTA: FieldKind.STRING,
|
|
54
66
|
FieldKind.URL: FieldKind.FLOAT,
|
|
55
67
|
FieldKind.HTTPURL: FieldKind.INTEGER,
|
|
56
68
|
FieldKind.TUPLE: FieldKind.INTEGER,
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from .._errors import generation_error
|
|
4
|
+
from ..context import GenerationContext
|
|
5
|
+
|
|
6
|
+
from conformly._internal.resolver import ResolvedModel
|
|
7
|
+
from conformly._internal.resolver.semantics import (
|
|
8
|
+
DictSemantic,
|
|
9
|
+
FieldSemantics,
|
|
10
|
+
ListSemantic,
|
|
11
|
+
TupleSemantic,
|
|
12
|
+
)
|
|
13
|
+
from conformly._internal.resolver.semantics._domains import finite_cardinality
|
|
14
|
+
from conformly._internal.types import ViolationType
|
|
15
|
+
from conformly.exceptions import GenerationError
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def calculate_valid_collection_range(
|
|
19
|
+
semantic: ListSemantic | DictSemantic | TupleSemantic,
|
|
20
|
+
) -> tuple[int, int]:
|
|
21
|
+
"""Choose a small default range without overriding declared size limits."""
|
|
22
|
+
if semantic.length_range is None:
|
|
23
|
+
return 1, 3
|
|
24
|
+
lower = semantic.length_range.min_length
|
|
25
|
+
upper = semantic.length_range.max_length
|
|
26
|
+
if upper == 0:
|
|
27
|
+
return lower, 0
|
|
28
|
+
lower = max(1, lower)
|
|
29
|
+
return lower, upper if upper is not None else max(3, lower)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def unique_collection_range(
|
|
33
|
+
lower: int,
|
|
34
|
+
upper: int,
|
|
35
|
+
semantic: FieldSemantics,
|
|
36
|
+
*,
|
|
37
|
+
collection: str,
|
|
38
|
+
extra_values: int = 0,
|
|
39
|
+
) -> tuple[int, int]:
|
|
40
|
+
cardinality = finite_cardinality(semantic)
|
|
41
|
+
if cardinality is not None:
|
|
42
|
+
cardinality += extra_values
|
|
43
|
+
if lower > cardinality:
|
|
44
|
+
raise generation_error(
|
|
45
|
+
"Collection size exceeds the number of distinct valid values",
|
|
46
|
+
code="impossible_collection_size",
|
|
47
|
+
collection=collection,
|
|
48
|
+
requested_size=lower,
|
|
49
|
+
cardinality=cardinality,
|
|
50
|
+
)
|
|
51
|
+
upper = min(upper, cardinality)
|
|
52
|
+
return lower, upper
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def collection_retry_error(
|
|
56
|
+
*, collection: str, requested_size: int, generated_size: int, attempts: int
|
|
57
|
+
) -> GenerationError:
|
|
58
|
+
return generation_error(
|
|
59
|
+
"Could not fill collection with distinct valid values",
|
|
60
|
+
code="collection_retry_exhausted",
|
|
61
|
+
collection=collection,
|
|
62
|
+
requested_size=requested_size,
|
|
63
|
+
generated_size=generated_size,
|
|
64
|
+
attempts=attempts,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def is_hashable(value: Any) -> bool:
|
|
69
|
+
try:
|
|
70
|
+
hash(value)
|
|
71
|
+
return True
|
|
72
|
+
except TypeError:
|
|
73
|
+
return False
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def generate_collection_item(
|
|
77
|
+
ctx: GenerationContext,
|
|
78
|
+
item_semantic: FieldSemantics,
|
|
79
|
+
item_nested_model: ResolvedModel | None,
|
|
80
|
+
violations: tuple[ViolationType, ...] | None,
|
|
81
|
+
) -> Any:
|
|
82
|
+
from ..orchestration import generate_semantic_value
|
|
83
|
+
|
|
84
|
+
return generate_semantic_value(ctx, item_semantic, item_nested_model, violations)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from .._errors import generation_error
|
|
2
|
+
from ..context import GenerationContext
|
|
3
|
+
|
|
4
|
+
from conformly._internal.resolver.semantics import BytesSemantic
|
|
5
|
+
from conformly._internal.types import ViolationType
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def generate_value(
|
|
9
|
+
ctx: GenerationContext, semantic: BytesSemantic, violation: ViolationType | None
|
|
10
|
+
) -> bytes:
|
|
11
|
+
lower = semantic.length_range.min_length
|
|
12
|
+
upper = semantic.length_range.max_length
|
|
13
|
+
if violation is None:
|
|
14
|
+
length = ctx.rng.randint(lower, upper if upper is not None else lower + 50)
|
|
15
|
+
elif violation == ViolationType.TOO_SHORT and lower > 0:
|
|
16
|
+
length = lower - 1
|
|
17
|
+
elif violation == ViolationType.TOO_LONG and upper is not None:
|
|
18
|
+
length = upper + 1
|
|
19
|
+
else:
|
|
20
|
+
raise generation_error(
|
|
21
|
+
"Unsupported bytes violation",
|
|
22
|
+
code="unsupported_violation",
|
|
23
|
+
violation=violation.value,
|
|
24
|
+
)
|
|
25
|
+
return ctx.rng.randbytes(length)
|