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.1.tar.gz
(8.8 kB
view hashes)
Built Distribution
Close
Hashes for light_types-1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5f6465eb87b8ac3d3e49637a1a6b710899c86d6750aa5c7312cc7e2621a2579 |
|
MD5 | c443df8a8ccff9cde931643ed87e71e4 |
|
BLAKE2b-256 | fa855006db961cf6e7f9755de2bbbd5263cb719cf3120b83980509ffc62ab57f |