Are Mixin classes abstract base classes?

Are Mixin classes abstract base classes? In the example below, test_base calls failed because python would not be able to resolve self.assertEqual, for example.

Also, is it incorrect for PyCharm to flag Mixin classes like the ones below that have unresolved attribute errors?

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) 
+5
source share
2 answers

Are Mixin classes AbstractBaseClasses? The most accurate answer for your case is no, but it probably should be.

Your class as an independent person cannot survive for your reasons, which you indicated. By doing this ABC , you will directly tell anyone who is looking at your class (e.g. pycharm) that

 from abc import ABCMeta, abstractmethod class TestConverterMixin(object): __metaclass__ = ABCMeta @abstractmethod def assertEqual(self, other): "Need concrete implementation somewhere" .... the rest of your code 

The problem is that you will need this for all other methods (self.AssertTrue, self.converter, etc.). You may have something else, but it seriously looks like a subclass of unittest.TestCase for me.

Oh, and that was wrong. No, they understood everything. If you did this with ABC or a subclass of TestCase, they would not complain. If you used interfaces like zope.Interface, pycharm etc. They are usually mistaken because they do not understand the registration and search process (it is outside the python core)

+5
source

I was having problems getting PyCharm to not complain about unresolved mixin attribute reference errors. In particular, I also had mixing classes depending on other mixin classes for which I could not inherit from each other. But then I found this perfect way to make PyCharm 2017.1 happy:

 class Human: def is_male(self): return True class BeardMixin: _facial_hair = {'length': 7, 'color': 'brown'} def has_beard(self): return True class BeardLengthMixin: """Mixin for class Human with BeardMixin to provide get_beard_length()""" def get_beard_length(self): assert isinstance(self, (Human, BeardMixin)) # PyCharm will now not complain about any of these 3 attributes if self.is_male() and self.has_beard(): return self._facial_hair['length'] 

The approving statement gives PyCharm the necessary information about what types can be. There is also a drawback: the affirmative operator itself does not do what, in your opinion, it does: it only checks that it is of any type, and not the one and the other. Unfortunately, using two assert statements does not work, because the second one overrides the first one with respect to PyCharm type inference.

0
source

Source: https://habr.com/ru/post/1211364/


All Articles