You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nonsensical exception message when calling __new__ on non-instaniable objects #78465
> type(sys.flags).__new__(type(sys.flags))
Traceback (most recent call last):
File "<pyshell#102>", line 1, in <module>type(sys.flags).__new__(type(sys.flags))
TypeError: tuple.__new__(sys.flags) is not safe, use sys.flags.__new__()
Ignoring the confusion caused by both the type and the instance being called "sys.flags", this error doesn't make sense. I am using "sys.flags" (the type).__new__, so why is it complaining?
"type(sys.flags)()" produces the standard "non-instantiable type" error.
The same behavior also happens for "sys.version_info", but strangely not for any of the other sys constants.
I'm trying to do something about this. Let me know if you have some ideas.
VadimPushtaev commented on Jul 30, 2018
VadimPushtaevmannequin
Mannequin
More actions
Here is what I learned:
Nothing is wrong with that "tuple.new(sys.flags) is not safe" part. __new__ is deleted from type(sys.flags), so parent's __new__ is called. tuple is indeed a base class for type(sys.flags).
Another part where we recommend to use "sys.flags.new()" doesn't make much sense, so I choose to delete this advice if there is no __new__ in a class.
This second part also may suggest a wrong class to call __new__ from:
In [1]: from collections import namedtuple
In [2]: class A(namedtuple('A', 'z')):
...: pass
...:
In [3]: object.__new__(A)
---------------------------------------------------------------------------
TypeError: object.__new__(A) is not safe, use tuple.__new__()
This should be A.__new__(), not tuple.__new__().
pppery commented on Jul 30, 2018
ppperymannequin
MannequinAuthor
More actions
Your PR is not an improvement:
In this case, this will produce the error "tuple.new(sys.flags) is not safe". But I didn't call "tuple.new", I called sys.flags.new, and type(X).new(type(X)) should always be safe
The change in error message for namedtuples (A) isn't related and (B) isn't correct. tuple.__new__(NamedTuple) works, and produces a namedtuple object, so tuple.new is what the error should point to.
pppery commented on Jul 30, 2018
ppperymannequin
MannequinAuthor
More actions
Same thing happens for other objects, like type(sys._getframe(0)).__new__(type(sys._getframe_)), and presumably any object that one cannot instantiate by calling
changed the title [-]Nonsensical exception message when calling `__new__` on some sys objects[/-][+]Nonsensical exception message when calling `__new__` on non-instaniable object[/+]on Jul 30, 2018
VadimPushtaev commented on Jul 31, 2018
VadimPushtaevmannequin
Mannequin
More actions
In this case, this will produce the error "tuple.__new__(sys.flags) is not safe". But I didn't call "tuple.__new__", I called sys.flags.__new__, and type(X).__new__(type(X)) should always be safe
Should it? There is no sys.flags.__new__, tuple.__new__ is called and it has different __new__.
The change in error message for namedtuples (A) isn't related and (B) isn't correct. tuple.__new__(NamedTuple) works, and produces a namedtuple object, so tuple.new is what the error should point to.
I believe you are right about (A), it's not related and should be discussed separately.
changed the title [-]Nonsensical exception message when calling `__new__` on non-instaniable object[/-][+]Nonsensical exception message when calling `__new__` on some sys objects[/+]on Jul 31, 2018
pppery commented on Jul 31, 2018
ppperymannequin
MannequinAuthor
More actions
The error I'm expecting here is "cannot create sys.flags objects". Anything else violates the fact that type(*args) is sugar for:
changed the title [-]Nonsensical exception message when calling `__new__` on some sys objects[/-][+]Nonsensical exception message when calling `__new__` on non-instaniable objects[/+]on Jul 31, 2018
Activity
pppery commentedon Jul 30, 2018
Ignoring the confusion caused by both the type and the instance being called "sys.flags", this error doesn't make sense. I am using "sys.flags" (the type).__new__, so why is it complaining?
"type(sys.flags)()" produces the standard "non-instantiable type" error.
The same behavior also happens for "sys.version_info", but strangely not for any of the other sys constants.
VadimPushtaev commentedon Jul 30, 2018
I'm trying to do something about this. Let me know if you have some ideas.
VadimPushtaev commentedon Jul 30, 2018
Here is what I learned:
Nothing is wrong with that "tuple.new(sys.flags) is not safe" part.
__new__is deleted fromtype(sys.flags), so parent's__new__is called.tupleis indeed a base class fortype(sys.flags).Another part where we recommend to use "sys.flags.new()" doesn't make much sense, so I choose to delete this advice if there is no
__new__in a class.This second part also may suggest a wrong class to call
__new__from:In [1]: from collections import namedtuple
In [2]: class A(namedtuple('A', 'z')):
...: pass
...:
In [3]: object.__new__(A)
---------------------------------------------------------------------------
TypeError: object.__new__(A) is not safe, use tuple.__new__()
This should be A.__new__(), not tuple.__new__().
pppery commentedon Jul 30, 2018
Your PR is not an improvement:
tuple.__new__(NamedTuple)works, and produces a namedtuple object, so tuple.new is what the error should point to.pppery commentedon Jul 30, 2018
Same thing happens for other objects, like
type(sys._getframe(0)).__new__(type(sys._getframe_)), and presumably any object that one cannot instantiate by calling[-]Nonsensical exception message when calling `__new__` on some sys objects[/-][+]Nonsensical exception message when calling `__new__` on non-instaniable object[/+]VadimPushtaev commentedon Jul 31, 2018
Should it? There is no sys.flags.__new__, tuple.__new__ is called and it has different __new__.
I believe you are right about (A), it's not related and should be discussed separately.
[-]Nonsensical exception message when calling `__new__` on non-instaniable object[/-][+]Nonsensical exception message when calling `__new__` on some sys objects[/+]pppery commentedon Jul 31, 2018
The error I'm expecting here is "cannot create sys.flags objects". Anything else violates the fact that type(*args) is sugar for:
("type" in the above snippet is a variable name, not the actual builtin "type")
[-]Nonsensical exception message when calling `__new__` on some sys objects[/-][+]Nonsensical exception message when calling `__new__` on non-instaniable objects[/+]13 remaining items
pythongh-78465: Fix error message for cls.__new__(cls, ...) where cls…
serhiy-storchaka commentedon Jun 26, 2025
See also bpo-43916/gh-88082 which introduced the Py_TPFLAGS_DISALLOW_INSTANTIATION flag and added multiple tests.
The error message was still nonsensical. #135981 fixes it.
gh-78465: Fix error message for cls.__new__(cls, ...) where cls is no…
pythongh-78465: Fix error message for cls.__new__(cls, ...) where cls…
[3.13] pythongh-78465: Fix error message for cls.__new__(cls, ...) wh…
[3.13] pythongh-78465: Fix error message for cls.__new__(cls, ...) wh…
[3.14] gh-78465: Fix error message for cls.__new__(cls, ...) where cl…
[3.13] gh-78465: Fix error message for cls.__new__(cls, ...) where cl…
pythongh-78465: Fix error message for cls.__new__(cls, ...) where cls…
pythongh-78465: Fix error message for cls.__new__(cls, ...) where cls…
pythongh-78465: Fix error message for cls.__new__(cls, ...) where cls…
pythongh-78465: Fix error message for cls.__new__(cls, ...) where cls…