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: 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, mitar, paul.j3
Priority: normal Keywords:

Created on 2015-07-28 02:08 by garyp, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 850 closed iamalbert, 2017-03-27 17:09
Messages (5)
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!
msg404959 - (view) Author: Mitar (mitar) * Date: 2021-10-25 11:08
I think the issue is that it is hard to subclass it. Ideally, call to open would be made through a new _open method which would then call it, and one could easily subclass that method if/when needed.
msg404977 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2021-10-25 16:12
Adding `newline` to `FileType` requires modifying both the `__init__` and `__call__` methods.  That's nearly the whole class.  I'd copy and edit, and forget about subclassing.
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 68927
2021-10-25 16:12:12paul.j3setmessages: + msg404977
2021-10-25 11:08:34mitarsetnosy: + mitar
messages: + msg404959
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