Skip to content
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

Closed
brechtm mannequin opened this issue Aug 6, 2015 · 8 comments
Closed

Inheriting from NoneType does not fail consistently #68994

brechtm mannequin opened this issue Aug 6, 2015 · 8 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@brechtm
Copy link
Mannequin

brechtm mannequin commented Aug 6, 2015

BPO 24806
Nosy @benjaminp, @1st1

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:

assignee = None
closed_at = <Date 2015-10-07.02:43:12.797>
created_at = <Date 2015-08-06.07:00:10.915>
labels = ['interpreter-core', 'type-bug']
title = 'Inheriting from NoneType does not fail consistently'
updated_at = <Date 2015-10-07.02:43:12.791>
user = 'https://bugs.python.org/brechtm'

bugs.python.org fields:

activity = <Date 2015-10-07.02:43:12.791>
actor = 'python-dev'
assignee = 'none'
closed = True
closed_date = <Date 2015-10-07.02:43:12.797>
closer = 'python-dev'
components = ['Interpreter Core']
creation = <Date 2015-08-06.07:00:10.915>
creator = 'brechtm'
dependencies = []
files = []
hgrepos = []
issue_num = 24806
keywords = []
message_count = 8.0
messages = ['248112', '248113', '248114', '248115', '248124', '248125', '248126', '252447']
nosy_count = 4.0
nosy_names = ['benjamin.peterson', 'python-dev', 'brechtm', 'yselivanov']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue24806'
versions = ['Python 3.6']

@brechtm
Copy link
Mannequin Author

brechtm mannequin commented Aug 6, 2015

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.
Also, the result is not what I expected, as "bool(Null())" yields True.

@brechtm brechtm mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Aug 6, 2015
@arigo
Copy link
Mannequin

arigo mannequin commented Aug 6, 2015

FWIW bool(Null()) gives "correctly" the result False in CPython 3.5.

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.

@brechtm
Copy link
Mannequin Author

brechtm mannequin commented Aug 6, 2015

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 :).

@brechtm
Copy link
Mannequin Author

brechtm mannequin commented Aug 6, 2015

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.

@arigo
Copy link
Mannequin

arigo mannequin commented Aug 6, 2015

@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.

@arigo
Copy link
Mannequin

arigo mannequin commented Aug 6, 2015

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.

@brechtm
Copy link
Mannequin Author

brechtm mannequin commented Aug 6, 2015

Ok. I was afraid a fix for this might affect "class Integer(Object, int)". Good to hear it shouldn't.

@python-dev
Copy link
Mannequin

python-dev mannequin commented Oct 7, 2015

New changeset c46ccfac8763 by Benjamin Peterson in branch '2.7':
prevent unacceptable bases from becoming bases through multiple inheritance (bpo-24806)
https://hg.python.org/cpython/rev/c46ccfac8763

New changeset e670b37e7b14 by Benjamin Peterson in branch '3.4':
prevent unacceptable bases from becoming bases through multiple inheritance (bpo-24806)
https://hg.python.org/cpython/rev/e670b37e7b14

New changeset e02e4afcce6a by Benjamin Peterson in branch '3.5':
merge 3.4 (bpo-24806)
https://hg.python.org/cpython/rev/e02e4afcce6a

New changeset 4b2a2688d2ad by Benjamin Peterson in branch 'default':
merge 3.5 (closes bpo-24806)
https://hg.python.org/cpython/rev/4b2a2688d2ad

@python-dev python-dev mannequin closed this as completed Oct 7, 2015
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants