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.

classification
Title: Do not allow to pass FileType class object instead of instance in add_argument
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bethard, josh.r, mangrisano, miss-islington, paul.j3, zygocephalus
Priority: normal Keywords: patch

Created on 2019-06-04 13:22 by zygocephalus, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13805 merged zygocephalus, 2019-06-04 13:27
PR 13901 merged miss-islington, 2019-06-07 20:08
Messages (10)
msg344568 - (view) Author: Demid (zygocephalus) * Date: 2019-06-04 13:22
There is a possibility that someone (like me) accidentally will omit parentheses with FileType arguments after FileType, and parser will contain wrong file until someone will try to use it. 

Example:
parser = argparse.ArgumentParser()
parser.add_argument('-x', type=argparse.FileType)
msg344614 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2019-06-04 17:40
I don't see an easy way around this.  FileType is a class, and thus is a callable.  add_argument checks that the 'type' parameter is a callable (or a string in the registry).  Otherwise it gives the programmer a lot of freedom regarding this parameter.
msg344635 - (view) Author: Michele Angrisano (mangrisano) * Date: 2019-06-04 19:27
Reading the examples in the doc, it's clear the behavior when FileType takes an argument. What's the behavior of FileType when is called without any argument?
msg344639 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2019-06-04 20:44
The docs specify what argparse.FileType() does via the default parameters: https://docs.python.org/3/library/argparse.html#argparse.FileType

If you mean what does it do when you fail to call it at all (passing type=argparse.FileType), the answer is the same as any other class: It tries to construct a FileType using the user provided argument as the sole positional argument. So if the user passes a valid mode string, it works, and returns a FileType object with that mode string, otherwise it barfs and dumps an error message for passing an invalid argument for FileType.
msg344657 - (view) Author: Michele Angrisano (mangrisano) * Date: 2019-06-04 23:35
Yes, I meant that. Thanks! :)
msg344658 - (view) Author: Demid (zygocephalus) * Date: 2019-06-04 23:50
If you will run `python test.py hello.txt, where test.py is:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('echo', type=argparse.FileType)
args = parser.parse_args()
print(args.echo)

You will receive:
FileType('hello.txt')

I think that can be confusing for someone who will forget to invoke FileType constructor.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Tuesday, June 4, 2019 10:27 PM, Michele Angrisano <report@bugs.python.org> wrote:

>
>
> Michele Angrisanomichele.angrisano@gmail.com added the comment:
>
> Reading the examples in the doc, it's clear the behavior when FileType takes an argument. What's the behavior of FileType when is called without any argument?
>
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> nosy: +mangrisano
>
> Python tracker report@bugs.python.org
> https://bugs.python.org/issue37150
msg344659 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2019-06-05 00:37
Ah, right. It doesn't actually validate the mode string (it just stores it for when open is called, assuming open will validate it). So yeah, it silently accepts any string, not just valid mode strings. Not a contractual guarantee or anything, just how FileType.__init__ is implemented.
msg344724 - (view) Author: Demid (zygocephalus) * Date: 2019-06-05 14:09
What if I will add mode check in FileType.__init__?
msg345006 - (view) Author: miss-islington (miss-islington) Date: 2019-06-07 20:08
New changeset 03d5831a2d62c68654ec223168e574cd546efbf6 by Miss Islington (bot) (zygocephalus) in branch 'master':
bpo-37150: Throw ValueError if FileType class object was passed in add_argument (GH-13805)
https://github.com/python/cpython/commit/03d5831a2d62c68654ec223168e574cd546efbf6
msg345011 - (view) Author: miss-islington (miss-islington) Date: 2019-06-07 21:12
New changeset 606ac581e2451c420117c55632f0fe13d4cec2cd by Miss Islington (bot) in branch '3.8':
bpo-37150: Throw ValueError if FileType class object was passed in add_argument (GH-13805)
https://github.com/python/cpython/commit/606ac581e2451c420117c55632f0fe13d4cec2cd
History
Date User Action Args
2022-04-11 14:59:16adminsetgithub: 81331
2019-06-07 21:12:36asvetlovsetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: - Python 2.7, Python 3.7
2019-06-07 21:12:04miss-islingtonsetmessages: + msg345011
2019-06-07 20:08:58miss-islingtonsetpull_requests: + pull_request13776
2019-06-07 20:08:41miss-islingtonsetnosy: + miss-islington
messages: + msg345006
2019-06-07 19:30:32mangrisanosetnosy: + bethard
2019-06-05 14:09:46zygocephalussetmessages: + msg344724
2019-06-05 00:37:10josh.rsetmessages: + msg344659
2019-06-04 23:50:25zygocephalussetmessages: + msg344658
2019-06-04 23:35:16mangrisanosetmessages: + msg344657
2019-06-04 20:44:14josh.rsetnosy: + josh.r
messages: + msg344639
2019-06-04 19:27:18mangrisanosetnosy: + mangrisano
messages: + msg344635
2019-06-04 17:40:16paul.j3setmessages: + msg344614
2019-06-04 15:44:53xtreaksetnosy: + paul.j3
2019-06-04 13:41:57SilentGhostsetversions: - Python 3.5, Python 3.6
2019-06-04 13:27:42zygocephalussetkeywords: + patch
stage: patch review
pull_requests: + pull_request13690
2019-06-04 13:22:00zygocephaluscreate