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

Created on 2020-02-26 11:44 by remi.lapeyre, last changed 2020-02-26 16:05 by remi.lapeyre.

Pull Requests
URL Status Linked Edit
PR 18668 open dorosch, 2020-02-26 12:22
Messages (3)
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 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:
Date User Action Args
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