pylint 对父类 __init__ 的误报

2 投票
1 回答
2501 浏览
提问于 2025-04-15 13:15

如果我从 ctypes.BigEndianStructure 这个类派生一个新类,pylint 会提醒我,如果我不调用 BigEndianStructure.__init__() 的话。这很好,但即使我修正了代码,pylint 还是会发出警告:

import ctypes

class Foo(ctypes.BigEndianStructure):
    def __init__(self):
        ctypes.BigEndianStructure.__init__(self)

$ pylint mymodule.py
C:  1: Missing docstring
C:  3:Foo: Missing docstring
W:  4:Foo.__init__: __init__ method from base class 'Structure' is not called
W:  4:Foo.__init__: __init__ method from base class 'BigEndianStructure' is not called
R:  3:Foo: Too few public methods (0/2)

一开始我以为这是因为 Structure 是来自一个 C 语言模块。如果我从我自己的类或者像 SocketServer.BaseServer 这样的纯 Python 类派生,就不会有这个警告。但如果我从 smbus.SMBus 这个 C 模块派生,我也不会收到警告。

有人知道除了禁用 W0231 之外的解决办法吗?

1 个回答

6

试着使用新的 super 调用方式:

class Foo(ctypes.BigEndianStructure):
    def __init__(self):
        super(Foo, self).__init__()

撰写回答