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.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 boolish 1.0.1
File Size Uploaded Provenance Actions
boolish-1.0.1.tar.gz 42.2 kB Without provenance Details for boolish-1.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 59.2 kB

Release files / boolish-1.0.1.tar.gz

Download URL boolish-1.0.1.tar.gz
Size 42.2 kB
Tags Source
SHA-256 checksum
How to use checksums
afeab373f9b7b2f5d1014eaff97632b7dda2ad0a2666662aec820ddc967adb7e
BLAKE2b-256 checksum
How to use checksums
a3c845bb1d7bb28143afcad349591065b9f5a6de8d0299d7a001c155c2f2a688
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.1-py3-none-any.whl

Download URL boolish-1.0.1-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e28a964743bdb5aaf8b1653f0e48df99aaaa3e0c8e41e4f9e80586d19b974bfb
BLAKE2b-256 checksum
How to use checksums
dc7939b11f7e898a25231266f5e889158329e14186af53b1ca78c1d9dc7bc481
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.1

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