简体   繁体   中英

python lint error if not implementing interface

I know python likes to play it nice and loose with types, but sometimes you want a plugin type interface, and want to discover before production that someone has missed something. I found abcmeta - so did the following:

class Abstract_Base(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def a():
        pass
    @abc.abstractmethod
    def b():
        pass

class Inheritor_One(Abstract_Base):
    def a():
        pass

but when I do python -m flake8.... it has no problem with that. Is there any way of writing it such that someone not overriding an abstract method will go bang at linting time?

Pylint raises abstract-method for your example:

W0223: Method 'b' is abstract in class 'Abstract_Base' but is not overridden (abstract-method)

If you actually want Inheritor_One to be an abstract class you can disable the warning locally in this class and still have the warning when you use the abstract class later on:

import abc


class Abstract_Base(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def a(self):
        pass
    @abc.abstractmethod
    def b(self):
        pass

class Inheritor_One(Abstract_Base):
    # pylint: disable=abstract-method
    def a(self):
        pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM