[New-bugs-announce] [issue44864] [argparse] Do not translate user-provided strings in `ArgumentParser.add_subparsers()`

Jérémie Detrey report at bugs.python.org
Sun Aug 8 04:42:38 EDT 2021


New submission from Jérémie Detrey <Jeremie.Detrey at loria.fr>:

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.

----------
components: Library (Lib)
messages: 399212
nosy: jdetrey
priority: normal
severity: normal
status: open
title: [argparse] Do not translate user-provided strings in `ArgumentParser.add_subparsers()`
type: behavior
versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44864>
_______________________________________


More information about the New-bugs-announce mailing list