1# Copyright 2017 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import inspect
6
7def IsMethodOverridden(parent_cls, child_cls, method_name):
8  assert inspect.isclass(parent_cls), '%s should be a class' % parent_cls
9  assert inspect.isclass(child_cls), '%s should be a class' % child_cls
10  assert parent_cls.__dict__.get(method_name), '%s has no method %s' % (
11      parent_cls, method_name)
12
13  if child_cls.__dict__.get(method_name):
14    # It's overridden
15    return True
16
17  if parent_cls in child_cls.__bases__:
18    # The parent is the base class of the child, we did not find the
19    # overridden method.
20    return False
21
22  # For all the base classes of this class that are not object, check if
23  # they override the method.
24  base_cls = [cls for cls in child_cls.__bases__ if cls and cls != object]
25  return any(
26      IsMethodOverridden(parent_cls, base, method_name) for base in base_cls)
27