classification
Title: argparse: convert_arg_line_to_args does not actually expect self argument
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Mariatta, berker.peksag, docs@python, python-dev, r.david.murray, siccegge
Priority: normal Keywords: patch

Created on 2016-10-16 16:19 by siccegge, last changed 2016-10-17 16:48 by siccegge. This issue is now closed.

Files
File name Uploaded Description Edit
issue28455.patch Mariatta, 2016-10-17 02:50 review
Messages (6)
msg278771 - (view) Author: Christoph Egger (siccegge) Date: 2016-10-16 16:19
Hi!

Both the 3.4 version and the current version of python documentation wrt the argparse module imply convert_arg_line_to_args replacements needs to accept two arguments while it acutally only works with one. (Not completely sure about details but documentation really could be clearer!)

https://docs.python.org/3.4/library/argparse.html#argparse.ArgumentParser.convert_arg_line_to_args

Example from documentation

    def convert_arg_line_to_args(self, arg_line):
        return arg_line.split()

If codeparser = argparse.ArgumentParser actually does

    def convert_arg_line_to_args(self, arg_line):
        return arg_line.split()
    parser = argparse.ArgumentParser()    
    parser.convert_arg_line_to_args = convert_arg_line_to_args

The code fails

  File "/usr/lib/python3.5/argparse.py", line 1735, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib/python3.5/argparse.py", line 1767, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib/python3.5/argparse.py", line 1779, in _parse_known_args
    arg_strings = self._read_args_from_files(arg_strings)
  File "/usr/lib/python3.5/argparse.py", line 2037, in _read_args_from_files
    for arg in self.convert_arg_line_to_args(arg_line):
  TypeError: convert_arg_line_to_args() missing 1 required positional argument: 'arg_line'
msg278775 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-10-16 18:47
The documentation assumes you know how python class methods work, but I agree that the wording is not entirely obvious even then and could be improved.  In particular it should make clear that the example shows how to define the function for subclassing or assignment to the class object.  If you assign it to the instance, as in your example, then indeed self does not get passed and you want a true single argument function.
msg278791 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2016-10-17 02:50
Hello,

I updated the documentation with an example of how to override ArgumentParser class. Hope this is a clearer than before.

Please review.

Thanks :)
msg278792 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-10-17 03:14
New changeset 4f8f7403881f by Berker Peksag in branch '3.5':
Issue #28455: Clarify example of overriding the convert_arg_line_to_args method
https://hg.python.org/cpython/rev/4f8f7403881f

New changeset 0b29adb5c804 by Berker Peksag in branch '3.6':
Issue #28455: Merge from 3.5
https://hg.python.org/cpython/rev/0b29adb5c804

New changeset a293e5db9083 by Berker Peksag in branch 'default':
Issue #28455: Merge from 3.6
https://hg.python.org/cpython/rev/a293e5db9083
msg278793 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-10-17 03:17
Thanks! I removed the import statement and simplified the last sentence a bit.
msg278817 - (view) Author: Christoph Egger (siccegge) Date: 2016-10-17 16:48
Looks quite helpfull indeed to me! Thanks!
History
Date User Action Args
2016-10-17 16:48:55sicceggesetmessages: + msg278817
2016-10-17 03:17:26berker.peksagsetstatus: open -> closed

versions: + Python 3.5, Python 3.6, Python 3.7, - Python 3.4
nosy: + berker.peksag

messages: + msg278793
resolution: fixed
stage: resolved
2016-10-17 03:14:51python-devsetnosy: + python-dev
messages: + msg278792
2016-10-17 02:50:28Mariattasetfiles: + issue28455.patch

nosy: + Mariatta
messages: + msg278791

keywords: + patch
2016-10-16 18:47:39r.david.murraysetnosy: + r.david.murray
messages: + msg278775
2016-10-16 16:19:40sicceggecreate