Skip to content

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

Closed

Description

@jdetrey
mannequin
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']

    Activity

    1. jdetrey commented on Aug 8, 2021

      jdetreymannequin
      MannequinAuthor

      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.

    2. added
      type-bugAn unexpected behavior, bug, or error
      3.11only security fixes
      stdlibStandard Library Python modules in the Lib/ directory
      on Aug 8, 2021
    3. 15 remaining items

    4. transferred this issue fromon Apr 10, 2022
    5. added
      3.12only security fixes
      3.13only security fixes
      3.14bugs and security fixes
      type-bugAn unexpected behavior, bug, or error
      and removed
      3.11only security fixes
      type-featureA feature request or enhancement
      on Sep 25, 2024
    6. serhiy-storchaka commented on Sep 25, 2024

      @serhiy-storchaka
      Member

      This is a bug (Tools/i18n/pygettext.py even emits warnings for this) and the fix should be backported.

    7. moved this from Features to Doc issues in Argparse issueson Sep 25, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.12only security fixes3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions