Title: Argparse: "Default" interacts incorrectly/non-intuitively with action='append' and action='extend' · Issue #110131 · python/cpython · GitHub
Open Graph Title: Argparse: "Default" interacts incorrectly/non-intuitively with action='append' and action='extend' · Issue #110131 · python/cpython
X Title: Argparse: "Default" interacts incorrectly/non-intuitively with action='append' and action='extend' · Issue #110131 · python/cpython
Description: Bug report Bug description: The documentation for default claims: The default keyword argument of add_argument(), whose value defaults to None, specifies what value should be used if the command-line argument is not present which (implic...
Open Graph Description: Bug report Bug description: The documentation for default claims: The default keyword argument of add_argument(), whose value defaults to None, specifies what value should be used if the command-li...
X Description: Bug report Bug description: The documentation for default claims: The default keyword argument of add_argument(), whose value defaults to None, specifies what value should be used if the command-li...
Opengraph URL: https://github.com/python/cpython/issues/110131
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Argparse: \"Default\" interacts incorrectly/non-intuitively with action='append' and action='extend'","articleBody":"# Bug report\n\n### Bug description:\n\nThe documentation for `default` claims:\n> The default keyword argument of [add_argument()](https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument), whose value defaults to None, specifies what value should be used if the command-line argument is not present\nwhich (implicitly) sets the expectation that the default is not used if the command-line argument is present\n\nin it's defense, the strange behaviour is documented in `append` but not `extend`\n> If the default value is non-empty, the default elements will be present in the parsed value for the option, with any values from the command line appended after those default values\n\nHowever, this behaviour is non-intuitive. The fact that the values will be prepended to the end-user's input is not obvious, furthermore, the end-user is *unable* to specify a set of parameters that don't contain undesired default values.\n\n```python\n DEFAULT_SET=['a','b','c','d']\n parser = argparse.ArgumentParser()\n parser.add_argument('--items',nargs='+',action='extend',dest='items',choices=DEFAULT_SET,default=DEFAULT_SET)\n args = parser.parse_args()\n\n print(args.items)\n```\n> $ python3 example.py --items a --items b c # expected: items will contain 'a', 'b', and 'c'\n['a', 'b', 'c', 'd', 'a', 'b', 'c']\n\n\nNow it's fair to say that there's a workaround:\n\n```python\n parser.add_argument('--items',nargs='+',action='extend',dest='items',choices=DEFAULT_SET,default=None)\n args = parser.parse_args()\n if args.items == None:\n args.items = DEFAULT_SET \n```\nhowever, isn't this the pattern that `default=` aims to eliminate/alleviate in the first place? \n\nIf the purpose of DEFAULT is to supply a value in the absense of user input, the fact that it supplies a value even when the user supplies input is an undesired *side effect* and not really default behaviour.\n\nIf the code needs to *always* be processing a set of values regardless of user input, then that really should be clearly stated in the code/logic, rather than a side effect of the input parsing\n```python\n do_something(DEFAULT_SET + args.items) # no ambiguity\n```\n\nhope this makes sense. thank you!\n\n### CPython versions tested on:\n\n3.8\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-158155\n* gh-158352\n* gh-158353\n* gh-158354\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/rhetzler","@type":"Person","name":"rhetzler"},"datePublished":"2023-09-30T00:52:27Z","url":"https://github.com/python/cpython/issues/110131"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3ba487dc-5ae2-c454-5f15-ebce5ef13617 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F26:323204:1511684:1BC9CEB:6B01FA8C |
| html-safe-nonce | f36c8feead067e7eaffa3933af214baf4105d0297dea2041215c36fa1ed373fb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjI2OjMyMzIwNDoxNTExNjg0OjFCQzlDRUI6NkIwMUZBOEMiLCJ2aXNpdG9yX2lkIjoiMzQ0NTY3ODQ0NjA0OTYyMjY2OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 07494fa8b6f9321f55dfd5770f06aaddd25e062961d53f04d4dfa00191279375 |
| hovercard-subject-tag | issue:1920042693 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/110131/issue_layout |
| twitter:image | https://opengraph.githubassets.com/52ae5f6a00ef34ab01fde66275343f930275efb5141fc332902fba81d95ac59d/python/cpython/issues/110131 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/52ae5f6a00ef34ab01fde66275343f930275efb5141fc332902fba81d95ac59d/python/cpython/issues/110131 |
| og:image:alt | Bug report Bug description: The documentation for default claims: The default keyword argument of add_argument(), whose value defaults to None, specifies what value should be used if the command-li... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rhetzler |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width