classification
Title: allow argparse.FileType to accept newline argument
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, garyp, paul.j3
Priority: normal Keywords:

Created on 2015-07-28 02:08 by garyp, last changed 2017-04-20 04:11 by berker.peksag. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 850 closed iamalbert, 2017-03-27 17:09
Messages (3)
msg247497 - (view) Author: Gary Peck (garyp) Date: 2015-07-28 02:08
argparse.FileType should support a "newline" argument that corresponds to the "newline" parameter to open(). In addition to more closely mirroring the open() API, this is also needed to properly use argparse.FileType with csv.reader() or csv.writer() (which require the file being passed in to have been opened with newline='').
msg247939 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-08-03 17:03
argparse.FileType serves 2 purposes

- handling filenames for simple quick shell-like scripts

- as a model for custom 'types', specifically one that uses a class to generate the desired type callable.

Other bug-issues show that it hasn't aged well.  It doesn't work nicely with the newer open/close context paradigm.  It needs changes to handle 'rb' and 'wb' modes.

I suppose it could be modified to accept the full range of parameters that the modern 'open' takes:

    open(file, mode='r', buffering=-1, encoding=None,
        errors=None, newline=None, closefd=True, opener=None) 

either explicitly, or as a pass through **kwargs.

But it is also something that you could easily subclass or modify for your own purposes.

Or just accept the filenames as strings, and do your own open/close after parsing.  This gives you more control over when and how the files are opened.
msg291941 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-04-20 04:11
I agree with paul j3. It would be nice to keep the current FileType API as is. The argparse documentation already encourages people to write their own custom types and I think this is a good use case for following the advice.

Thanks for the report!
History
Date User Action Args
2017-04-20 04:11:59berker.peksagsetstatus: open -> closed

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

messages: + msg291941
resolution: rejected
stage: resolved
2017-03-27 17:09:26iamalbertsetpull_requests: + pull_request750
2015-08-03 17:03:35paul.j3setnosy: + paul.j3
messages: + msg247939
2015-07-28 02:08:44garypcreate