An open API service indexing awesome lists of open source software.

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"

Awesome Lists containing this project

README

          

# Light Types

[![image](https://img.shields.io/pypi/v/light-types.svg)](https://pypi.python.org/pypi/light-types)
[![codecov](https://codecov.io/gh/likeinlife/light-types/graph/badge.svg?token=7QUSPNC4CQ)](https://codecov.io/gh/likeinlife/light-types)
[![Rye](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/rye/main/artwork/badge.json)](https://rye.astral.sh)
[![image](https://img.shields.io/pypi/l/light-types.svg)](https://github.com/likeinlife/light-types/blob/main/LICENSE)
Mypy checked
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![image](https://img.shields.io/pypi/pyversions/light-types.svg)](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`