Skip to content

Conversation

@picnixz
Copy link
Member

@picnixz picnixz commented Jan 10, 2026

if (!result) {
return NULL;
}
if (!_PyNamespace_Check(result)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many ways to clone an object, you should make a decision whether call __init__ and/or __new__bor just create an instance of the base class. This is done differently for other types.

The following code follows the way of __reduce__() (which is also used in copy.copy()). It is equivalent to:

new_ns = type(ns)()
new_dict = dict(ns.__dict__)
for k, v in kwargs.items():
    new_dict[k] = v
for k, v in new_dict.items():
    new_ns.__dict__[k] = v

but more efficient.

This is what copy.copy() will execute.

We can implement this for the case when the result of type(ns)() is not a SimpleNamespace, but this will add an amount of code, and all this for the case which perhaps never occurs in real code. So, I am fine with simply raising an error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to change the existing behavior. Maybe we could decide in a follow-up PR how to change this?

}
if (!_PyNamespace_Check(result)) {
PyErr_Format(PyExc_TypeError,
"%T.__new__() must return an instance of a subclass of %s",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think about something like "%T() returned %T object". __new__() is rarely called directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this locally but I wondered whether this could be confused with __init__ or a simple function call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants