This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Bug in example of collections.ChainMap
Type: Stage: resolved
Components: Documentation Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: docs@python, jonathan.scholbach, rhettinger
Priority: low Keywords: patch

Created on 2019-11-11 22:39 by jonathan.scholbach, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17108 merged jonathan.scholbach, 2019-11-11 22:42
PR 17109 merged miss-islington, 2019-11-12 00:50
PR 17110 merged miss-islington, 2019-11-12 00:50
Messages (5)
msg356398 - (view) Author: Jonathan Scholbach (jonathan.scholbach) * Date: 2019-11-11 22:39
Below "Examples and Recipes", the Documentation of collections.ChainMap has an "Example of letting user specified command-line arguments take precedence over environment variables which in turn take precedence over default values:" In there, a ChainMap is created which represents the default values, updated by the command-line arguments, if they have been set. The relevant code snippet is the following:
   
    parser = argparse.ArgumentParser()
    parser.add_argument('-u', '--user')
    parser.add_argument('-c', '--color')
    namespace = parser.parse_args()
    command_line_args = {k:v for k, v in vars(namespace).items() if v}

If user passes an empty string as value for any of the command-line arguments, that argument would not appear in `command_line_args` (because the boolean value of empty string is `False`). However, passing the empty string as a value for a command-line argument, would reflect the intent of overwriting the default value (setting it to the empty string). With the current example code, this would erroneously not be reflected in the `command_line_args`. This is caused by checking for the boolean of `v` instead of checking for `v` not being `None`. So, this should be handled correctly by writing

    command_line_args = {k: v for k, v in vars(namespace).items() if v is not None}
msg356404 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-12 00:13
The example was just meant to be a quick snippet to suggest a possible use case.  It isn't a stand-alone application.  However, stylistically, this is an improvement and I'll accept some variation of the PR.
msg356405 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-12 00:49
New changeset 98480cef9dba04794bd61c7e7cca643d384c8c35 by Raymond Hettinger (Jonathan Scholbach) in branch 'master':
bpo-38771:  Explict test for None in code example (GH-17108)
https://github.com/python/cpython/commit/98480cef9dba04794bd61c7e7cca643d384c8c35
msg356406 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-12 00:58
New changeset cb1c06e3bfbce7f4e8dcb0db84bcee54c8d4fa21 by Raymond Hettinger (Miss Islington (bot)) in branch '3.7':
bpo-38771:  Explict test for None in code example (GH-17108) (GH-17110)
https://github.com/python/cpython/commit/cb1c06e3bfbce7f4e8dcb0db84bcee54c8d4fa21
msg356407 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-12 00:59
New changeset d8e08456025d9349abbf76035c821d3f7b2d722a by Raymond Hettinger (Miss Islington (bot)) in branch '3.8':
bpo-38771:  Explict test for None in code example (GH-17108) (GH-17109)
https://github.com/python/cpython/commit/d8e08456025d9349abbf76035c821d3f7b2d722a
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 82952
2019-11-12 00:59:17rhettingersetmessages: + msg356407
2019-11-12 00:58:55rhettingersetmessages: + msg356406
2019-11-12 00:52:29rhettingersetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.7
2019-11-12 00:50:14miss-islingtonsetpull_requests: + pull_request16617
2019-11-12 00:50:08miss-islingtonsetpull_requests: + pull_request16616
2019-11-12 00:49:58rhettingersetmessages: + msg356405
2019-11-12 00:13:03rhettingersetpriority: normal -> low

messages: + msg356404
versions: - Python 3.5, Python 3.6, Python 3.7
2019-11-12 00:00:16rhettingersetassignee: docs@python -> rhettinger

nosy: + rhettinger
2019-11-11 22:42:58jonathan.scholbachsetkeywords: + patch
stage: patch review
pull_requests: + pull_request16614
2019-11-11 22:39:04jonathan.scholbachcreate