classification
Title: argparser documentation error
Type: resource usage Stage: resolved
Components: Parser Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: arsar7, lys.nikolaou, pablogsal, rhettinger
Priority: normal Keywords:

Created on 2021-06-16 21:26 by arsar7, last changed 2021-06-16 21:50 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26763 arsar7, 2021-06-16 21:26
Messages (3)
msg395963 - (view) Author: Arman Sargsyan (arsar7) * Date: 2021-06-16 21:26
URL - https://docs.python.org/3/howto/argparse.html

The following code will return a type error:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number")
args = parser.parse_args()
print(args.square**2)

This should be changed to:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number")
args = parser.parse_args()
print(int(args.square)**2)
msg395964 - (view) Author: Arman Sargsyan (arsar7) * Date: 2021-06-16 21:30
I have signed the CLA
msg395966 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-06-16 21:50
If you read the text afterwards, you'll see that raising a TypeError was the intended purpose of this example.  Just afterward, it shows how to use *type* to make the code correct.
History
Date User Action Args
2021-06-16 21:50:35rhettingersetstatus: open -> closed

assignee: rhettinger

nosy: + rhettinger
messages: + msg395966
resolution: not a bug
stage: resolved
2021-06-16 21:30:27arsar7setmessages: + msg395964
2021-06-16 21:26:12arsar7create