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 and classes with annotated __init__ methods.

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"

See the demo folder as well.

Tests

$ ./test.sh
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.5.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

lilvali-0.1.5-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lilvali-0.1.5.tar.gz
  • Upload date:
  • Size: 13.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.5.tar.gz
Algorithm Hash digest
SHA256 b6314153b8a5930c66a95796dd6d303cdfe9abf3c744db1e7a61ec1c7eaa70ec
MD5 e769051ab77012bafcda1be79a7db792
BLAKE2b-256 a54928d48032c7ba4574678c05c4d3c93ac1c26d339fd988d67fbda941c5c52c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lilvali-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 18.3 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ea4c512e2fcf47127ab2db75a57ade0f722fcbf3ade78fa12efbc992fb43ccc8
MD5 827a220dda75d84750ea7eb61f2094f1
BLAKE2b-256 ca8f8eede2afbe9e6845e2d41f80adf25e39ea53dc2122d329fc862aa0d8046f

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