[issue38678] TypeError raised trying to run TPT 10.3 Example 2 in Python 3.4.3
New submission from David Goldsmith <eulergaussriemann@gmail.com>: When I run the second example code of Section 10.3 of The Python Tutorial: import argparse from getpass import getuser parser = argparse.ArgumentParser(description='An argparse example.') parser.add_argument('name', nargs='?', default=getuser(), help='The name of someone to greet.') parser.add_argument('--verbose', '-v', action='count') args = parser.parse_args() greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3] print(f'{greeting}, {args.name}') if not args.verbose: print('Try running this again with multiple "-v" flags!') the greeting assignment line raises:
greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
Is this an artifact of me being so far behind in my Python version: Davids-MacBook-Air:~ DLG$ python3 Python 3.4.3 |Anaconda 2.4.0 (x86_64)| (default, Oct 20 2015, 14:27:51) [GCC 4.2.1 (Apple Inc. build 5577)] on darwin Or is it a bug in the example code in the doc? ---------- assignee: docs@python components: Documentation messages: 355914 nosy: David Goldsmith, docs@python priority: normal severity: normal status: open title: TypeError raised trying to run TPT 10.3 Example 2 in Python 3.4.3 type: crash _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Terry J. Reedy <tjreedy@udel.edu> added the comment: Crash means 'python stopped erroneously without a traceback'. Same exception in 3.8 and 3.9. Argparse uses None for a count of 0. I consider this a bug. If not changed, it should be documented. As argparse is, the example needs to recode None to 0. (Or, it could add '-v' to sys.argv.) With proper line wrapping, the result could be import argparse from getpass import getuser parser = argparse.ArgumentParser(description='An argparse example.') parser.add_argument('name', nargs='?', default=getuser(), # wrap help='The name of someone to greet.') parser.add_argument('--verbose', '-v', action='count') args = parser.parse_args() # new args.verbose = 0 if args.verbose is None else args.verbose greeting = (["Hi", "Hello", "Greetings! its very nice to meet you"] #wrap [args.verbose % 3]) print(f'{greeting}, {args.name}') ---------- nosy: +terry.reedy stage: -> needs patch title: TypeError raised trying to run TPT 10.3 Example 2 in Python 3.4.3 -> TypeError for Tutorial 10.3 Example 2 type: crash -> behavior _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: I'll replace this example with more representative one that works. ---------- assignee: docs@python -> rhettinger nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- keywords: +patch pull_requests: +16712 stage: needs patch -> patch review pull_request: https://github.com/python/cpython/pull/17207 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Terry J. Reedy <tjreedy@udel.edu> added the comment: Raymond's patch changes the tutorial example to something more useful for beginners. It also gives the proper fix for the old example, which is to add 'default=0' after "action='count'", but puts this in the proper place, the ref manual explanation of 'count'. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Change by Terry J. Reedy <tjreedy@udel.edu>: ---------- versions: +Python 3.7, Python 3.8, Python 3.9 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: New changeset 04c79d6088a22d467f04dbe438050c26de22fa85 by Raymond Hettinger in branch 'master': bpo-38678: Improve argparse example in tutorial (GH-17207) https://github.com/python/cpython/commit/04c79d6088a22d467f04dbe438050c26de2... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +16715 pull_request: https://github.com/python/cpython/pull/17212 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +16716 pull_request: https://github.com/python/cpython/pull/17213 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: New changeset 39134b374fd506c5f0f6d232e259ba48c651d88f by Raymond Hettinger (Miss Islington (bot)) in branch '3.8': bpo-38678: Improve argparse example in tutorial (GH-17207) (GH-17212) https://github.com/python/cpython/commit/39134b374fd506c5f0f6d232e259ba48c65... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: New changeset d2faac63af007e52620c642dfcc576b787b55dcd by Raymond Hettinger (Miss Islington (bot)) in branch '3.7': bpo-38678: Improve argparse example in tutorial (GH-17207) (GH-17213) https://github.com/python/cpython/commit/d2faac63af007e52620c642dfcc576b787b... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue38678> _______________________________________
participants (4)
-
David Goldsmith -
miss-islington -
Raymond Hettinger -
Terry J. Reedy