Mixin类是抽象基类吗?在下面的示例中,对 test_base 的调用将失败,因为 python 无法解析 self.assertEqual。
此外,PyCharm 是否不正确,因为像下面这样标记的 Mixin 类具有未解析的属性错误?
class TestConverterMixin(object):
def setUp(self):
self.alt_hasher = getattr(hash, self.converter.__class__.__name__)
def test_base(self):
with self.settings(PASSWORD_HASHERS=[self.hasher, ]):
load_hashers(settings.PASSWORD_HASHERS)
for password in PASSWORDS:
orig = self.alt_hasher.encrypt(password)
conv = self.converter.from_orig(orig)
# see if we get a working hash:
self.assertTrue(check_password(password, conv))
# convert back and test with passlib:
back = self.converter.to_orig(conv)
self.assertEqual(orig, back)
最佳答案
Mixin 类是 AbstractBaseClasses 吗?对于您的情况,最准确的答案是否定的,但可能应该是。
由于您指出的原因,您作为独立类(class)无法生存。通过使其成为 ABC你明确地告诉任何看你类(class)的人(比如 pycharm)
from abc import ABCMeta, abstractmethod
class TestConverterMixin(object):
__metaclass__ = ABCMeta
@abstractmethod
def assertEqual(self, other):
"Need concrete implementation somewhere"
.... the rest of your code
问题是所有其他方法(self.AssertTrue、self.converter 等)都需要这个。您可能还有其他想法,但这看起来真的只是 unittest.TestCase
的子类对我来说。
哦,PyCharm 错了吗?不,他们做对了。如果您将其设为 ABC 或 TestCase 的子类,他们就不会提示了。如果你使用接口(interface),比如 zope.Interface,pycharm 之类的通常会出错,因为它们不了解注册和查找过程。(它在 python 核心之外)
关于python - Mixin类是抽象基类吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28010960/