Skip to main content

Nothing (almost) should ever be any str or any int


What is valtypes

Valtypes is a flexible data parsing library which will help you make illegal states unrepresentable and enable you to practice "Parse, don’t validate" in Python. It has many features that might interest you, so let's dive into some examples.

Examples

Create constrained types:

from valtypes.type.str import NonEmpty, MaximumLength


class Name(NonEmpty, MaximumLength):
    __maximum_length__ = 20

    
def initials(name: Name) -> str:
    # name is guaranteed to be a non-empty string of maximum length 20
    return f"{name[0]}."


initials(Name("Fred"))  # passes
initials(Name(""))  # parsing error
initials("")  # fails at static type checking

Parse complex data structures:

from dataclasses import dataclass


from valtypes import parse
from valtypes.type import int, list, str


@dataclass
class User:
    id: int.Positive
    name: Name
    hobbies: list.NonEmpty[str.NonEmpty]

    
raw = {"id": 1, "name": "Fred", "hobbies": ["origami", "curling", "programming"]}

print(parse(User, raw))
User(id=1, name='Fred', hobbies=['origami', 'curling', 'programming'])

Installation

Install from PyPI:

pip install valtypes

Build the latest version from source:

pip install git+https://github.com/LeeeeT/valtypes

Metadata

Release files for valtypes 6.0.1

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

Source distribution (sdist)

Source distribution for valtypes 6.0.1
File Size Uploaded
valtypes-6.0.1.tar.gz 17.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for valtypes 6.0.1
File Interpreter ABI Platform
valtypes-6.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 50.4 kB

Release files / valtypes-6.0.1.tar.gz

Download URL valtypes-6.0.1.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4faf8b1391d6358bde2cef06a9e2ad53f61e659a20f11a1b41c868954f483756
BLAKE2b-256 checksum
How to use checksums
5ad4000586f3c23120deae3b35633097859623a9fbf33bdf01d9092cc5f1c6d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / valtypes-6.0.1-py3-none-any.whl

Download URL valtypes-6.0.1-py3-none-any.whl
Size 33.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8d2ee8c304a5a2701c74a979cc208b16ae807d6e85144bf4d6e46073baab11f
BLAKE2b-256 checksum
How to use checksums
48c41756daff3aacf4222cd1ff911923db863c90ec59f08da0a56b1681d62623
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

This release

6.0.1 This release

2 release files

6.0.0

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