This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: subclassing types.SimpleNamespace does not work
Type: behavior Stage: resolved
Components: Versions: Python 3.3, Python 3.4
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.snow Nosy List: Arfrever, christian.heimes, eric.snow, python-dev, r.david.murray
Priority: normal Keywords: patch

Created on 2012-10-08 01:10 by r.david.murray, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
issue16160.diff eric.snow, 2012-10-11 04:25 review
Messages (8)
msg172355 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-10-08 01:10
Python 3.3.0+ (3.3:152d85b2da3a, Oct  6 2012, 13:17:54) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from types import SimpleNamespace
>>> class Foo(SimpleNamespace):
...   pass
>>> y = Foo(bar=8, foo=9)
>>> y

It doesn't work to define an __init__ method, either, which is what I really wanted to do.  (I was subclassing to get the nice repr).
msg172357 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2012-10-08 02:26
I have verified that the __init__ function isn't executed when SimpleNamespace is subclasses. I guess that's happening:

"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)

That's about right:

>>> class SubStr(str):
...     pass
>>> type(SubStr.__new__(SubStr))
<class '__main__.SubStr'>
msg172510 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-10-09 19:44
Yikes.  I'll get a patch up tonight.
msg172552 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-10-10 05:04
Here's a patch that fixes subclass support.  It was supposed to work in the first place.  Guess I had tunnel vision at the time.

The fix is essentially a copy of the code in dict_new() in Objects/dictobject.c.  I left out the part about releasing GC if the type is not a subclass.

Note: see #15022, #15004, and #15003 for other things SimpleNamespace is missing.  I can get those done if it would be meaningful to you.
msg172565 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-10-10 11:09
Thanks.  I don't have a need for those in my current application at the current time.
msg172617 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-10-11 04:25
Here's an updated patch that addresses Éric's review comments.
msg172827 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-10-13 20:24
Am I good to commit this?
msg173133 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-10-17 05:57
New changeset c5124145e79e by Eric Snow in branch '3.3':
Close #16160: Subclass support now works for types.SimpleNamespace.  Thanks to RDM for noticing.

New changeset 47b9732696eb by Eric Snow in branch 'default':
merge for issue #16160: Subclass support now works for types.SimpleNamespace.
Date User Action Args
2022-04-11 14:57:36adminsetgithub: 60364
2012-10-17 05:57:18python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg173133

resolution: fixed
stage: patch review -> resolved
2012-10-13 20:24:12eric.snowsetmessages: + msg172827
2012-10-11 04:25:29eric.snowsetfiles: + issue16160.diff

messages: + msg172617
2012-10-11 04:23:15eric.snowsetfiles: - issue16160.diff
2012-10-10 11:09:42r.david.murraysetmessages: + msg172565
2012-10-10 05:04:32eric.snowsetfiles: + issue16160.diff
keywords: + patch
messages: + msg172552

stage: needs patch -> patch review
2012-10-09 19:44:40eric.snowsetassignee: eric.snow
messages: + msg172510
2012-10-08 02:26:14christian.heimessetnosy: + christian.heimes
messages: + msg172357
2012-10-08 01:30:04Arfreversetnosy: + Arfrever
2012-10-08 01:10:07r.david.murraycreate