
Fair enough, thanks for the explanation On Wed, Mar 30, 2022 at 4:55 PM Paul Moore <p.f.moore@gmail.com> wrote:
On Wed, 30 Mar 2022 at 15:39, Adrian Torres Justo <atorresj@redhat.com> wrote:
Why not use just to_bool(getenv(key))?
We did use distutils.util.strtobool(os.getenv(key, default)) for a while, eventually we just built a helper that did the same because it's easier / more comfortable.
No. There would be hundreds different trivial implementations.
Can you elaborate? Here's more or less what I had in mind (using the os.getenv source code as template):
from distutils.util import strtobool
def getenv(key, default=None, to_bool=False): """Get an environment variable, return None if it doesn't exist. The optional second argument can specify an alternate default. key, default and the result are str.""" val = environ.get(key, default) return val if not to_bool else strtobool(val)
Sure, there are other ways to implement that are probably equally trivial, but why does that matter?
There are a lot of ways to interpret "convert to bool" - should "yes" and "no" be converted? What about "1" and "0"? Or "001", or "-1"? What should happen to unrecognised values? What if the environment variable doesn't exist? It's not at all obvious to me that strtobool is the definitive way of doing this (quite apart from the fact that distutils will be getting removed from the stdlib in Python 3.12 - see https://peps.python.org/pep-0632/).
What is so special about *this particular way* that it's worth including in the stdlib? Particularly when it's so easy to write for yourself.
Paul