New submission from xitop <[email protected]>:
An exception in __init__subclass__ leads under certain circumstances to wrong
isinstance() and issubclass() results. The exception probably leaves Python
internal data in inconsistent state.
Here is a demonstration program from Stack Overflow:
--- begin --
from abc import ABCMeta
class Animal(metaclass=ABCMeta):
pass
class Plant(metaclass=ABCMeta):
def __init_subclass__(cls):
assert not issubclass(cls, Animal), "Plants cannot be Animals"
class Dog(Animal):
pass
try:
class Triffid(Animal, Plant):
pass
except Exception:
pass
print("Dog is Animal?", issubclass(Dog, Animal))
print("Dog is Plant?", issubclass(Dog, Plant))
--- end --
Result is:
Dog is Animal? True
Dog is Plant? True
Changing the order of the print statements will result in:
Dog is Plant? False
Dog is Animal? False
Another ill-behaving program and a partial analysis can be found at SO:
https://stackoverflow.com/q/57848663/5378816
----------
components: Interpreter Core
messages: 351599
nosy: xitop
priority: normal
severity: normal
status: open
title: Interrupting class creation in __init_subclass__ may lead to incorrect
isinstance() and issubclass() results
type: behavior
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue38085>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com