This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: argparse.FileType does not expand tilde "~"
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Julian, berker.peksag, garthy, martin.panter, paul.j3, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-12-12 10:31 by garthy, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
19959-argparse_filetype_expanduser.patch garthy, 2013-12-12 10:33 review
19959-argparse_filetype_expanduser_plus_doc.patch garthy, 2013-12-12 13:55 review
Messages (9)
msg205949 - (view) Author: Garth Bushell (garthy) Date: 2013-12-12 10:31
argparse.FileType does not expand tilde "~". 

This would be useful to take file parameters beginning with "~" and use os.path.expanduser to expand this.
msg205950 - (view) Author: Garth Bushell (garthy) Date: 2013-12-12 10:33
Add patch to add an option to argparse.FileType to enable expanduser.
msg205954 - (view) Author: Garth Bushell (garthy) Date: 2013-12-12 13:55
Update patch to include docs.
msg206206 - (view) Author: Julian Berman (Julian) * Date: 2013-12-15 00:37
Why not take this a step further and make it take a callable that's expected to take the command line argument and coerce it into a path
msg206580 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2013-12-19 05:46
Normally the unix shell takes care of this expansion.  If I call a simple script that prints sys.argv and runs a simple parser, I get:

    2135:~/mypy$ python2.7 ~/mypy/test.txt
    ['', '/home/paul/mypy/test.txt']
    Namespace(file=<open file '/home/paul/mypy/test.txt', mode 'r'...)

I have to quote the argument '~/mypy/' to bypass this expansion.

This is under Linux (bash).  Is Windows or Mac different?

My impression is that `FileType` has been provided as an example of a custom `type`, as opposed to a comprehensive file handler.  But I can't quote a developer on this. is a bug report that worries that 'argparse.FileType opens a file and never closes it'.  There are some comments there about whether it is appropriate to expand on the capabilities of FileType.
msg262546 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-03-28 02:04
I think this change is not appropriate for Unix, because it is redundant and would conflict with normal shell expansion. I’m not familiar with Windows, but I suspect it is not appropriate there either (neither Windows command shells nor argparse do wildcard * and ? glob expansion, right?). Pending response from Garth or Julian.
msg262555 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-03-28 05:20
I'm with Martin.
msg262569 - (view) Author: Julian Berman (Julian) * Date: 2016-03-28 12:19
My support (or really, asking for a more generic callable) was to enable other use cases, not this one specifically -- e.g., allowing for constructing a pathlib Path or a twisted.python.filepath.FilePath without needing to write one's own argparse action.

For ~ specifically -- it's true if you're going to go via a shell it'd be expanded first, but I'm not sure that'd count as "conflicting" with the shell's expansion, it just won't be needed. Consider that twisted.python.filepath.FilePath for example, expands ~ in its constructor -- people often construct paths in other places and occasionally expansion is convenient. Like in test cases.

I certainly don't feel strongly about the patch though, especially as-is.
msg262807 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-04-03 01:04
I’m not super familiar with argparse, but to get a Path or FilePath object, can’t you use that directly instead of argparse’s FileType class?

The proposal would conflict with escaping a literal tilde (~) in the shell. It would make it harder for the end user to specify a real file name that starts with a tilde.
Date User Action Args
2022-04-11 14:57:55adminsetgithub: 64158
2016-04-03 01:04:08martin.pantersetstatus: open -> closed

messages: + msg262807
2016-03-28 12:19:49Juliansetstatus: pending -> open

messages: + msg262569
2016-03-28 05:20:39serhiy.storchakasetstatus: open -> pending
2016-03-28 05:20:24serhiy.storchakasetstatus: pending -> open
nosy: + serhiy.storchaka
messages: + msg262555

2016-03-28 02:04:22martin.pantersetstatus: open -> pending

nosy: + martin.panter
messages: + msg262546

resolution: rejected
2013-12-19 05:46:30paul.j3setnosy: + paul.j3
messages: + msg206580
2013-12-15 00:37:56Juliansetnosy: + Julian
messages: + msg206206
2013-12-12 13:55:19garthysetfiles: + 19959-argparse_filetype_expanduser_plus_doc.patch

messages: + msg205954
2013-12-12 13:33:06berker.peksagsetnosy: + berker.peksag
2013-12-12 10:34:14garthysettype: enhancement
2013-12-12 10:33:42garthysetfiles: + 19959-argparse_filetype_expanduser.patch
keywords: + patch
messages: + msg205950
2013-12-12 10:31:03garthycreate