On , I learnt ...
Python’s envparse
library has counter-intuitive boolean casting
If you use env.bool
from the
envparse
library to convert an
environment variable into a boolean Python variable then only an allow-list of
strings are considered truthy.
For example:
$ pip install envparse
$ X=0 python -c "from envparse import env; print(env.bool('X'))"
False
$ X=1 python -c "from envparse import env; print(env.bool('X'))"
True
$ X=2 python -c "from envparse import env; print(env.bool('X'))"
False
$ X=true python -c "from envparse import env; print(env.bool('X'))"
True
$ X=truthy python -c "from envparse import env; print(env.bool('X'))"
False
$ X="in Python this would cast to True" python -c "from envparse import env; print(env.bool('X'))"
False
The allow-list is:
BOOLEAN_TRUE_STRINGS = ('true', 'on', 'ok', 'y', 'yes', '1')
Since this differs from how Python casts strings to booleans, it is somewhat surprising.