Parse, don't validate
Project description
Light Types
"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
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
light_types-1.2.2.tar.gz
(8.8 kB
view details)
Built Distribution
File details
Details for the file light_types-1.2.2.tar.gz
.
File metadata
- Download URL: light_types-1.2.2.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | be43aafc11b775331235cf7858959b10438f9ef911870a590b61622f7c9aa08d |
|
MD5 | 2dcb01144b766f087497fd3768ef9f51 |
|
BLAKE2b-256 | 60fe27a4a4fc9fdcffe67897a7d6abb56f6344cc74ffdc85c1829c90dc02ad9b |
File details
Details for the file light_types-1.2.2-py3-none-any.whl
.
File metadata
- Download URL: light_types-1.2.2-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52b12ccc620a461081303aa3402b8c08f6564a1916a13ad6bcfcd05eb58d82ff |
|
MD5 | d24f50acdf48a84cb291fd6d34968104 |
|
BLAKE2b-256 | 659899acdf995697b12350092ab8e3c07e4130e5374db67f8d6dded7474e2a0f |