Skip to main content

Tiny validation library for Python

Project description

lilVali

A small Python 3.12 validation experiment for playing with PEP 695.

Supports most basic typing constructs including Generics. Also supports dataclasses through a special metaclass.

Install

pip install lilvali

Usage

from lilvali import validate, validate
from lilvali.errors import *

Simple examples

from lilvali import validate, validate
from lilvali.errors import *


@validate
def add[T: (int, float)](x: int, y: T) -> int | float:
    return x + y


def main():
    print(f"{add(1, 2)=}")
    print(f"{add(1, 2.0)=}")

    try:
        print(f"{add(1.0, 2)=}")
    except ValidationError as e:
        print(f"{e=}")
    else:
        raise RuntimeError("Expected ValidationError")


if __name__ == "__main__":
    main()
@validate
@dataclass
class SomeClass:
    x: int
    y: str = field(default="hello")

    @validator
    def _x(value):
        if value is None or value < 0:
            raise ValidationError

    @validator
    def _y(value) -> bool:
        return value == "hello"


@validate
class NotADC:
    def __init__(self, x: int, y: str):
        self.x = x
        self.y = y

    @validator
    def _x(value):
        if value is None or value < 0:
            raise ValidationError

    @validator
    def _y(value) -> bool:
        return value == "hello"

Tests

Running tests with coverage
.........................
----------------------------------------------------------------------
Ran 25 tests in 0.006s

OK
Name                           Stmts   Miss  Cover   Missing
------------------------------------------------------------
lilvali/__init__.py                2      0   100%
lilvali/binding.py               167      0   100%
lilvali/errors.py                  6      0   100%
lilvali/validate.py               80      0   100%
tests/test_tiny_validate.py      195      0   100%
tests/test_validate_types.py     124      0   100%
------------------------------------------------------------
TOTAL                            574      0   100%

TODO

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lilvali-0.1.3.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

lilvali-0.1.3-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file lilvali-0.1.3.tar.gz.

File metadata

  • Download URL: lilvali-0.1.3.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for lilvali-0.1.3.tar.gz
Algorithm Hash digest
SHA256 41b5270df34df5541b17e59ce6179f32a58bf717baa8a996dc55062d2e816f2d
MD5 263fdba33cbb97257e73f71fd63ab44d
BLAKE2b-256 34eb031496a1bccfca2d79dda3142d689246f82c8b5785c7774d524a48238282

See more details on using hashes here.

File details

Details for the file lilvali-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: lilvali-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for lilvali-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a3f58ad7c66de876bfa2c9a10a28b4cd535b7bf7cdb8131d253d94fcbad76780
MD5 f43720151aeab9598888acd1871f3956
BLAKE2b-256 8f7e82fd6efd4eff35d8350bce3f57f7ea54522ad665012181aa82853dbf2479

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page