Skip to main content

strcoerce

Safe string-to-type coercion for Python. Zero dependencies.

The problem

import os
DEBUG = bool(os.environ.get("DEBUG", "false"))  # Always True — "false" is a non-empty string

bool("false") is True in Python. This catches experienced developers off guard and is a documented trap with no stdlib fix.

Install

pip install strcoerce

Usage

from strcoerce import parse_bool, parse_int, parse_float, parse_list, parse_duration

# Environment variables
DEBUG  = parse_bool(os.environ.get("DEBUG", "false"))   # False
PORT   = parse_int(os.environ.get("PORT", "8080"))      # 8080
HOSTS  = parse_list(os.environ.get("ALLOWED_HOSTS", ""))  # []

# With defaults
parse_bool("maybe", default=False)  # False — no exception
parse_int("oops",   default=0)      # 0

# Durations
from datetime import timedelta
parse_duration("2h30m")   # timedelta(hours=2, minutes=30)
parse_duration("1w2d")    # timedelta(weeks=1, days=2)

API

parse_bool(s, *, default=<raise>)

Input Result
"1", "true", "yes", "on", "t", "y" True
"0", "false", "no", "off", "f", "n" False
anything else (no default) ParseError
anything else (default given) default

Case-insensitive. Strips surrounding whitespace.

parse_int(s, *, default=<raise>, base=10)

parse_int("42")              # 42
parse_int("0xff", base=16)   # 255
parse_int("bad", default=0)  # 0

parse_float(s, *, default=<raise>)

parse_float("3.14")           # 3.14
parse_float("1e-3")           # 0.001
parse_float("bad", default=0) # 0

parse_list(s, sep=",", *, strip=True)

parse_list("a, b, c")          # ["a", "b", "c"]
parse_list("a|b|c", sep="|")   # ["a", "b", "c"]
parse_list("")                 # []

parse_duration(s)

Parses combinations of Nw Nd Nh Nm Ns (weeks/days/hours/minutes/seconds).

parse_duration("30s")        # timedelta(seconds=30)
parse_duration("5m")         # timedelta(minutes=5)
parse_duration("2h30m")      # timedelta(hours=2, minutes=30)
parse_duration("1w2d3h4m5s") # timedelta(weeks=1, days=2, hours=3, minutes=4, seconds=5)

Error handling

All functions raise ParseError (subclass of ValueError) when input is invalid and no default is given.

from strcoerce import ParseError

try:
    val = parse_bool(user_input)
except ParseError as e:
    print(f"Invalid boolean: {e}")

License

MIT

Metadata

Release files for strcoerce 0.1.0

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

Source distribution (sdist)

Source distribution for strcoerce 0.1.0
File Size Uploaded
strcoerce-0.1.0.tar.gz 5.7 kB Details

Built distribution (wheel)

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

Total release size: 10.4 kB

Release files / strcoerce-0.1.0.tar.gz

Download URL strcoerce-0.1.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f0534ce142830e67c48a7f5e9bfdca1036b58b31a89a381b0a0cd2ab4033b605
BLAKE2b-256 checksum
How to use checksums
22da80c4e0327594b7827ad1a69d59c1583a420388353524d1dd79857fb8ae07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 9, 2026.

Transparency log

Release files / strcoerce-0.1.0-py3-none-any.whl

Download URL strcoerce-0.1.0-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09cb1000911943711d425295a51d3b8798fd16359e6ead82b697f3562224ffbf
BLAKE2b-256 checksum
How to use checksums
40fd5ed834a8d636d63fe48ef1142e35d59bafa452af8927d0f95e0e62844e4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 9, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

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