1# Copyright 2014 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
7
8def IsDirectlyConstructable(cls):
9  """Returns True if instance of |cls| can be construct without arguments."""
10  assert inspect.isclass(cls)
11  if not hasattr(cls, '__init__'):
12    # Case |class A: pass|.
13    return True
14  if cls.__init__ is object.__init__:
15    # Case |class A(object): pass|.
16    return True
17  # Case |class (object):| with |__init__| other than |object.__init__|.
18  args, _, _, defaults = inspect.getargspec(cls.__init__)
19  if defaults is None:
20    defaults = ()
21  # Return true if |self| is only arg without a default.
22  return len(args) == len(defaults) + 1
23