obligate 1.0.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.
- obligate-1.0.0/LICENSE +33 -0
- obligate-1.0.0/PKG-INFO +48 -0
- obligate-1.0.0/README.md +31 -0
- obligate-1.0.0/pyproject.toml +33 -0
- obligate-1.0.0/src/obligate/__init__.py +1 -0
- obligate-1.0.0/src/obligate/boolvalidator.py +60 -0
obligate-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
BSD 4-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, Samuel Jones
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. All advertising materials mentioning features or use of this software must
|
|
17
|
+
display the following acknowledgement:
|
|
18
|
+
This product includes software developed by Obligate.
|
|
19
|
+
|
|
20
|
+
4. Neither the name of the copyright holder nor the names of its
|
|
21
|
+
contributors may be used to endorse or promote products derived from
|
|
22
|
+
this software without specific prior written permission.
|
|
23
|
+
|
|
24
|
+
THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS OR
|
|
25
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
26
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
27
|
+
EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
28
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
29
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
30
|
+
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
31
|
+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
33
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
obligate-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: obligate
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Utilities for enforcing contracts
|
|
5
|
+
License-Expression: BSD-4-Clause
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: contracts,runtime-validation,precondition,postcondition,validation
|
|
8
|
+
Author: Samuel Jones
|
|
9
|
+
Author-email: sjones.nova@gmail.com
|
|
10
|
+
Requires-Python: >=3.14
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Topic :: Software Development :: Testing
|
|
14
|
+
Project-URL: Repository, https://github.com/sjones5516/obligate
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# Obligate
|
|
18
|
+
|
|
19
|
+
Obligate is a Python library for enforcing contracts.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Use the package manager pip to install obligate.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install obligate
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import obligate
|
|
33
|
+
|
|
34
|
+
@obligate.BoolValidator.pre(
|
|
35
|
+
lambda x : x >= 0,
|
|
36
|
+
lambda x: ValueError(f"Expected value greater than 0. Got {x}."),
|
|
37
|
+
)
|
|
38
|
+
def sqrt(x: float) -> float:
|
|
39
|
+
return x ** 0.5
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Contributing
|
|
43
|
+
|
|
44
|
+
Pull requests are welcome. For major changes, please open an issue first
|
|
45
|
+
to discuss what you would like to change.
|
|
46
|
+
|
|
47
|
+
Please make sure to update tests as appropriate.
|
|
48
|
+
|
obligate-1.0.0/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Obligate
|
|
2
|
+
|
|
3
|
+
Obligate is a Python library for enforcing contracts.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Use the package manager pip to install obligate.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install obligate
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import obligate
|
|
17
|
+
|
|
18
|
+
@obligate.BoolValidator.pre(
|
|
19
|
+
lambda x : x >= 0,
|
|
20
|
+
lambda x: ValueError(f"Expected value greater than 0. Got {x}."),
|
|
21
|
+
)
|
|
22
|
+
def sqrt(x: float) -> float:
|
|
23
|
+
return x ** 0.5
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Contributing
|
|
27
|
+
|
|
28
|
+
Pull requests are welcome. For major changes, please open an issue first
|
|
29
|
+
to discuss what you would like to change.
|
|
30
|
+
|
|
31
|
+
Please make sure to update tests as appropriate.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "obligate"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Utilities for enforcing contracts"
|
|
5
|
+
license = "BSD-4-Clause"
|
|
6
|
+
authors = [
|
|
7
|
+
{name = "Samuel Jones",email = "sjones.nova@gmail.com"}
|
|
8
|
+
]
|
|
9
|
+
keywords = ["contracts", "runtime-validation", "precondition", "postcondition", "validation"]
|
|
10
|
+
dynamic = [ "classifiers" ]
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
requires-python = ">=3.14"
|
|
13
|
+
dependencies = [
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
repository = "https://github.com/sjones5516/obligate"
|
|
18
|
+
|
|
19
|
+
[tool.poetry]
|
|
20
|
+
packages = [{include = "obligate", from = "src"}]
|
|
21
|
+
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Topic :: Software Development :: Testing"
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[build-system]
|
|
27
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
28
|
+
build-backend = "poetry.core.masonry.api"
|
|
29
|
+
|
|
30
|
+
[dependency-groups]
|
|
31
|
+
dev = [
|
|
32
|
+
"pytest (>=9.1.1,<10.0.0)"
|
|
33
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from src.obligate.boolvalidator import BoolValidator
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
from typing import Callable
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class BoolValidator:
|
|
6
|
+
@staticmethod
|
|
7
|
+
def pre[**P, R](
|
|
8
|
+
condition: Callable[P, bool],
|
|
9
|
+
error: Callable[P, Exception],
|
|
10
|
+
) -> Callable[[Callable[P, R]], Callable[P, R]]:
|
|
11
|
+
"""Guard a function with a precondition, raising a dynamically built exception.
|
|
12
|
+
|
|
13
|
+
``error`` is called with the *same* arguments that were passed to the
|
|
14
|
+
wrapped function, letting the exception message embed the actual
|
|
15
|
+
values that failed validation.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
condition: Returns ``True`` if the arguments are valid.
|
|
19
|
+
error: Called as ``error(*args, **kwargs)`` when ``condition``
|
|
20
|
+
fails; must return an ``Exception`` instance to raise.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def decorator(func: Callable[P, R]) -> Callable[P, R]:
|
|
24
|
+
@functools.wraps(func)
|
|
25
|
+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
|
26
|
+
if not condition(*args, **kwargs):
|
|
27
|
+
raise error(*args, **kwargs)
|
|
28
|
+
return func(*args, **kwargs)
|
|
29
|
+
|
|
30
|
+
return wrapper
|
|
31
|
+
|
|
32
|
+
return decorator
|
|
33
|
+
|
|
34
|
+
@staticmethod
|
|
35
|
+
def post[**P, R](
|
|
36
|
+
condition: Callable[[R], bool],
|
|
37
|
+
error: Callable[[R], Exception],
|
|
38
|
+
) -> Callable[[Callable[P, R]], Callable[P, R]]:
|
|
39
|
+
"""Guard a function with a postcondition, raising a dynamically built exception.
|
|
40
|
+
|
|
41
|
+
``error`` is called with the wrapped function's return value, letting
|
|
42
|
+
the exception message embed the actual result that failed validation.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
condition: Returns ``True`` if the result is valid.
|
|
46
|
+
error: Called as ``error(result)`` when ``condition`` fails; must
|
|
47
|
+
return an ``Exception`` instance to raise.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def decorator(func: Callable[P, R]) -> Callable[P, R]:
|
|
51
|
+
@functools.wraps(func)
|
|
52
|
+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
|
53
|
+
result = func(*args, **kwargs)
|
|
54
|
+
if not condition(result):
|
|
55
|
+
raise error(result)
|
|
56
|
+
return result
|
|
57
|
+
|
|
58
|
+
return wrapper
|
|
59
|
+
|
|
60
|
+
return decorator
|