summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2017-03-11 05:48:36 -0800
committerGitHub <noreply@github.com>2017-03-11 05:48:36 -0800
commite3101808ae077a3d352d8740cc39d877ed355b86 (patch)
tree96cbd8435a6677e4bf266236fea17018b69972a4 /lib
parent15f80ed15c31c4a36c05d9fe405f9bc712898b1c (diff)
downloadspack-e3101808ae077a3d352d8740cc39d877ed355b86.tar.gz
spack-e3101808ae077a3d352d8740cc39d877ed355b86.tar.bz2
spack-e3101808ae077a3d352d8740cc39d877ed355b86.tar.xz
spack-e3101808ae077a3d352d8740cc39d877ed355b86.zip
Make multimethods work with inheritance. (#3411)
Previously, this would fail with a NoSuchMethodError: class Package(object): # this is the default implementation def some_method(self): ... class Foo(Package): @when('platform=cray') def some_method(self): ... @when('platform=linux') def some_method(self): ... This fixes the implementation of `@when` so that the superclass method will be invoked when no subclass method matches. Adds tests to ensure this works, as well.
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/multimethod.py12
-rw-r--r--lib/spack/spack/test/multimethod.py8
2 files changed, 17 insertions, 3 deletions
diff --git a/lib/spack/spack/multimethod.py b/lib/spack/spack/multimethod.py
index 4e2fb3bdaa..67737c8bed 100644
--- a/lib/spack/spack/multimethod.py
+++ b/lib/spack/spack/multimethod.py
@@ -128,10 +128,16 @@ class SpecMultiMethod(object):
if self.default:
return self.default(package_self, *args, **kwargs)
+
else:
- raise NoSuchMethodError(
- type(package_self), self.__name__, spec,
- [m[0] for m in self.method_list])
+ superclass = super(package_self.__class__, package_self)
+ superclass_fn = getattr(superclass, self.__name__, None)
+ if callable(superclass_fn):
+ return superclass_fn(*args, **kwargs)
+ else:
+ raise NoSuchMethodError(
+ type(package_self), self.__name__, spec,
+ [m[0] for m in self.method_list])
def __str__(self):
return "SpecMultiMethod {\n\tdefault: %s,\n\tspecs: %s\n}" % (
diff --git a/lib/spack/spack/test/multimethod.py b/lib/spack/spack/test/multimethod.py
index 90948f010c..fbcc70afe8 100644
--- a/lib/spack/spack/test/multimethod.py
+++ b/lib/spack/spack/test/multimethod.py
@@ -118,3 +118,11 @@ def test_virtual_dep_match(builtin_mock):
pkg = spack.repo.get('multimethod^mpich@1.0')
assert pkg.different_by_virtual_dep() == 1
+
+
+def test_multimethod_with_base_class(builtin_mock):
+ pkg = spack.repo.get('multimethod@3')
+ assert pkg.base_method() == "subclass_method"
+
+ pkg = spack.repo.get('multimethod@1')
+ assert pkg.base_method() == "base_method"