Imagine we have a lot of TagRule objects with a lot of common code. It could be interesting to create a first rule with the common code and then inherit from it to make the other rules. Actually, this is not possible when using a single inheritance because of _getrules:
def _get_rules(self, cls):
"""Returns a list of rules of a given class
Rules are treated as singletons - we only instantiate each
rule once.
"""
result = []
for rule_class in cls.__subclasses__():
rule_name = rule_class.__name__.lower()
if rule_name not in self._rules:
rule = rule_class(self)
self._rules[rule_name] = rule
result.append(self._rules[rule_name])
return result
for rule_class in cls.__subclasses__() only iterate over subclasses. So we have to do something like:
class TagTest(TestRule):
def __init__(self, controller, severity=None):
super().__init__(controller, severity)
def configure(self, test_tags):
self.test_tags = test_tags.split(',')
def apply(self, testcase):
continue
class TagScope(TagTest, TestRule): # Ugly hack for cls.subclasses
def apply(self, testcase):
continue
Improvement idea: use isinstance() instead of iterating over direct subclasses.
class Foo:
foo = 1
class Bar(Foo):
bar = 1
class Foobar(Bar):
foobar = 1
isinstance(Foobar(), Foo) # True
Imagine we have a lot of TagRule objects with a lot of common code. It could be interesting to create a first rule with the common code and then inherit from it to make the other rules. Actually, this is not possible when using a single inheritance because of
_getrules:for rule_class in cls.__subclasses__()only iterate oversubclasses. So we have to do something like:Improvement idea: use
isinstance()instead of iterating over direct subclasses.