classification
Title: os.getenv documentation is misleading
Type: enhancement Stage: patch review
Components: Documentation Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: asvetlov, docs@python, dorosch, remi.lapeyre, serhiy.storchaka, sir-sigurd
Priority: normal Keywords: patch

Created on 2020-02-26 11:44 by remi.lapeyre, last changed 2021-07-02 21:03 by serhiy.storchaka.

Pull Requests
URL Status Linked Edit
PR 18668 open dorosch, 2020-02-26 12:22
Messages (4)
msg362689 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-02-26 11:44
The documentation states that "*key*, *default* and the result are str." at https://github.com/python/cpython/blame/3.8/Doc/library/os.rst#L224 but either I'm missing something or it's not actually true:

$ python -c 'import os; print(type(os.getenv("FOO")))'           
<class 'NoneType'>
$ python -c 'import os; print(type(os.getenv("FOO", default=1)))'
<class 'int'>

Only *key* needs to be a string as it is used to lookup the value in os.environ.

I think this can be fixed by a new contributor
msg362694 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-02-26 15:06
I consider a free type for the default as an implementation detail, not the encouraged approach.

Since the value is always a string, using the same type for the default sounds like a sane design for me.
msg362697 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-02-26 16:05
I don't really have a preference regarding saying that `default` should be a string or not but the phrase should still be reworded to be less confusing.

In typeshed it's documented with a generic type: https://github.com/python/typeshed/blob/master/stdlib/3/os/__init__.pyi#L363.
msg396879 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-07-02 21:03
It is not so uncommon to write:

    foo_rate = float(os.getenv("FOO_RATE", default_foo_rate))

where default_foo_rate is float.

In any case the default value for the default parameter is not a string.
History
Date User Action Args
2021-07-02 21:03:40serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg396879
2021-07-02 13:17:45sir-sigurdsetnosy: + sir-sigurd
2020-02-26 16:05:28remi.lapeyresetmessages: + msg362697
2020-02-26 15:06:39asvetlovsetnosy: + asvetlov
messages: + msg362694
2020-02-26 12:22:34doroschsetkeywords: + patch
nosy: + dorosch

pull_requests: + pull_request18024
stage: patch review
2020-02-26 11:44:26remi.lapeyrecreate