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

[argparse] Do not translate user-provided strings in ArgumentParser.add_subparsers() #89027

Open
jdetrey mannequin opened this issue Aug 8, 2021 · 5 comments
Open

[argparse] Do not translate user-provided strings in ArgumentParser.add_subparsers() #89027

jdetrey mannequin opened this issue Aug 8, 2021 · 5 comments
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@jdetrey
Copy link
Mannequin

jdetrey mannequin commented Aug 8, 2021

BPO 44864
Nosy @rhettinger, @terryjreedy, @jdetrey
PRs
  • bpo-44864: Do not translate user-provided strings in ArgumentParser.add_subparsers() #27667
  • 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 = None
    created_at = <Date 2021-08-08.08:42:38.108>
    labels = ['type-feature', 'library', '3.11']
    title = '[argparse] Do not translate user-provided strings in `ArgumentParser.add_subparsers()`'
    updated_at = <Date 2021-08-25.09:24:41.860>
    user = 'https://github.com/jdetrey'

    bugs.python.org fields:

    activity = <Date 2021-08-25.09:24:41.860>
    actor = 'jdetrey'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-08-08.08:42:38.108>
    creator = 'jdetrey'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44864
    keywords = ['patch']
    message_count = 5.0
    messages = ['399212', '399568', '400226', '400231', '400247']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'terry.reedy', 'python-dev', 'paul.j3', 'jdetrey']
    pr_nums = ['27667']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue44864'
    versions = ['Python 3.11']

    @jdetrey
    Copy link
    Mannequin Author

    jdetrey mannequin commented Aug 8, 2021

    Dear all,

    In the argparse module, the ArgumentParser.add_subparsers() method may call the _() translation function on user-provided strings. See e.g. 3.9/Lib/argparse.py:1776 and 3.9/Lib/argparse.py:L1777:

        def add_subparsers(self, **kwargs):
            # [...]
            if 'title' in kwargs or 'description' in kwargs:
                title = _(kwargs.pop('title', 'subcommands'))
                description = _(kwargs.pop('description', None))

    When elements 'title' and/or 'description' are set in kwargs, they will be popped from the dictionary and then fed to _(). However, these are user-provided strings, and it seems to me that translating them should be the user's responsibility. This seems to be the expected behavior for all other user-provided strings in the argparse module: see e.g. the ArgumentParser's description parameter (in 3.9/Lib/argparse.py:1704 then 3.9/Lib/argparse.py:1312), which never gets translated by the argparse module.

    However, the default title string 'subcommands' should still be localized. Therefore, I'd suggest restricting the call to _() to this string only, as in the following:

                title = kwargs.pop('title', _('subcommands'))
                description = kwargs.pop('description', None)

    I'll submit a pull request with this change.

    Kind regards,
    Jérémie.

    @jdetrey jdetrey mannequin added type-bug An unexpected behavior, bug, or error 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir labels Aug 8, 2021
    @terryjreedy
    Copy link
    Member

    Jérémie, what does the doc say about translation? This will likely determine whether the is a bug report or a future-version-only enhancement request.

    The PR changes the two lines as indicated above and the CLA is signed. A blurb will be needed if this is to me merged. (Not my decision.)

    @terryjreedy terryjreedy removed 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes type-bug An unexpected behavior, bug, or error labels Aug 13, 2021
    @jdetrey
    Copy link
    Mannequin Author

    jdetrey mannequin commented Aug 24, 2021

    Hi Terry,

    Thanks for the feedback!

    I've just added a blurb to the PR.

    Regarding the issue type, even though this is indeed translation-related, I'd lean toward a bug report rather than an enhancement request: the fact that user-provided strings get fed to the localization function by the argparse module is contrary to the expected behavior, and thus should qualify as a bug.

    However, since this is very minor, I'm totally fine with this being applied only to future versions!

    Thanks again!

    Kind regards,
    Jérémie.

    @terryjreedy
    Copy link
    Member

    'bug', for the tracker, mean a divergence between implementation and doc. Hence my question about the current doc. We could decide that either is wrong and should be changed.

    A change to meet 'expectations' and fix a 'design bug' is an enhancement.

    @jdetrey
    Copy link
    Mannequin Author

    jdetrey mannequin commented Aug 25, 2021

    Hi,

    Thank you for the clarification!

    In fact, I'm afraid the localization feature of argparse is undocumented. (At least, I couldn't find anything about it in the documentation.) A hint that modules should only take care of their own strings can however be inferred from https://docs.python.org/3/library/gettext.html#deferred-translations: “In most coding situations, strings are translated where they are coded.”

    But the fact remains that the argparse module deals with user-provided strings in a non-consistent way: all other user-provided strings (such as the usage, description, or epilog parameters to ArgumentParser's constructor, for instance) are not translated by argparse (leaving this responsibility with the caller); only the title and description parameters to ArgumentParser.add_subparsers() will be translated if provided.

    This behavior discrepancy is what prompted me to report this as a bug in the first place, but since this is an undocumented feature anyway, I'm perfectly fine with this being categorized as an enhancement instead.

    Kind regards,
    Jérémie.

    @jdetrey jdetrey mannequin added the type-feature A feature request or enhancement label Aug 25, 2021
    @jdetrey jdetrey mannequin added the type-feature A feature request or enhancement label Aug 25, 2021
    @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.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    Status: Features
    Status: No status
    Development

    No branches or pull requests

    1 participant