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

Do not allow to pass FileType class object instead of instance in add_argument #81331

Closed
zygocephalus mannequin opened this issue Jun 4, 2019 · 10 comments
Closed

Do not allow to pass FileType class object instead of instance in add_argument #81331

zygocephalus mannequin opened this issue Jun 4, 2019 · 10 comments
Labels
3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@zygocephalus
Copy link
Mannequin

zygocephalus mannequin commented Jun 4, 2019

BPO 37150
Nosy @MojoVampire, @miss-islington, @mangrisano, @zygocephalus
PRs
  • bpo-37150: Throw ValueError if FileType class object was passed in add_argument #13805
  • [3.8] bpo-37150: Throw ValueError if FileType class object was passed in add_argument (GH-13805) #13901
  • 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 2019-06-07.21:12:36.335>
    created_at = <Date 2019-06-04.13:22:00.658>
    labels = ['3.8', 'type-feature', 'library', '3.9']
    title = 'Do not allow to pass FileType class object instead of instance in add_argument'
    updated_at = <Date 2019-06-07.21:12:36.334>
    user = 'https://github.com/zygocephalus'

    bugs.python.org fields:

    activity = <Date 2019-06-07.21:12:36.334>
    actor = 'asvetlov'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-06-07.21:12:36.335>
    closer = 'asvetlov'
    components = ['Library (Lib)']
    creation = <Date 2019-06-04.13:22:00.658>
    creator = 'zygocephalus'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37150
    keywords = ['patch']
    message_count = 10.0
    messages = ['344568', '344614', '344635', '344639', '344657', '344658', '344659', '344724', '345006', '345011']
    nosy_count = 6.0
    nosy_names = ['bethard', 'paul.j3', 'josh.r', 'miss-islington', 'mangrisano', 'zygocephalus']
    pr_nums = ['13805', '13901']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue37150'
    versions = ['Python 3.8', 'Python 3.9']

    @zygocephalus
    Copy link
    Mannequin Author

    zygocephalus mannequin commented Jun 4, 2019

    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)

    @zygocephalus zygocephalus mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jun 4, 2019
    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Jun 4, 2019

    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.

    @mangrisano
    Copy link
    Mannequin

    mangrisano mannequin commented Jun 4, 2019

    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?

    @MojoVampire
    Copy link
    Mannequin

    MojoVampire mannequin commented Jun 4, 2019

    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.

    @mangrisano
    Copy link
    Mannequin

    mangrisano mannequin commented Jun 4, 2019

    Yes, I meant that. Thanks! :)

    @zygocephalus
    Copy link
    Mannequin Author

    zygocephalus mannequin commented Jun 4, 2019

    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

    @MojoVampire
    Copy link
    Mannequin

    MojoVampire mannequin commented Jun 5, 2019

    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.

    @zygocephalus
    Copy link
    Mannequin Author

    zygocephalus mannequin commented Jun 5, 2019

    What if I will add mode check in FileType.__init__?

    @miss-islington
    Copy link
    Contributor

    New changeset 03d5831 by Miss Islington (bot) (zygocephalus) in branch 'master':
    bpo-37150: Throw ValueError if FileType class object was passed in add_argument (GH-13805)
    03d5831

    @miss-islington
    Copy link
    Contributor

    New changeset 606ac58 by Miss Islington (bot) in branch '3.8':
    bpo-37150: Throw ValueError if FileType class object was passed in add_argument (GH-13805)
    606ac58

    @asvetlov asvetlov removed the 3.7 (EOL) end of life label Jun 7, 2019
    @asvetlov asvetlov closed this as completed Jun 7, 2019
    @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.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants