New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue a warning when populating a CPython type dict with non-string keys #55664
Comments
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. |
2 ways to do it: class A(object):
locals()[42] = "abc" or type("A", (object,), {42: "abc"}) |
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. |
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). |
For what it's worth, I believe this could be implemented easily by calling _PyDict_HasOnlyStringKeys at the end of the class creation process. |
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? |
How can they be set afterwords? 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.
>>> class A(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' |
Hmm, that's true (although there's a trick using gc.get_referers(), IIRC). |
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. |
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. |
The patch at bpo-11470 adds a new warning type, CompatibilityWarning. I think probably that should be used here too. |
Reproduced on 3.11. >>> 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' |
Why not raise an error if it contradicts language spec? |
The PR is ready for review. |
…ry with non-string keys. (GH-105338) Co-authored-by: Daniel Urban <durban@users.noreply.github.com>
…ctionary with non-string keys. (pythonGH-105338) Co-authored-by: Daniel Urban <durban@users.noreply.github.com>
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:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: