Mixin类是抽象基类吗?

smi*_*thy 6 python pycharm

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)
Run Code Online (Sandbox Code Playgroud)

Phi*_*per 5

Mixin类是AbstractBaseClasses吗?你的案件最准确的答案是否定的,但可能应该如此.

你所指出的原因,你作为一个独立的阶层无法生存.通过使它成为一个ABC,你明确地告诉任何看着你的类(如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 
Run Code Online (Sandbox Code Playgroud)

问题是你需要这个所有其他方法(self.AssertTrue,self.converter等).你可能会想到别的东西,但这看起来只是unittest.TestCase我的一个子类.

哦,PyCharm错了.不,他们做对了.如果你把它作为ABC或TestCase的子类,他们就不会抱怨.如果你使用了接口,比如zope.Interface,pycharm等通常会出错,因为他们不理解注册和查找过程.(它在python核心之外)