https://github.com/likeinlife/light-types
Create types that respect the invariant. "Parse, don't validate"
https://github.com/likeinlife/light-types
phantom-types python3 types value-object
Last synced: 4 months ago
JSON representation
Create types that respect the invariant. "Parse, don't validate"
- Host: GitHub
- URL: https://github.com/likeinlife/light-types
- Owner: likeinlife
- License: mit
- Created: 2024-07-26T15:28:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-16T20:36:10.000Z (over 1 year ago)
- Last Synced: 2025-09-11T05:14:19.425Z (5 months ago)
- Topics: phantom-types, python3, types, value-object
- Language: Python
- Homepage:
- Size: 38.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Light Types
[](https://pypi.python.org/pypi/light-types)
[](https://codecov.io/gh/likeinlife/light-types)
[](https://rye.astral.sh)
[](https://github.com/likeinlife/light-types/blob/main/LICENSE)
[](https://github.com/astral-sh/ruff)
[](https://pypi.python.org/pypi/light-types)
"Parse, don't validate"
Compatible with PydanticV2
Inspired by [Phantom types](https://github.com/antonagestam/phantom-types/)
# Examples
## Sample
```python
from light_types import LightType
class StartsWithString(str, LightType):
@classmethod
def validate(cls, value: str) -> bool:
return value.startswith("String")
```
## With Pydantic
```python
from light_types import LightType
class StartsWithString(str, LightType):
@classmethod
def validate(cls, value: str) -> bool:
return value.startswith("String")
class MyModel(BaseModel):
value: StartsWithString
assert TypeAdapter(MyModel).validate_python({"value": "StringOk"})
```
## QLightType
```python
from light_types import QLightType, NumericQ
class NumericBetween5And10(int, QLightType):
validator = (NumericQ() > 5).custom(lambda n: n < 10)
```
```python
from light_types import QLightType, StringQ
class StartsWithString(str, QLightType):
validator = StringQ().startswith("String")
```
```python
from light_types import QLightType, StringQ
class StringWith2O(str, QLightType):
validator = StringQ().startswith("String") & StringQ().custom(lambda s: s.count("o") >= 2)
```
```python
from light_types import QLightType, StringQ
class StringWith2O(str, QLightType):
validator = StringQ().startswith("String") | ~StringQ().custom(lambda s: s.count("o") >= 2)
```
```python
from light_types import QLightType, StringQ, LengthQ
class StringWith2O(str, QLightType):
validator = StringQ().startswith("foo") & (LengthQ[str]() > 5)
```
# Tests, linting, formatting
- `rye test | lint | fmt`