Consider following code snippet:
class AbstractClass(object):
def method1(self):
raise NotImplementedError()
def method2(self):
raise NotImplementedError()
class SemiConcreteClass(AbstractClass):
def method1(self):
return True
class ConcreteClass(SemiConcreteClass):
def method2(self):
return True
Running checks on this file with default config (pylint classes.py
) yields some missing docstrings (let's ignore them) and this warning:
W: 8, 0: Method 'method2' is abstract in class 'AbstractClass' but is not overridden (abstract-method)
I know SemiConcreteClass
is in fact abstract and should be sub-classed in order to be used and I do not want Pylint to report it. How can I configure Pylint to treat these classes as just another abstract class? Naming convention of class name, module where class resides - all this solutions would be fine.
I know I can explicitly silence warning in class by # pylint: disable=abstract-method
comment. Is there more elegant solution?
pylint 1.4.3,
astroid 1.3.6, common 0.63.2
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)]
The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class. All the methods in an interface are implicitly abstract unless the interface methods are static or default.
An abstract class usually has one or more abstract method. So yes it can have some method implemented. The goal is to force the user to implement these methods to have an object working.
The methods and properties defined (but not implemented) in an abstract class are called abstract methods and abstract properties. All abstract methods and properties need to be implemented in a child class in order to be able to create objects from it.
Note the abstract base class may have more than one abstract methods. The child class must implement all of them failing which TypeError will be raised.
Unfortunately pylint is not really intelligent enough in cases like these, personally I would just disable the warning since ConcreteClass
obviously implements both. The alternative solution would be to make SemiConcreteClass
implement both methods but that's usually something you are trying to avoid.
Instead of having Pylint check for this I usually just use the Python Abstract Base Classes instead, they will notify you of unimplemented methods.
Example:
#!/usr/bin/python
import abc
class AbstractClass(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def method1(self):
raise NotImplementedError()
@abc.abstractmethod
def method2(self):
raise NotImplementedError()
class SemiConcreteClass(AbstractClass):
def method1(self):
return True
SemiConcreteClass()
Which results in:
Traceback (most recent call last):
File "test.py", line 22, in <module>
SemiConcreteClass()
TypeError: Can't instantiate abstract class SemiConcreteClass with abstract methods method2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With