Closed felciano closed 3 years ago
Just in case anyone else runs across this, this is an ugly workaround that likely gets the behavior you want:
MY_VAR = config("MY_VAR", cast=str, default=None)
if MY_VAR == "None": MY_VAR = None
The default
value should aways be a string so cast would work the same way with env values or default values.
It looks like the
cast
operator takes precedence over default values, including castingNone
as a default value, with inconsistent results depending on what type is referenced bycast
:This means, for example, a
if not my_var
test for a missing environment variable whose default is None may or may not be true depending on whether acast=str
option was specified. While this may strictly adhere topython
semantics , wherestr(None)
returns the string"None"
, it seems counter-intuitive and possibly dangerous for a configuration library.Would it make sense to add a constraint that if both
cast
anddefault
options are used, then the default value must be of a validcast
type?