New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow argparse.FileType to accept newline argument #68927
Comments
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=''). |
argparse.FileType serves 2 purposes
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. |
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! |
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. |
Adding |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: