You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
issue a warning when populating a CPython type dict with non-string keys #55664
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='https://github.com/Yhg1s'closed_at=Nonecreated_at=<Date2011-03-09.20:53:47.152>labels= ['interpreter-core', 'type-feature', '3.11']
title='issue a warning when populating a CPython type dict with non-string keys'updated_at=<Date2021-12-02.09:24:17.831>user='https://github.com/voidspace'
It is valid in CPython to create a new type with non-string keys in the dict. This is a problem for other implementations (neither pypy nor jython support it).
Note that other implementations not supporting this has been agreed by Guido. The language spec says that the class dict is a namespace and should have string keys.
loewis commented on Mar 10, 2011
loewismannequin
Mannequin
More actions
I fail to see the need to warn about this, though. Users using the feature are likely aware that this violates the language specification, and will find out quickly when they do port it to another Python implementation.
There are many many other aspects of CPython that don't, and often even can't, work on other Python implementations that are not warned about (such as usage of extensions modules, or development of extension modules).
stutzbach commented on Mar 10, 2011
stutzbachmannequin
Mannequin
More actions
For what it's worth, I believe this could be implemented easily by calling _PyDict_HasOnlyStringKeys at the end of the class creation process.
loewis commented on Mar 10, 2011
loewismannequin
Mannequin
More actions
Can somebody propose a patch?
durban commented on Mar 10, 2011
durbanmannequin
Mannequin
More actions
Can somebody propose a patch?
Yes, here it is. (I'm not sure if the test is in the correct file.)
How about the case where non-string keys are added after the class is created?
(I think I've heard some third-party lib does this, perhaps a generic functions implementation?)
alex@alex-laptop:~/projects/pypy$ python3.1
Python 3.1.2 (release31-maint, Sep 17 2010, 20:34:23)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> classA(object):
... pass
...
>>> A.__dict__[32] ="heh"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict_proxy' object does not support item assignment
>>> setattr(A, 32, "heh")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int'
Cool, someone uses my PyErr_WarnFormat() function! :-) I didn't know that NULL can be used for the category: I would prefer an explicit PyExc_RuntimeWarning to not have to read the source of PyErr_WarnFormat() or its documentation.
1 remaining item
BreamoreBoy commented on Jul 11, 2014
BreamoreBoymannequin
Mannequin
More actions
The patch is short and sweet. Assuming it is acceptable do we commit or don't we? See also the reference to bpo-11470 in msg132032.
>>> A =type("A", (object,), {42: "abc"})
>>> dir(A())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
Activity
voidspace commentedon Mar 9, 2011
It is valid in CPython to create a new type with non-string keys in the dict. This is a problem for other implementations (neither pypy nor jython support it).
This should raise a warning.
alex commentedon Mar 9, 2011
2 ways to do it:
or
type("A", (object,), {42: "abc"})
voidspace commentedon Mar 9, 2011
Note that other implementations not supporting this has been agreed by Guido. The language spec says that the class dict is a namespace and should have string keys.
loewis commentedon Mar 10, 2011
I fail to see the need to warn about this, though. Users using the feature are likely aware that this violates the language specification, and will find out quickly when they do port it to another Python implementation.
There are many many other aspects of CPython that don't, and often even can't, work on other Python implementations that are not warned about (such as usage of extensions modules, or development of extension modules).
stutzbach commentedon Mar 10, 2011
For what it's worth, I believe this could be implemented easily by calling _PyDict_HasOnlyStringKeys at the end of the class creation process.
loewis commentedon Mar 10, 2011
Can somebody propose a patch?
durban commentedon Mar 10, 2011
Yes, here it is. (I'm not sure if the test is in the correct file.)
pitrou commentedon Mar 10, 2011
How about the case where non-string keys are added after the class is created?
(I think I've heard some third-party lib does this, perhaps a generic functions implementation?)
alex commentedon Mar 10, 2011
How can they be set afterwords?
pitrou commentedon Mar 10, 2011
Hmm, that's true (although there's a trick using gc.get_referers(), IIRC).
I was probably thinking about instance dicts rather than type dicts, then.
ncoghlan commentedon Mar 24, 2011
Thomas, I know you've been working on this post-Pycon. Could you please take a look at Daniel's patch and/or publish your own.
vstinner commentedon Mar 24, 2011
Cool, someone uses my PyErr_WarnFormat() function! :-) I didn't know that NULL can be used for the category: I would prefer an explicit PyExc_RuntimeWarning to not have to read the source of PyErr_WarnFormat() or its documentation.
1 remaining item
BreamoreBoy commentedon Jul 11, 2014
The patch is short and sweet. Assuming it is acceptable do we commit or don't we? See also the reference to bpo-11470 in msg132032.
iritkatriel commentedon Dec 1, 2021
Reproduced on 3.11.
serhiy-storchaka commentedon Dec 2, 2021
Why not raise an error if it contradicts language spec?
furkanonder commentedon Jun 7, 2023
The PR is ready for review.
gh-55664: Add warning when creating a type using a namespace dictionaâĶ
pythongh-55664: Add warning when creating a type using a namespace diâĶ