-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use mro lookup to avoid need for __wrapped__ in inheritance for Pytho…
…n 3.7+.
- Loading branch information
1 parent
cc6fbfc
commit efa02c6
Showing
7 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from __future__ import print_function | ||
|
||
import unittest | ||
import inspect | ||
import imp | ||
|
||
import wrapt | ||
|
||
from compat import PY2, PY3, exec_ | ||
|
||
class TestInheritance(unittest.TestCase): | ||
|
||
def test_single_inheritance(self): | ||
@wrapt.decorator | ||
def passthru(wrapped, instance, args, kwargs): | ||
return wrapped(*args, **kwargs) | ||
|
||
@passthru | ||
class BaseClass(object): | ||
def __init__(self): | ||
self.value = 1 | ||
|
||
class DerivedClass(BaseClass): | ||
def __init__(self): | ||
super(DerivedClass, self).__init__() | ||
self.value = 2 | ||
|
||
base = BaseClass() | ||
|
||
self.assertEqual(type(base), BaseClass.__wrapped__) | ||
self.assertTrue(isinstance(base, BaseClass.__wrapped__)) | ||
self.assertEqual(base.value, 1) | ||
|
||
self.assertEqual(type(base).__mro__, (BaseClass.__wrapped__, object)) | ||
self.assertEqual(BaseClass.__mro_entries__(()), (BaseClass.__wrapped__,)) | ||
|
||
derived = DerivedClass() | ||
|
||
self.assertEqual(type(derived), DerivedClass) | ||
self.assertTrue(isinstance(derived, BaseClass.__wrapped__)) | ||
self.assertTrue(isinstance(derived, DerivedClass)) | ||
self.assertEqual(derived.value, 2) | ||
|
||
self.assertEqual(type(derived).__mro__, (DerivedClass, BaseClass.__wrapped__, object)) | ||
|
||
def test_multiple_inheritance(self): | ||
@wrapt.decorator | ||
def passthru(wrapped, instance, args, kwargs): | ||
return wrapped(*args, **kwargs) | ||
|
||
@passthru | ||
class BaseClass1(object): | ||
pass | ||
|
||
@passthru | ||
class BaseClass2(object): | ||
pass | ||
|
||
class DerivedClass(BaseClass1, BaseClass2): | ||
pass | ||
|
||
derived = DerivedClass() | ||
|
||
self.assertEqual(type(derived), DerivedClass) | ||
self.assertTrue(isinstance(derived, BaseClass1.__wrapped__)) | ||
self.assertTrue(isinstance(derived, BaseClass2.__wrapped__)) | ||
self.assertTrue(isinstance(derived, DerivedClass)) | ||
|
||
self.assertEqual(type(derived).__mro__, (DerivedClass, | ||
BaseClass1.__wrapped__, BaseClass2.__wrapped__, object)) | ||
|
||
def test_multiple_inheritance_common(self): | ||
@wrapt.decorator | ||
def passthru(wrapped, instance, args, kwargs): | ||
return wrapped(*args, **kwargs) | ||
|
||
@passthru | ||
class CommonClass(object): | ||
pass | ||
|
||
@passthru | ||
class BaseClass1(CommonClass): | ||
pass | ||
|
||
@passthru | ||
class BaseClass2(CommonClass): | ||
pass | ||
|
||
class DerivedClass(BaseClass1, BaseClass2): | ||
pass | ||
|
||
derived = DerivedClass() | ||
|
||
self.assertEqual(type(derived), DerivedClass) | ||
self.assertTrue(isinstance(derived, CommonClass.__wrapped__)) | ||
self.assertTrue(isinstance(derived, BaseClass1.__wrapped__)) | ||
self.assertTrue(isinstance(derived, BaseClass2.__wrapped__)) | ||
self.assertTrue(isinstance(derived, DerivedClass)) | ||
|
||
self.assertEqual(type(derived).__mro__, (DerivedClass, | ||
BaseClass1.__wrapped__, BaseClass2.__wrapped__, | ||
CommonClass.__wrapped__, object)) |