Types Module
The types module provides a comprehensive set of classes and utilities for working with various data types, including version strings, numerical types, identifiers, and serialization formats. It focuses on enforcing validation rules and providing robust error handling.
Key Features
Version validation and parsing (SemVer, CalVer, Date Versioning)
Validated numeric types with constraints
UUID and ULID handling with versioning
Scientific number validation
Serialization support (JSON, YAML, TOML)
Type hints and generic utilities
Version Types
Version Base Classes
- class VersionValidatorMixin
A mixin that provides version validation functionality.
Version Implementation Classes
- class SemVersion
Semantic Versioning implementation (MAJOR.MINOR.PATCH[-TAG]).
- class DateVersion
Date-based versioning implementation (YYYY.MM.DD[-TAG]).
- class CalVersion
Calendar versioning implementation (YY.MM[-TAG]).
- class MajorMinorVersion
Simple major.minor versioning without patch numbers.
Numeric Types
Base Classes
Integer Types
- class ValidatedInt(ValidatedNumber[int])
Base class for validated integer types.
- class PositiveInt(ValidatedInt)
Integer type that must be positive.
- class NegativeInt(ValidatedInt)
Integer type that must be negative.
- class UnsignedInt(ValidatedInt)
Integer type that must be non-negative.
- class BigInt
Class for handling large integers with architecture-aware bounds.
- Parameters:
value – Integer value
strict – Enable strict validation
context – Validation context (“Positive”, “Negative”, “Unsigned”)
Float Types
- class ValidatedFloat(ValidatedNumber[float])
Base class for validated float types.
- class PositiveFloat(ValidatedFloat)
Float type that must be positive.
- class NegativeFloat(ValidatedFloat)
Float type that must be negative.
- class UnsignedFloat(ValidatedFloat)
Float type that must be non-negative.
- class BigDecimal
Class for handling large decimal numbers.
- Parameters:
value – Decimal value
strict – Enable strict validation
context – Validation context (“Positive”, “Negative”, “Unsigned”)
stop_warnings – Suppress warnings
Scientific Numbers
Special Number Types
- class NaN
Not-a-Number type with validation.
- PositiveInfinity = Infinity(float("inf"))
- NegativeInfinity = Infinity(float("-inf"))
Identifier Types
UUID Types
- class UUIDType
Base class for UUID handling.
- class StrUUIDType(UUIDType)
String-based UUID with validation.
- class IntUUIDType(UUIDType)
Integer-based UUID with validation.
Versioned UUID Types
- class UUIDVersionMixin
Mixin for versioned UUID support.
- class UUIDV1
UUID version 1 implementation (time-based).
- class UUIDV2
UUID version 2 implementation (DCE Security).
- class UUIDV3
UUID version 3 implementation (MD5 hash-based).
- class UUIDV4
UUID version 4 implementation (random).
- class UUIDV5
UUID version 5 implementation (SHA-1 hash-based).
- class StrUUIDV1
String-based UUID version 1.
- class StrUUIDV2
String-based UUID version 2.
- class StrUUIDV3
String-based UUID version 3.
- class StrUUIDV4
String-based UUID version 4.
- class StrUUIDV5
String-based UUID version 5.
- class IntUUIDV1
Integer-based UUID version 1.
- class IntUUIDV2
Integer-based UUID version 2.
- class IntUUIDV3
Integer-based UUID version 3.
- class IntUUIDV4
Integer-based UUID version 4.
- class IntUUIDV5
Integer-based UUID version 5.
ULID Types
- class ULIDType
Base class for ULID handling.
- class StrULIDType(ULIDType)
String-based ULID with validation.
- class IntULIDType(ULIDType)
Integer-based ULID with validation.
Serialization Types
- class JsonMixin
Mixin for JSON serialization support.
- classmethod to_json(cls, value)
- classmethod from_json(cls, value)
Type Aliases
Examples
Version Handling:
# Create a semantic version
version = SemVersion("1.2.3-beta")
# Create a calendar version
cal_version = CalVersion("23.04")
Numeric Validation:
# Create a positive integer
pos_int = PositiveInt(42)
# Create a big integer with constraints
big_int = BigInt(1000000, strict=True, context="Positive")
# Create a scientific number
sci_num = ScientificNumber("1.23e-4")
UUID Handling:
# Create a string UUID
uuid_str = StrUUIDType("550e8400-e29b-41d4-a716-446655440000")
# Create a version 4 UUID
uuid_v4 = UUIDV4("550e8400-e29b-41d4-a716-446655440000")
ULID Handling:
# Create a string ULID
ulid_str = StrULIDType("01ARZ3NDEKTSV4RRFFQ69G5FAV")
# Create an integer ULID
ulid_int = IntULIDType(1234567890)
Serialization:
class MyType(JsonMixin):
def __init__(self, data):
self.data = data
# Convert to/from JSON
obj = MyType({"key": "value"})
json_data = obj.to_json()
new_obj = MyType.from_json(json_data)
Notes
All numeric types include validation to ensure values meet specified constraints.
UUID and ULID implementations support both string and integer representations.
Version classes support various versioning schemes with proper validation.
Serialization mixins provide consistent interfaces for different formats.
All classes include proper type hints for better IDE support.