[issue16160] subclassing types.SimpleNamespace does not work

Christian Heimes report at bugs.python.org
Mon Oct 8 04:26:14 CEST 2012


Christian Heimes added the comment:

I have verified that the __init__ function isn't executed when SimpleNamespace is subclasses. I guess that's happening:

http://docs.python.org/py3k/c-api/typeobj.html?highlight=tp_init#PyTypeObject.tp_init

"If the tp_new function returns an instance of some other type that is not a subtype of the original type, no tp_init function is called; if tp_new returns an instance of a subtype of the original type, the subtype’s tp_init is called."

namespace_new always returns a namespace object no matter what. As namespace is not a subclass of the Foo (the other way around), the type check in type_call() fails and __init__ isn't called. The tp_new method needs a fix.

That looks all wrong to me:

>>> import types
>>> class SubNS(types.SimpleNamespace):
...     pass
... 
>>> SubNS.__new__(SubNS)
namespace()

That's about right:

>>> class SubStr(str):
...     pass
... 
>>> type(SubStr.__new__(SubStr))
<class '__main__.SubStr'>

----------
nosy: +christian.heimes

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16160>
_______________________________________


More information about the Python-bugs-list mailing list