classification
Title: TypeError for Tutorial 10.3 Example 2
Type: behavior 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: David Goldsmith, docs@python, rhettinger, terry.reedy
Priority: normal Keywords: patch

Created on 2019-11-03 23:31 by David Goldsmith, last changed 2019-11-18 06:17 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17207 merged rhettinger, 2019-11-17 21:56
PR 17212 merged miss-islington, 2019-11-18 06:06
PR 17213 merged miss-islington, 2019-11-18 06:06
Messages (7)
msg355914 - (view) Author: David Goldsmith (David Goldsmith) Date: 2019-11-03 23:31
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?
msg356273 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-11-08 23:29
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}')
msg356291 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-09 08:13
I'll replace this example with more representative one that works.
msg356839 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-11-18 03:40
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'.
msg356844 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-18 06:06
New changeset 04c79d6088a22d467f04dbe438050c26de22fa85 by Raymond Hettinger in branch 'master':
bpo-38678: Improve argparse example in tutorial (GH-17207)
https://github.com/python/cpython/commit/04c79d6088a22d467f04dbe438050c26de22fa85
msg356846 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-18 06:17
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/39134b374fd506c5f0f6d232e259ba48c651d88f
msg356847 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-18 06:17
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/d2faac63af007e52620c642dfcc576b787b55dcd
History
Date User Action Args
2019-11-18 06:17:31rhettingersetmessages: + msg356847
2019-11-18 06:17:17rhettingersetmessages: + msg356846
2019-11-18 06:10:05rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-11-18 06:06:50miss-islingtonsetpull_requests: + pull_request16716
2019-11-18 06:06:42miss-islingtonsetpull_requests: + pull_request16715
2019-11-18 06:06:22rhettingersetmessages: + msg356844
2019-11-18 03:44:53terry.reedysetversions: + Python 3.7, Python 3.8, Python 3.9
2019-11-18 03:40:36terry.reedysetmessages: + msg356839
2019-11-17 21:56:19rhettingersetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request16712
2019-11-09 08:13:26rhettingersetassignee: docs@python -> rhettinger

messages: + msg356291
nosy: + rhettinger
2019-11-08 23:29:23terry.reedysetnosy: + terry.reedy
title: TypeError raised trying to run TPT 10.3 Example 2 in Python 3.4.3 -> TypeError for Tutorial 10.3 Example 2
messages: + msg356273

type: crash -> behavior
stage: needs patch
2019-11-03 23:31:18David Goldsmithcreate