Skip to content
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

Closed
garyp mannequin opened this issue Jul 28, 2015 · 5 comments
Closed

allow argparse.FileType to accept newline argument #68927

garyp mannequin opened this issue Jul 28, 2015 · 5 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@garyp
Copy link
Mannequin

garyp mannequin commented Jul 28, 2015

BPO 24739
Nosy @mitar, @berkerpeksag
PRs
  • bpo-24739: allow argparse.FileType to accept 'newline' keyword argument #850
  • 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:

    assignee = None
    closed_at = <Date 2017-04-20.04:11:59.203>
    created_at = <Date 2015-07-28.02:08:44.263>
    labels = ['3.7', 'type-feature', 'library']
    title = 'allow argparse.FileType to accept newline argument'
    updated_at = <Date 2021-10-25.16:12:12.326>
    user = 'https://bugs.python.org/garyp'

    bugs.python.org fields:

    activity = <Date 2021-10-25.16:12:12.326>
    actor = 'paul.j3'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-04-20.04:11:59.203>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2015-07-28.02:08:44.263>
    creator = 'garyp'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 24739
    keywords = []
    message_count = 5.0
    messages = ['247497', '247939', '291941', '404959', '404977']
    nosy_count = 4.0
    nosy_names = ['mitar', 'berker.peksag', 'paul.j3', 'garyp']
    pr_nums = ['850']
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue24739'
    versions = ['Python 3.7']

    @garyp
    Copy link
    Mannequin Author

    garyp mannequin commented Jul 28, 2015

    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='').

    @garyp garyp mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jul 28, 2015
    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Aug 3, 2015

    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.

    @berkerpeksag
    Copy link
    Member

    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!

    @berkerpeksag berkerpeksag added the 3.7 (EOL) end of life label Apr 20, 2017
    @mitar
    Copy link
    Mannequin

    mitar mannequin commented Oct 25, 2021

    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.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Oct 25, 2021

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant