Reputation: 1851
I'm currently developping for python 2, and I'm trying to use abstract base classes to simulate interfaces. I have an interface, a base implementation of that interface and many subclasses that extend the base implementation. It looks like this:
class Interface(object):
__metaclass__ = ABCMeta
class IAudit(Interface):
@abstractproperty
def timestamp(self):
raise NotImplementedError()
@abstractproperty
def audit_type(self):
raise NotImplementedError()
class BaseAudit(IAudit):
def __init__(self, audit_type):
# init logic
pass
@property
def timestamp(self):
return self._timestamp
@property
def audit_type(self):
return self._audit_type
class ConcreteAudit(BaseAudit):
def __init__(self, audit_type):
# init logic
super(ConcreteAudit, self).__init__(audit_type)
pass
However PyCharm notifies me that ConcreteAudit
should implement all abstract methods. However, BaseAudit
(which is not specified as an abc) already implements those methods, and ConcreteAudit
is a subclass of BaseAudit
. Why is PyCharm warning me? Shouldn't it detect that IAudit
's contract is already implemented through BaseAudit
?
Upvotes: 1
Views: 52
Reputation: 8137
Why is PyCharm warning you?
Because all Python IDEs suck, that's why.
Whenever an intern/junior programmer/peer tells me that something that I wrote isn't working for him, I tell him that I'm not discussing it until he tries it by executing a Python script from the command line or from the stock interpreter. 99% of the time, the problem disappears.
Why do they suck? Beats me. But they all sometimes hide exceptions, sometimes make it possible to have stuff imported you didn't know about, and all sometimes decide (as in this case) that something is problematic that a real program running on the stock interpreter simply won't have a problem with.
I tried you code in both Python 2.7 and Python 3.4, and as long as I add from abc import ABCMeta, abstractproperty
at the top, it runs peachy-keen fine.
So just ditch PyCharm, or update the tags to show that's where the error is.
Upvotes: 2