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.4.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: lilvali-0.1.4.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.4.tar.gz
Algorithm Hash digest
SHA256 17c9e5179bb8044a9c3180b14028acaee88ddbec31adddda2a9eeaf5edd93495
MD5 0ad1707349429cde2f34494df1fd34f7
BLAKE2b-256 520f64330a5e4fcfff2d311271f6726f13ea67c9a78a213c076200f0833bb2b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lilvali-0.1.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cc1772c789c5e9de2506714cece239e3aa6f1af1b6e6f24abfec97fd922e4bba
MD5 069603a939c01e21060f83cb622a00eb
BLAKE2b-256 c3269a18a94a9f8ad7790efcb2f1facf1ef76f26253bfceb25bcc788b32a45ca

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