Skip to main content
 首页 » 编程设计

python之pylint 为可调用的对象属性给出不可调用的错误

2024年12月31日7jackei

不确定是我做错了什么还是 pylint 有问题。在下面的代码中,我得到了 self.type is not callable E1102 的 linting 错误。

虽然我可以忽略它并继续工作,但似乎这种事情应该很容易修复......只是不知道如何修复它。

from typing import Callable 
 
 
class Thing: 
    def __init__(self, thing_type: Callable): 
        self._type = thing_type 
        self._value = None 
 
    @property 
    def type(self) -> Callable: 
        return self._type 
 
    @property 
    def value(self): 
        return self._value 
 
    @value.setter 
    def value(self, value): 
        self._value = self.type(value) 

请您参考如下方法:

暂时不接受这个答案,但我确实发现它与 @property 装饰器的使用有关。在创建新的对象属性的过程中,类型提示信息丢失到pylint,VSCode intellisense似乎不起作用。使用旧式属性语法,我得到了正确的 linting 和类型提示(见下图)

看起来有点遗憾,但我希望有人能提出更好的答案