[New-bugs-announce] [issue31655] SimpleNamespace accepts non-string keyword names

Serhiy Storchaka report at bugs.python.org
Sun Oct 1 02:36:42 EDT 2017


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

Usually non-string keyword names are rejected.

>>> def f(**kwargs): pass                                                                                                                                                                                                                    
...                                                                                                                                                                                                                                          
>>> f(**{0:0})
Traceback (most recent call last):                                                                                                                                                                                                           
  File "<stdin>", line 1, in <module>                                                                                                                                                                                                        
TypeError: f() keywords must be strings                                                                                                                                                                                                      
>>> dict(**{0:0})
Traceback (most recent call last):                                                                                                                                                                                                           
  File "<stdin>", line 1, in <module>                                                                                                                                                                                                        
TypeError: keywords must be strings                                                                                                                                                                                                          

There are checks in multiple places that satisfy this: in _PyEval_EvalCodeWithName(), PyArg_ValidateKeywordArguments(), PyArg_ParseTupleAndKeywords(), etc.

But SimpleNamespace is an exception.

>>> from types import SimpleNamespace
>>> SimpleNamespace(**{0:0})
namespace()

Non-string keys are omitted in the repr. Wouldn't be better to add also the check that keyword names for SimpleNamespace constructor are strings?

I don't know how classify this issue, as a bug or an enhancement.

Based on the StackOverflow question: https://stackoverflow.com/questions/46164770/accepting-integers-as-keys-of-kwargs.

----------
components: Interpreter Core
messages: 303450
nosy: haypo, serhiy.storchaka
priority: normal
severity: normal
status: open
title: SimpleNamespace accepts non-string keyword names

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


More information about the New-bugs-announce mailing list