envbool 0.2.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {envbool-0.2.0 → envbool-0.4.0}/PKG-INFO +63 -60
- {envbool-0.2.0 → envbool-0.4.0}/README.md +61 -57
- {envbool-0.2.0 → envbool-0.4.0}/pyproject.toml +4 -5
- {envbool-0.2.0 → envbool-0.4.0}/src/envbool/__init__.py +23 -9
- envbool-0.4.0/src/envbool/__main__.py +6 -0
- envbool-0.4.0/src/envbool/_cli.py +224 -0
- {envbool-0.2.0 → envbool-0.4.0}/src/envbool/_core.py +26 -55
- envbool-0.4.0/src/envbool/_defaults.py +189 -0
- {envbool-0.2.0 → envbool-0.4.0}/src/envbool/_env.py +20 -4
- {envbool-0.2.0 → envbool-0.4.0}/src/envbool/exceptions.py +19 -9
- envbool-0.2.0/src/envbool/_cli.py +0 -234
- envbool-0.2.0/src/envbool/_config.py +0 -369
- envbool-0.2.0/src/envbool/_defaults.py +0 -14
- {envbool-0.2.0 → envbool-0.4.0}/src/envbool/py.typed +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: envbool
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: A small Python library and CLI tool for coercing environment variables (and arbitrary strings) into boolean values.
|
|
5
5
|
Keywords: environment variables,boolean,configuration,env,coerce
|
|
6
6
|
Author: Kyle O'Malley
|
|
7
7
|
Author-email: Kyle O'Malley <j.kyle.omalley@gmail.com>
|
|
8
8
|
License-Expression: MIT
|
|
9
|
-
Classifier: Development Status ::
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
10
|
Classifier: Intended Audience :: Developers
|
|
11
11
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
12
12
|
Classifier: Topic :: Utilities
|
|
@@ -18,7 +18,6 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.13
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.14
|
|
20
20
|
Classifier: Typing :: Typed
|
|
21
|
-
Requires-Dist: platformdirs>=4.9.6
|
|
22
21
|
Requires-Python: >=3.11
|
|
23
22
|
Project-URL: Homepage, https://github.com/jkomalley/envbool
|
|
24
23
|
Project-URL: Repository, https://github.com/jkomalley/envbool
|
|
@@ -67,18 +66,17 @@ CACHE = envbool("CACHE")
|
|
|
67
66
|
- **Always returns `bool`.** No `None`, no surprises in your type signatures.
|
|
68
67
|
- **Customizable value sets.** Replace or extend the truthy/falsy words your
|
|
69
68
|
environment uses.
|
|
70
|
-
- **
|
|
71
|
-
|
|
69
|
+
- **Process-level defaults.** Call `set_defaults()` once at startup instead
|
|
70
|
+
of threading options through every call site.
|
|
72
71
|
- **A CLI for shell scripts.** Exit codes map to truthiness, so it drops
|
|
73
72
|
straight into `&&` / `||` chains.
|
|
74
|
-
- **Zero ceremony.**
|
|
73
|
+
- **Zero ceremony.** Zero dependencies, fully typed, Python 3.11+.
|
|
75
74
|
|
|
76
75
|
## Contents
|
|
77
76
|
|
|
78
77
|
- [Installation](#installation)
|
|
79
78
|
- [Usage](#usage)
|
|
80
79
|
- [Command-line interface](#command-line-interface)
|
|
81
|
-
- [Configuration](#configuration)
|
|
82
80
|
- [API reference](#api-reference)
|
|
83
81
|
- [Advanced topics](#advanced-topics)
|
|
84
82
|
- [Contributing](#contributing)
|
|
@@ -151,6 +149,37 @@ to_bool("0") # False
|
|
|
151
149
|
to_bool("maybe", strict=True) # raises InvalidBoolValueError
|
|
152
150
|
```
|
|
153
151
|
|
|
152
|
+
### Process-level defaults
|
|
153
|
+
|
|
154
|
+
Set policy once at startup instead of threading `strict=`/`extend_truthy=`
|
|
155
|
+
through every call site:
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
import envbool
|
|
159
|
+
|
|
160
|
+
envbool.set_defaults(strict=True, extend_truthy=["enabled"])
|
|
161
|
+
|
|
162
|
+
envbool.envbool("DEBUG") # now raises on unrecognized values by default
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`set_defaults()` replaces the process-level defaults **from the built-ins**,
|
|
166
|
+
not from whatever a previous `set_defaults()` call left in place — call it
|
|
167
|
+
once. Call-site arguments (`envbool("X", strict=False)`) still override
|
|
168
|
+
whatever `set_defaults()` configured:
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
built-in defaults → set_defaults() → function arguments / CLI flags
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
`get_defaults()` returns the active `Defaults` (a frozen dataclass: `strict`,
|
|
175
|
+
`warn`, `effective_truthy`, `effective_falsy`) for inspection.
|
|
176
|
+
`reset_defaults()` restores the built-ins — call it in a test fixture (see
|
|
177
|
+
[Testing code that uses envbool](#testing-code-that-uses-envbool)).
|
|
178
|
+
|
|
179
|
+
> Through 0.3.x, envbool read TOML config files (`envbool.toml`,
|
|
180
|
+
> `[tool.envbool]`). 0.4.0 removed them in favor of `set_defaults()` — see
|
|
181
|
+
> `CHANGELOG.md` for the rationale and migration note.
|
|
182
|
+
|
|
154
183
|
## Command-line interface
|
|
155
184
|
|
|
156
185
|
The `envbool` command exits `0` for truthy, `1` for falsy, and `2` on error, so
|
|
@@ -176,9 +205,9 @@ in that order of priority.
|
|
|
176
205
|
|
|
177
206
|
```console
|
|
178
207
|
$ envbool --help
|
|
179
|
-
usage: envbool [-h] [--value TEXT] [--strict] [--warn] [--default]
|
|
180
|
-
[--
|
|
181
|
-
[--extend-
|
|
208
|
+
usage: envbool [-h] [--value TEXT] [--strict] [--warn] [--default]
|
|
209
|
+
[--required] [--print] [--truthy VALUE] [--falsy VALUE]
|
|
210
|
+
[--extend-truthy VALUE] [--extend-falsy VALUE]
|
|
182
211
|
[VAR_NAME]
|
|
183
212
|
|
|
184
213
|
Coerce an environment variable or string to a boolean.
|
|
@@ -192,48 +221,25 @@ options:
|
|
|
192
221
|
--strict, -s Raise error on unrecognized values.
|
|
193
222
|
--warn Log a warning on unrecognized values.
|
|
194
223
|
--default, -d Default value if unset/empty (default: false).
|
|
224
|
+
--required, -r Exit 2 if VAR_NAME is not set in the environment.
|
|
195
225
|
--print, -p Print "true" or "false" instead of using exit codes.
|
|
196
226
|
--truthy VALUE Replace the truthy set with VALUE (repeatable).
|
|
197
227
|
--falsy VALUE Replace the falsy set with VALUE (repeatable).
|
|
198
228
|
--extend-truthy VALUE
|
|
199
229
|
Add VALUE to the truthy set (repeatable).
|
|
200
230
|
--extend-falsy VALUE Add VALUE to the falsy set (repeatable).
|
|
201
|
-
--show-config Print the effective configuration and exit.
|
|
202
231
|
```
|
|
203
232
|
|
|
204
233
|
A few rules worth knowing:
|
|
205
234
|
|
|
206
|
-
- Omitting `--strict` / `--warn`
|
|
235
|
+
- Omitting `--strict` / `--warn` uses the built-in defaults (lenient, no
|
|
236
|
+
warnings). `set_defaults()` is a library-level concern — the one-shot CLI
|
|
237
|
+
process doesn't read it.
|
|
207
238
|
- `VAR_NAME` and `--value` are mutually exclusive.
|
|
208
|
-
- `--
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
## Configuration
|
|
214
|
-
|
|
215
|
-
Share defaults across a project by dropping an `envbool.toml` at its root (or a
|
|
216
|
-
`[tool.envbool]` table in `pyproject.toml`):
|
|
217
|
-
|
|
218
|
-
```toml
|
|
219
|
-
# envbool.toml
|
|
220
|
-
strict = true
|
|
221
|
-
extend_truthy = ["enabled"]
|
|
222
|
-
extend_falsy = ["disabled"]
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
`envbool` walks up from the current directory to find the nearest project config,
|
|
226
|
-
then falls back to a user-level `config.toml` in the platform's standard config
|
|
227
|
-
directory (`~/.config/envbool/` on Linux, `~/Library/Application Support/envbool/`
|
|
228
|
-
on macOS), resolved via [platformdirs](https://pypi.org/project/platformdirs/).
|
|
229
|
-
|
|
230
|
-
Values resolve in three layers, each overriding the last:
|
|
231
|
-
|
|
232
|
-
```
|
|
233
|
-
built-in defaults → config file → function arguments / CLI flags
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
Set `ENVBOOL_NO_CONFIG=1` to skip config discovery entirely.
|
|
239
|
+
- `--required` only applies to `VAR_NAME`; combining it with `--value` or
|
|
240
|
+
giving it no `VAR_NAME` at all is a usage error.
|
|
241
|
+
- With no `VAR_NAME`, `--value`, or non-empty piped stdin, the CLI prints
|
|
242
|
+
usage and exits `2`.
|
|
237
243
|
|
|
238
244
|
## API reference
|
|
239
245
|
|
|
@@ -241,24 +247,30 @@ Set `ENVBOOL_NO_CONFIG=1` to skip config discovery entirely.
|
|
|
241
247
|
| --- | --- |
|
|
242
248
|
| `envbool(var, **opts)` | Read an environment variable and return `bool`. |
|
|
243
249
|
| `to_bool(value, **opts)` | Coerce a string to `bool`. |
|
|
244
|
-
| `
|
|
245
|
-
| `
|
|
250
|
+
| `set_defaults(**opts)` | Set process-level strict/warn/truthy/falsy defaults, replacing the built-ins. |
|
|
251
|
+
| `get_defaults()` | Return the active `Defaults`. |
|
|
252
|
+
| `reset_defaults()` | Restore built-in defaults. |
|
|
253
|
+
| `Defaults` | Frozen dataclass: `strict`, `warn`, `effective_truthy`, `effective_falsy`. |
|
|
246
254
|
| `DEFAULT_TRUTHY` | `frozenset` of the built-in truthy strings. |
|
|
247
255
|
| `DEFAULT_FALSY` | `frozenset` of the built-in falsy strings. |
|
|
248
256
|
| `EnvBoolError` | Base class for every exception the library raises. |
|
|
249
257
|
| `InvalidBoolValueError` | Raised in strict mode for unrecognized values. Also a `ValueError`. |
|
|
250
|
-
| `
|
|
258
|
+
| `MissingEnvVarError` | Raised by `envbool(required=True)` when the variable is unset. Also a `KeyError`. |
|
|
251
259
|
|
|
252
260
|
`envbool()` and `to_bool()` share the same keyword-only options:
|
|
253
261
|
|
|
254
262
|
| Option | Type | Default | Meaning |
|
|
255
263
|
| --- | --- | --- | --- |
|
|
256
264
|
| `default` | `bool` | `False` | Returned for unset/empty input. |
|
|
257
|
-
| `strict` | `bool \| None` | `None` | Raise on unrecognized values (`None` defers to
|
|
258
|
-
| `warn` | `bool \| None` | `None` | Log a warning on unrecognized values (`None` defers to
|
|
265
|
+
| `strict` | `bool \| None` | `None` | Raise on unrecognized values (`None` defers to `set_defaults()`). |
|
|
266
|
+
| `warn` | `bool \| None` | `None` | Log a warning on unrecognized values (`None` defers to `set_defaults()`). |
|
|
259
267
|
| `truthy` / `falsy` | `Iterable[str] \| None` | `None` | **Replace** the effective set. |
|
|
260
268
|
| `extend_truthy` / `extend_falsy` | `Iterable[str] \| None` | `None` | **Extend** the effective set. |
|
|
261
269
|
|
|
270
|
+
`envbool()` also accepts `required` (`bool`, default `False`): when `True`, a
|
|
271
|
+
variable that is unset raises `MissingEnvVarError` before `default` is applied. A
|
|
272
|
+
variable set to an empty string counts as present and still uses `default`.
|
|
273
|
+
|
|
262
274
|
## Advanced topics
|
|
263
275
|
|
|
264
276
|
### Exception handling
|
|
@@ -288,10 +300,6 @@ InvalidBoolValueError: Invalid boolean value for MY_VAR: 'maybe'
|
|
|
288
300
|
Expected falsy: 0, false, no, off
|
|
289
301
|
```
|
|
290
302
|
|
|
291
|
-
`ConfigError` is raised when a config file is found but malformed (for example,
|
|
292
|
-
`strict = "yes"` instead of `strict = true`). It carries the offending path on
|
|
293
|
-
`e.path`.
|
|
294
|
-
|
|
295
303
|
### Logging
|
|
296
304
|
|
|
297
305
|
`envbool` logs through the standard `logging` module under the `"envbool"`
|
|
@@ -307,7 +315,6 @@ logging.getLogger("envbool").addHandler(logging.StreamHandler())
|
|
|
307
315
|
|
|
308
316
|
| Level | When |
|
|
309
317
|
| --- | --- |
|
|
310
|
-
| `DEBUG` | A config file was discovered and loaded, or none was found. |
|
|
311
318
|
| `WARNING` | An unrecognized value fell through in lenient mode (only when `warn=True`). |
|
|
312
319
|
| `WARNING` | The truthy and falsy sets overlap (truthy wins). |
|
|
313
320
|
|
|
@@ -330,24 +337,20 @@ else:
|
|
|
330
337
|
|
|
331
338
|
### Testing code that uses envbool
|
|
332
339
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
autouse fixture:
|
|
340
|
+
If your tests call `set_defaults()`, reset it between tests with an autouse
|
|
341
|
+
fixture so overrides don't leak across the suite:
|
|
336
342
|
|
|
337
343
|
```python
|
|
338
344
|
# conftest.py
|
|
339
345
|
import pytest
|
|
340
|
-
from envbool
|
|
346
|
+
from envbool import reset_defaults
|
|
341
347
|
|
|
342
348
|
@pytest.fixture(autouse=True)
|
|
343
|
-
def
|
|
349
|
+
def _reset_envbool_defaults():
|
|
344
350
|
yield
|
|
345
|
-
|
|
351
|
+
reset_defaults()
|
|
346
352
|
```
|
|
347
353
|
|
|
348
|
-
`_reset_config()` is private but stable and exists for exactly this purpose; it
|
|
349
|
-
clears the cache under a lock, so it is safe to call from any thread.
|
|
350
|
-
|
|
351
354
|
## Contributing
|
|
352
355
|
|
|
353
356
|
Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for development
|
|
@@ -39,18 +39,17 @@ CACHE = envbool("CACHE")
|
|
|
39
39
|
- **Always returns `bool`.** No `None`, no surprises in your type signatures.
|
|
40
40
|
- **Customizable value sets.** Replace or extend the truthy/falsy words your
|
|
41
41
|
environment uses.
|
|
42
|
-
- **
|
|
43
|
-
|
|
42
|
+
- **Process-level defaults.** Call `set_defaults()` once at startup instead
|
|
43
|
+
of threading options through every call site.
|
|
44
44
|
- **A CLI for shell scripts.** Exit codes map to truthiness, so it drops
|
|
45
45
|
straight into `&&` / `||` chains.
|
|
46
|
-
- **Zero ceremony.**
|
|
46
|
+
- **Zero ceremony.** Zero dependencies, fully typed, Python 3.11+.
|
|
47
47
|
|
|
48
48
|
## Contents
|
|
49
49
|
|
|
50
50
|
- [Installation](#installation)
|
|
51
51
|
- [Usage](#usage)
|
|
52
52
|
- [Command-line interface](#command-line-interface)
|
|
53
|
-
- [Configuration](#configuration)
|
|
54
53
|
- [API reference](#api-reference)
|
|
55
54
|
- [Advanced topics](#advanced-topics)
|
|
56
55
|
- [Contributing](#contributing)
|
|
@@ -123,6 +122,37 @@ to_bool("0") # False
|
|
|
123
122
|
to_bool("maybe", strict=True) # raises InvalidBoolValueError
|
|
124
123
|
```
|
|
125
124
|
|
|
125
|
+
### Process-level defaults
|
|
126
|
+
|
|
127
|
+
Set policy once at startup instead of threading `strict=`/`extend_truthy=`
|
|
128
|
+
through every call site:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
import envbool
|
|
132
|
+
|
|
133
|
+
envbool.set_defaults(strict=True, extend_truthy=["enabled"])
|
|
134
|
+
|
|
135
|
+
envbool.envbool("DEBUG") # now raises on unrecognized values by default
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`set_defaults()` replaces the process-level defaults **from the built-ins**,
|
|
139
|
+
not from whatever a previous `set_defaults()` call left in place — call it
|
|
140
|
+
once. Call-site arguments (`envbool("X", strict=False)`) still override
|
|
141
|
+
whatever `set_defaults()` configured:
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
built-in defaults → set_defaults() → function arguments / CLI flags
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
`get_defaults()` returns the active `Defaults` (a frozen dataclass: `strict`,
|
|
148
|
+
`warn`, `effective_truthy`, `effective_falsy`) for inspection.
|
|
149
|
+
`reset_defaults()` restores the built-ins — call it in a test fixture (see
|
|
150
|
+
[Testing code that uses envbool](#testing-code-that-uses-envbool)).
|
|
151
|
+
|
|
152
|
+
> Through 0.3.x, envbool read TOML config files (`envbool.toml`,
|
|
153
|
+
> `[tool.envbool]`). 0.4.0 removed them in favor of `set_defaults()` — see
|
|
154
|
+
> `CHANGELOG.md` for the rationale and migration note.
|
|
155
|
+
|
|
126
156
|
## Command-line interface
|
|
127
157
|
|
|
128
158
|
The `envbool` command exits `0` for truthy, `1` for falsy, and `2` on error, so
|
|
@@ -148,9 +178,9 @@ in that order of priority.
|
|
|
148
178
|
|
|
149
179
|
```console
|
|
150
180
|
$ envbool --help
|
|
151
|
-
usage: envbool [-h] [--value TEXT] [--strict] [--warn] [--default]
|
|
152
|
-
[--
|
|
153
|
-
[--extend-
|
|
181
|
+
usage: envbool [-h] [--value TEXT] [--strict] [--warn] [--default]
|
|
182
|
+
[--required] [--print] [--truthy VALUE] [--falsy VALUE]
|
|
183
|
+
[--extend-truthy VALUE] [--extend-falsy VALUE]
|
|
154
184
|
[VAR_NAME]
|
|
155
185
|
|
|
156
186
|
Coerce an environment variable or string to a boolean.
|
|
@@ -164,48 +194,25 @@ options:
|
|
|
164
194
|
--strict, -s Raise error on unrecognized values.
|
|
165
195
|
--warn Log a warning on unrecognized values.
|
|
166
196
|
--default, -d Default value if unset/empty (default: false).
|
|
197
|
+
--required, -r Exit 2 if VAR_NAME is not set in the environment.
|
|
167
198
|
--print, -p Print "true" or "false" instead of using exit codes.
|
|
168
199
|
--truthy VALUE Replace the truthy set with VALUE (repeatable).
|
|
169
200
|
--falsy VALUE Replace the falsy set with VALUE (repeatable).
|
|
170
201
|
--extend-truthy VALUE
|
|
171
202
|
Add VALUE to the truthy set (repeatable).
|
|
172
203
|
--extend-falsy VALUE Add VALUE to the falsy set (repeatable).
|
|
173
|
-
--show-config Print the effective configuration and exit.
|
|
174
204
|
```
|
|
175
205
|
|
|
176
206
|
A few rules worth knowing:
|
|
177
207
|
|
|
178
|
-
- Omitting `--strict` / `--warn`
|
|
208
|
+
- Omitting `--strict` / `--warn` uses the built-in defaults (lenient, no
|
|
209
|
+
warnings). `set_defaults()` is a library-level concern — the one-shot CLI
|
|
210
|
+
process doesn't read it.
|
|
179
211
|
- `VAR_NAME` and `--value` are mutually exclusive.
|
|
180
|
-
- `--
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
## Configuration
|
|
186
|
-
|
|
187
|
-
Share defaults across a project by dropping an `envbool.toml` at its root (or a
|
|
188
|
-
`[tool.envbool]` table in `pyproject.toml`):
|
|
189
|
-
|
|
190
|
-
```toml
|
|
191
|
-
# envbool.toml
|
|
192
|
-
strict = true
|
|
193
|
-
extend_truthy = ["enabled"]
|
|
194
|
-
extend_falsy = ["disabled"]
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
`envbool` walks up from the current directory to find the nearest project config,
|
|
198
|
-
then falls back to a user-level `config.toml` in the platform's standard config
|
|
199
|
-
directory (`~/.config/envbool/` on Linux, `~/Library/Application Support/envbool/`
|
|
200
|
-
on macOS), resolved via [platformdirs](https://pypi.org/project/platformdirs/).
|
|
201
|
-
|
|
202
|
-
Values resolve in three layers, each overriding the last:
|
|
203
|
-
|
|
204
|
-
```
|
|
205
|
-
built-in defaults → config file → function arguments / CLI flags
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
Set `ENVBOOL_NO_CONFIG=1` to skip config discovery entirely.
|
|
212
|
+
- `--required` only applies to `VAR_NAME`; combining it with `--value` or
|
|
213
|
+
giving it no `VAR_NAME` at all is a usage error.
|
|
214
|
+
- With no `VAR_NAME`, `--value`, or non-empty piped stdin, the CLI prints
|
|
215
|
+
usage and exits `2`.
|
|
209
216
|
|
|
210
217
|
## API reference
|
|
211
218
|
|
|
@@ -213,24 +220,30 @@ Set `ENVBOOL_NO_CONFIG=1` to skip config discovery entirely.
|
|
|
213
220
|
| --- | --- |
|
|
214
221
|
| `envbool(var, **opts)` | Read an environment variable and return `bool`. |
|
|
215
222
|
| `to_bool(value, **opts)` | Coerce a string to `bool`. |
|
|
216
|
-
| `
|
|
217
|
-
| `
|
|
223
|
+
| `set_defaults(**opts)` | Set process-level strict/warn/truthy/falsy defaults, replacing the built-ins. |
|
|
224
|
+
| `get_defaults()` | Return the active `Defaults`. |
|
|
225
|
+
| `reset_defaults()` | Restore built-in defaults. |
|
|
226
|
+
| `Defaults` | Frozen dataclass: `strict`, `warn`, `effective_truthy`, `effective_falsy`. |
|
|
218
227
|
| `DEFAULT_TRUTHY` | `frozenset` of the built-in truthy strings. |
|
|
219
228
|
| `DEFAULT_FALSY` | `frozenset` of the built-in falsy strings. |
|
|
220
229
|
| `EnvBoolError` | Base class for every exception the library raises. |
|
|
221
230
|
| `InvalidBoolValueError` | Raised in strict mode for unrecognized values. Also a `ValueError`. |
|
|
222
|
-
| `
|
|
231
|
+
| `MissingEnvVarError` | Raised by `envbool(required=True)` when the variable is unset. Also a `KeyError`. |
|
|
223
232
|
|
|
224
233
|
`envbool()` and `to_bool()` share the same keyword-only options:
|
|
225
234
|
|
|
226
235
|
| Option | Type | Default | Meaning |
|
|
227
236
|
| --- | --- | --- | --- |
|
|
228
237
|
| `default` | `bool` | `False` | Returned for unset/empty input. |
|
|
229
|
-
| `strict` | `bool \| None` | `None` | Raise on unrecognized values (`None` defers to
|
|
230
|
-
| `warn` | `bool \| None` | `None` | Log a warning on unrecognized values (`None` defers to
|
|
238
|
+
| `strict` | `bool \| None` | `None` | Raise on unrecognized values (`None` defers to `set_defaults()`). |
|
|
239
|
+
| `warn` | `bool \| None` | `None` | Log a warning on unrecognized values (`None` defers to `set_defaults()`). |
|
|
231
240
|
| `truthy` / `falsy` | `Iterable[str] \| None` | `None` | **Replace** the effective set. |
|
|
232
241
|
| `extend_truthy` / `extend_falsy` | `Iterable[str] \| None` | `None` | **Extend** the effective set. |
|
|
233
242
|
|
|
243
|
+
`envbool()` also accepts `required` (`bool`, default `False`): when `True`, a
|
|
244
|
+
variable that is unset raises `MissingEnvVarError` before `default` is applied. A
|
|
245
|
+
variable set to an empty string counts as present and still uses `default`.
|
|
246
|
+
|
|
234
247
|
## Advanced topics
|
|
235
248
|
|
|
236
249
|
### Exception handling
|
|
@@ -260,10 +273,6 @@ InvalidBoolValueError: Invalid boolean value for MY_VAR: 'maybe'
|
|
|
260
273
|
Expected falsy: 0, false, no, off
|
|
261
274
|
```
|
|
262
275
|
|
|
263
|
-
`ConfigError` is raised when a config file is found but malformed (for example,
|
|
264
|
-
`strict = "yes"` instead of `strict = true`). It carries the offending path on
|
|
265
|
-
`e.path`.
|
|
266
|
-
|
|
267
276
|
### Logging
|
|
268
277
|
|
|
269
278
|
`envbool` logs through the standard `logging` module under the `"envbool"`
|
|
@@ -279,7 +288,6 @@ logging.getLogger("envbool").addHandler(logging.StreamHandler())
|
|
|
279
288
|
|
|
280
289
|
| Level | When |
|
|
281
290
|
| --- | --- |
|
|
282
|
-
| `DEBUG` | A config file was discovered and loaded, or none was found. |
|
|
283
291
|
| `WARNING` | An unrecognized value fell through in lenient mode (only when `warn=True`). |
|
|
284
292
|
| `WARNING` | The truthy and falsy sets overlap (truthy wins). |
|
|
285
293
|
|
|
@@ -302,24 +310,20 @@ else:
|
|
|
302
310
|
|
|
303
311
|
### Testing code that uses envbool
|
|
304
312
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
autouse fixture:
|
|
313
|
+
If your tests call `set_defaults()`, reset it between tests with an autouse
|
|
314
|
+
fixture so overrides don't leak across the suite:
|
|
308
315
|
|
|
309
316
|
```python
|
|
310
317
|
# conftest.py
|
|
311
318
|
import pytest
|
|
312
|
-
from envbool
|
|
319
|
+
from envbool import reset_defaults
|
|
313
320
|
|
|
314
321
|
@pytest.fixture(autouse=True)
|
|
315
|
-
def
|
|
322
|
+
def _reset_envbool_defaults():
|
|
316
323
|
yield
|
|
317
|
-
|
|
324
|
+
reset_defaults()
|
|
318
325
|
```
|
|
319
326
|
|
|
320
|
-
`_reset_config()` is private but stable and exists for exactly this purpose; it
|
|
321
|
-
clears the cache under a lock, so it is safe to call from any thread.
|
|
322
|
-
|
|
323
327
|
## Contributing
|
|
324
328
|
|
|
325
329
|
Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for development
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "envbool"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.4.0"
|
|
4
4
|
description = "A small Python library and CLI tool for coercing environment variables (and arbitrary strings) into boolean values."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [{ name = "Kyle O'Malley", email = "j.kyle.omalley@gmail.com" }]
|
|
7
7
|
requires-python = ">=3.11"
|
|
8
8
|
license = "MIT"
|
|
9
9
|
keywords = ["environment variables", "boolean", "configuration", "env", "coerce"]
|
|
10
|
-
dependencies = [
|
|
11
|
-
"platformdirs>=4.9.6",
|
|
12
|
-
]
|
|
10
|
+
dependencies = []
|
|
13
11
|
classifiers = [
|
|
14
|
-
"Development Status ::
|
|
12
|
+
"Development Status :: 4 - Beta",
|
|
15
13
|
"Intended Audience :: Developers",
|
|
16
14
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
17
15
|
"Topic :: Utilities",
|
|
@@ -40,6 +38,7 @@ build-backend = "uv_build"
|
|
|
40
38
|
|
|
41
39
|
[dependency-groups]
|
|
42
40
|
dev = [
|
|
41
|
+
"hypothesis>=6.155.5",
|
|
43
42
|
"pre-commit>=4.5.1",
|
|
44
43
|
"pytest>=9.0.3",
|
|
45
44
|
"pytest-cov>=7.1.0",
|
|
@@ -11,32 +11,46 @@ For except clauses, envbool.exceptions is also importable by name:
|
|
|
11
11
|
Available names:
|
|
12
12
|
envbool() -- read an env var and coerce to bool (primary API)
|
|
13
13
|
to_bool() -- coerce an arbitrary string to bool (no os.environ)
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
set_defaults() -- set process-level strict/warn/truthy/falsy defaults
|
|
15
|
+
get_defaults() -- inspect the active process-level Defaults
|
|
16
|
+
reset_defaults() -- restore built-in defaults (for test fixtures)
|
|
17
|
+
Defaults -- frozen dataclass returned by get_defaults()
|
|
16
18
|
DEFAULT_TRUTHY -- built-in truthy set (frozenset)
|
|
17
19
|
DEFAULT_FALSY -- built-in falsy set (frozenset)
|
|
18
20
|
EnvBoolError -- base exception for all envbool errors
|
|
19
21
|
InvalidBoolValueError -- raised in strict mode for unrecognized values
|
|
20
|
-
|
|
22
|
+
MissingEnvVarError -- raised by envbool(required=True) when a var is unset
|
|
21
23
|
"""
|
|
22
24
|
# All implementation lives in private underscore-prefixed modules so the public
|
|
23
25
|
# surface can be reshaped without breaking imports. Do not import from _core,
|
|
24
26
|
# _env, _config, _cli, or _defaults directly.
|
|
25
27
|
|
|
26
|
-
from envbool._config import EnvBoolConfig, load_config
|
|
27
28
|
from envbool._core import to_bool
|
|
28
|
-
from envbool._defaults import
|
|
29
|
+
from envbool._defaults import (
|
|
30
|
+
DEFAULT_FALSY,
|
|
31
|
+
DEFAULT_TRUTHY,
|
|
32
|
+
Defaults,
|
|
33
|
+
get_defaults,
|
|
34
|
+
reset_defaults,
|
|
35
|
+
set_defaults,
|
|
36
|
+
)
|
|
29
37
|
from envbool._env import envbool
|
|
30
|
-
from envbool.exceptions import
|
|
38
|
+
from envbool.exceptions import (
|
|
39
|
+
EnvBoolError,
|
|
40
|
+
InvalidBoolValueError,
|
|
41
|
+
MissingEnvVarError,
|
|
42
|
+
)
|
|
31
43
|
|
|
32
44
|
__all__ = [
|
|
33
45
|
"DEFAULT_FALSY",
|
|
34
46
|
"DEFAULT_TRUTHY",
|
|
35
|
-
"
|
|
36
|
-
"EnvBoolConfig",
|
|
47
|
+
"Defaults",
|
|
37
48
|
"EnvBoolError",
|
|
38
49
|
"InvalidBoolValueError",
|
|
50
|
+
"MissingEnvVarError",
|
|
39
51
|
"envbool",
|
|
40
|
-
"
|
|
52
|
+
"get_defaults",
|
|
53
|
+
"reset_defaults",
|
|
54
|
+
"set_defaults",
|
|
41
55
|
"to_bool",
|
|
42
56
|
]
|