Skip to main content

boolish

Lianes8 Certified

Read loose, human boolean-ish strings and values as a real bool. A typed, dependency-free stand-in for distutils.util.strtobool, which Python deprecated in 3.10 and removed in 3.12 (PEP 632), plus a broader coercer, non-raising predicates, an environment helper, a multilingual vocabulary and a command line tool.

Install

pip install boolish

Python 3.10 or newer, no dependencies.

Quick start

>>> from boolish import strtobool
>>> strtobool("yes"), strtobool("  Off "), strtobool("enabled")
(True, False, True)
>>> strtobool("maybe", False)
False
>>> strtobool("maybe")
Traceback (most recent call last):
...
boolish.NotBooleanError: 'maybe' is not a recognised boolean

NotBooleanError subclasses ValueError, so code written for the stdlib function keeps catching it. Coerce more than strings with to_bool, or ask without ever raising:

>>> from boolish import is_falsy, is_truthy, to_bool
>>> to_bool(1), to_bool(0.0), to_bool("on"), to_bool(None, False)
(True, False, True, False)
>>> is_truthy("maybe"), is_falsy("maybe"), is_falsy("no")
(False, False, True)

Read a flag from the environment. Unset gives the default; a set but unreadable value raises, naming the variable, so a misconfigured flag fails loudly:

>>> import os
>>> from boolish import getenv_bool
>>> os.environ["CACHE"] = "off"
>>> getenv_bool("CACHE", True), getenv_bool("UNSET_FLAG_FOR_README", True)
(False, True)

Pick or build a vocabulary. CLASSIC is exactly the stdlib function's twelve words, DEFAULT adds enable(d)/disable(d), MULTILINGUAL adds Spanish, French, German, Italian and Portuguese; lenient mode (strict=False) folds accents:

>>> from boolish import CLASSIC, DEFAULT, MULTILINGUAL
>>> strtobool("enabled", None, rules=CLASSIC) is None
True
>>> strtobool("Sí", rules=MULTILINGUAL, strict=False), strtobool("nein", rules=MULTILINGUAL)
(True, False)
>>> pirate = DEFAULT.extend(true={"aye"}, false={"nay"})
>>> strtobool("AYE", rules=pirate)
True

Command line

$ boolish yes
true
$ boolish --rules multilingual --lenient NÃO
false
$ boolish --true aye --false nay aye
true

The exit code is 0 for true, 1 for false and 2 for an unrecognised value or a usage error, so if boolish --quiet "$FLAG"; then ...; fi works in a shell. python -m boolish runs the same tool.

Why

distutils.util.strtobool was the quiet default for reading flags. It returned 1 or 0 rather than a bool, did not ignore surrounding spaces, and is gone since Python 3.12. Other packages fill part of the gap: setuptools keeps a copy in its private setuptools._distutils.util; python-strtobool 1.0.3 copies the function and returns a bool; str2bool (last release 1.1, December 2017) knows no on/off and returns None for unknown input. boolish is checked word by word against the stdlib function, setuptools, python-strtobool, configparser, click, PyYAML (YAML 1.1) and ruamel.yaml (YAML 1.2) and never gives an answer that contradicts any of them; it adds typed results and errors, defaults, non-string input, an environment helper and a CLI.

Learn more

  • GUIDE: modes, vocabularies, errors, the CLI and how boolish compares with other parsers.
  • CHANGELOG: what changed in each release, including the upgrade notes for 1.0.0.

License

MIT. See LICENSE.

Metadata

Release files for boolish 1.0.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 boolish 1.0.0
File Size Uploaded Provenance Actions
boolish-1.0.0.tar.gz 40.9 kB Without provenance Details for boolish-1.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 57.9 kB

Release files / boolish-1.0.0.tar.gz

Download URL boolish-1.0.0.tar.gz
Size 40.9 kB
Tags Source
SHA-256 checksum
How to use checksums
321b99c283e06b5f08d2257017305d19df8d0942e0f283a111d0b1ac53784a97
BLAKE2b-256 checksum
How to use checksums
257df5869e6c2a3165a261631dedfbdd8e9dfe53715fb0df83986d15e8ef761f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / boolish-1.0.0-py3-none-any.whl

Download URL boolish-1.0.0-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0901892493ef7a25e08f30c349fafef0bd8f6353e95986978d7fe3bf97beed57
BLAKE2b-256 checksum
How to use checksums
84cda1763f7c276a8e66fa0e988a5e504d18723134cbc0cba72bba46c64e8251
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for boolish 1.0.0

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.

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