msg130462 - (view) |
Author: Michael Foord (michael.foord) *  |
Date: 2011-03-09 20:53 |
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. |
|
|
msg130464 - (view) |
Author: Alex Gaynor (alex) *  |
Date: 2011-03-09 20:54 |
2 ways to do it: class A(object): locals()[42] = "abc" or type("A", (object,), {42: "abc"}) |
|
|
msg130465 - (view) |
Author: Michael Foord (michael.foord) *  |
Date: 2011-03-09 20:55 |
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. |
|
|
msg130484 - (view) |
Author: Martin v. Löwis (loewis) *  |
Date: 2011-03-10 03:02 |
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). |
|
|
msg130510 - (view) |
Author: Daniel Stutzbach (stutzbach)  |
Date: 2011-03-10 15:54 |
For what it's worth, I believe this could be implemented easily by calling _PyDict_HasOnlyStringKeys at the end of the class creation process. |
|
|
msg130512 - (view) |
Author: Martin v. Löwis (loewis) *  |
Date: 2011-03-10 16:28 |
Can somebody propose a patch? |
|
|
msg130520 - (view) |
Author: Daniel Urban (daniel.urban) *  |
Date: 2011-03-10 18:50 |
> Can somebody propose a patch? Yes, here it is. (I'm not sure if the test is in the correct file.) |
|
|
msg130521 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2011-03-10 18:52 |
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?) |
|
|
msg130522 - (view) |
Author: Alex Gaynor (alex) *  |
Date: 2011-03-10 18:53 |
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 "", line 1, in TypeError: 'dict_proxy' object does not support item assignment >>> setattr(A, 32, "heh") Traceback (most recent call last): File "", line 1, in TypeError: attribute name must be string, not 'int' |
|
|
msg130523 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2011-03-10 18:55 |
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. |
|
|
msg131974 - (view) |
Author: Alyssa Coghlan (ncoghlan) *  |
Date: 2011-03-24 13:34 |
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. |
|
|
msg131975 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2011-03-24 13:43 |
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. |
|
|
msg132032 - (view) |
Author: Daniel Urban (daniel.urban) *  |
Date: 2011-03-24 21:24 |
> I would prefer an explicit PyExc_RuntimeWarning to not have to read the > source of PyErr_WarnFormat() or its documentation. The patch at adds a new warning type, CompatibilityWarning. I think probably that should be used here too. |
|
|
msg222753 - (view) |
Author: Mark Lawrence (BreamoreBoy) * |
Date: 2014-07-11 14:52 |
The patch is short and sweet. Assuming it is acceptable do we commit or don't we? See also the reference to #11470 in . |
|
|
msg407472 - (view) |
Author: Irit Katriel (iritkatriel) *  |
Date: 2021-12-01 16:36 |
Reproduced on 3.11. >>> A = type("A", (object,), {42: "abc"}) >>> dir(A()) Traceback (most recent call last): File "", line 1, in TypeError: '<' not supported between instances of 'int' and 'str' |
|
|
msg407520 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2021-12-02 09:24 |
Why not raise an error if it contradicts language spec? |
|
|