New submission from Saiyang Gou <gousaiyang...@gmail.com>:

It is generally a convention to design the repr string such that 
`eval(repr(obj))` can reproduce the object. Issue 24360 handles the special 
situation when arg name passed to `argparse.Namespace` is not a valid 
identifier:

>>> from argparse import Namespace
>>> Namespace(**{')': 3})
Namespace(**{')': 3})

However there is one more corner case to handle: the arg name could be a 
reserved keyword.

>>> Namespace(**{'return': 3})
Namespace(return=3)
>>> Namespace(return=3)
  File "<stdin>", line 1
    Namespace(return=3)
              ^
SyntaxError: invalid syntax

I noticed that the documentation of `str.isidentifier` tells me to also check 
for keywords with `keyword.iskeyword`. However `__debug__` is not considered a 
keyword by `keyword.iskeyword`, but cannot be assigned to:

>>> keyword.iskeyword('__debug__')
False
>>> Namespace(**{'__debug__':3})
Namespace(__debug__=3)
>>> Namespace(__debug__=3)
  File "<stdin>", line 1
SyntaxError: cannot assign to __debug__

I propose to enhance the arg name check in `argparse._AttributeHolder` from `if 
name.isidentifier():` to `if name.isidentifier() and not 
keyword.iskeyword(name) and name != '__debug__:'` to fix this. However this may 
slow down the argparse library since it will need to `import keyword` at the 
beginning, and `__repr__` will also be slower due to the added arg name checks.

By the way, when I came across issue 39076, I noticed that 
`types.SimpleNamespace` is not considering this problem at all:

>>> types.SimpleNamespace(**{')': 1, 'return': 2})
namespace()=1, return=2)

----------
components: Library (Lib)
messages: 366265
nosy: gousaiyang
priority: normal
severity: normal
status: open
title: argparse.Namespace __repr__ does not handle reserved keywords
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40265>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to