Python Lint 不会检测到不正确的类成员。它继续运行我的代码,我在下面有 productName 成员,而不是 productNameTest。它应该发送一个错误。如何解决?目前使用 VS 代码。
产品型号:
@dataclass(init=False)
class ProductModel:
productId: int
productName: str
产品服务:class ProductService:
def __init__(self, productModel: ProductModel):
self.productModel= productModel
def getProductModel(self, curveData):
self.productModel.productNameTest = "ABCD" # productNameTest is not a member and should be giving error
最佳答案
很好的第一个问题:)
这看起来像是 pylint 中的假阴性(对数据类的理解不好?),您可以在 pylint's github 中打开错误或拉取请求来修复问题.
顺便说一句,与评论中所说的相反,您的代码是有意义的。唯一的事情是你不必在 python 中做显式的 getter/setter。您可以拥有公共(public)属性:
class ProductService:
def __init__(self, product_model: ProductModel):
self.product_model= product_model
或私有(private)属性...class ProductService:
def __init__(self, productModel: ProductModel):
self.__product_model= product_model
@property
def product_model(self):
return self.__product_model
@product_model.setter
def product_model(self, value):
self.__product_model = value
在这两种情况下,调用代码都是 obj.product_model
或 obj.product_model = new_value
.
关于python - VSCode PyLint 未检测到我的 Python DTO 类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66729956/