New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inheriting from NoneType does not fail consistently #68994
Comments
These both raise an exception: class Null(type(None)): pass
class Null(object, type(None)): pass The following does not: class Object(object): pass
class Null(Object, type(None)): pass This should also raise a TypeError. |
FWIW The problem in my opinion is that "!Py_TPFLAGS_BASETYPE" is checked only on the best base instead of on all bases. It lets this kind of nonsense pass through. In CPython 2.7 (but not 3.5) the following example also works (and is equally nonsense): class A(int): pass
class Bogus(A, bool): pass Although we're subclassing NoneType or bool, I did not manage to get a CPython crash from any of these examples. |
Similar inconsistent behavior for: class Object(object): pass
class Integer(Object, int): pass versus class Integer(object, int): pass However, I'm successfully using the first version, which saves some boilerplate code for emulating ints. Instances of Integer seem to behave exactly like ints. Personally, I wouldn't like to loose this ability. (May I shouldn't have created this ticket :). |
This is a real-world example where this type of inheritance is used: https://github.com/brechtm/rinohtype/blob/8bd961243c1059aa7cb738493e4687f7a5930d5b/rinoh/backend/pdf/cos.py#L121 I don't think there is any inherent reason not to subclass from (indirectly) object and int at the same time. I understand this is only a limitation of CPython? Please correct me if I'm wrong. |
@brechtm No, the example you give is wrong. It is correct that this refuses to work (and unrelated to this bug): class Integer(object, int): pass for reasons explained in the docs. |
To be clearer, this bug report is, more precisely, about subclassing built-in classes that are not meant to be subclassable. This includes type(None) and bool. |
Ok. I was afraid a fix for this might affect "class Integer(Object, int)". Good to hear it shouldn't. |
New changeset c46ccfac8763 by Benjamin Peterson in branch '2.7': New changeset e670b37e7b14 by Benjamin Peterson in branch '3.4': New changeset e02e4afcce6a by Benjamin Peterson in branch '3.5': New changeset 4b2a2688d2ad by Benjamin Peterson in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: