castfit: basic type casting
Cuddles the Cat
"If it fits, I sits."
Why?
castfit helps you convert things like command-line arguments (e.g., from docopt) and simple API responses into something more typed with low overhead.
Install
# modern (recommended)
uv add castfit
# classic
python -m pip install castfit
Alternatively, you can just download the single file and name it castfit.py.
Example: CLI-like Args
# Example: CLI-like args
from typing import Optional
from pathlib import Path
from castfit import castfit
class Args:
host: str
port: int
timeout: Optional[float]
log: Path
data = {
"host": "localhost",
"port": "8080",
# "timeout": "5.0" # key can be missing
"log": "app.log",
}
config = castfit(Args, data)
assert config.host == "localhost"
assert config.port == 8080
assert config.timeout is None
assert config.log == Path("app.log")
# if timeout was present:
data = {"host": "localhost", "port": "8080", "timeout": "5.0", "log": "app.log"}
config = castfit(Args, data)
assert config.host == "localhost"
assert config.port == 8080
assert config.timeout == 5.0
assert config.log == Path("app.log")
Example: Nested Types
# Example: nested types
from dataclasses import dataclass
from typing import Literal
from castfit import castfit
@dataclass
class Pet:
name: str
type: Literal["cat", "dog", "other"]
age: int
@dataclass
class Owner:
name: str
pets: list[Pet]
owner_data = {
"name": "Alice",
"pets": [
{"name": "Cuddles", "type": "cat", "age": "4"},
{"name": "Buddy", "type": "dog", "age": "2.5"}, # age will be cast to int(2)
],
}
owner = castfit(Owner, owner_data)
assert owner.name == "Alice"
assert len(owner.pets) == 2
assert isinstance(owner.pets[0], Pet)
assert owner.pets[0].name == "Cuddles"
assert owner.pets[0].type == "cat"
assert owner.pets[0].age == 4
assert owner.pets[1].name == "Buddy"
assert owner.pets[1].age == 2 # Cast from "2.5" to int
Example: Custom Functions
# Example: adding a custom converter
from dataclasses import dataclass
import castfit
@dataclass
class LatLon:
lat: float
lon: float
@castfit.casts
def str_to_latlon(s: str) -> LatLon:
lat, lon = map(float, s.split(","))
return LatLon(lat, lon)
assert castfit.to_type("40.7,-74.0", LatLon) == LatLon(40.7, -74.0)
Other Projects
pydantic: comprehensive, but feels heavy.cattrs: good simple cases, but has a complex set of converters.
License
Metadata
Release files for castfit 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| castfit-0.1.3.tar.gz | 12.9 kB | Without provenance | Details for castfit-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| castfit-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for castfit-0.1.3-py3-none-any.whl |
Total release size: 25.3 kB
Release files / castfit-0.1.3.tar.gz
| Download URL | castfit-0.1.3.tar.gz |
|---|---|
| Size | 12.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
12f39f1091e7ac63151f24886c5259147d794e1d962d42f39a69b1d79a5d0fc7
|
|
BLAKE2b-256 checksum How to use checksums |
6519c8ef6295ed8f413240a8de259fc9ef984e6769d5ef98f1c27608ba696d9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / castfit-0.1.3-py3-none-any.whl
| Download URL | castfit-0.1.3-py3-none-any.whl |
|---|---|
| Size | 12.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
faa261b894c46dd4fa5550858c936b56b68bb34a24bd3f33839835c24f49634d
|
|
BLAKE2b-256 checksum How to use checksums |
63eee37f85699db4338092c5e697d50079e0e1b10090b7486f8c5726f6b771ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for castfit 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.