Skip to main content

Light Types

image codecov Rye image Mypy checked Ruff image

"Parse, don't validate"

Compatible with PydanticV2

Inspired by Phantom types

Examples

Sample

from light_types import LightType


class StartsWithString(str, LightType):
    @classmethod
    def validate(cls, value: str) -> bool:
        return value.startswith("String")

With Pydantic

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

from light_types import QLightType, NumericQ

class NumericBetween5And10(int, QLightType):
    validator = (NumericQ() > 5).custom(lambda n: n < 10)
from light_types import QLightType, StringQ

class StartsWithString(str, QLightType):
    validator = StringQ().startswith("String")
from light_types import QLightType, StringQ

class StringWith2O(str, QLightType):
    validator = StringQ().startswith("String") & StringQ().custom(lambda s: s.count("o") >= 2)
from light_types import QLightType, StringQ

class StringWith2O(str, QLightType):
    validator = StringQ().startswith("String") | ~StringQ().custom(lambda s: s.count("o") >= 2)
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

Metadata

Release files for light-types 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for light-types 1.2.2
File Size Uploaded
light_types-1.2.2.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for light-types 1.2.2
File Interpreter ABI Platform
light_types-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 18.7 kB

Release files / light_types-1.2.2.tar.gz

Download URL light_types-1.2.2.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
be43aafc11b775331235cf7858959b10438f9ef911870a590b61622f7c9aa08d
BLAKE2b-256 checksum
How to use checksums
60fe27a4a4fc9fdcffe67897a7d6abb56f6344cc74ffdc85c1829c90dc02ad9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release files / light_types-1.2.2-py3-none-any.whl

Download URL light_types-1.2.2-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52b12ccc620a461081303aa3402b8c08f6564a1916a13ad6bcfcd05eb58d82ff
BLAKE2b-256 checksum
How to use checksums
659899acdf995697b12350092ab8e3c07e4130e5374db67f8d6dded7474e2a0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page