VSCode PyLint Not detecting my Python DTO Class Members

Question:

Python Lint does not detect incorrect class members. It continues running my code, I have productName member below, not productNameTest. It should be sending an error. How can this be resolved ? Currently using VS Code.

Product Model:

@dataclass(init=False)
class ProductModel:
    
    productId: int
    productName: str

Product Service:

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
Asked By: mattsmith5

||

Answers:

Very good first question 🙂

This look like a false negative in pylint (bad understanding of data classes ?), you can open a bug or a pull request to fix the problem in pylint’s github.

By the way, contrary to what was said in the comment, your code make sense. The only thing is that you don’t have to do explicit getter/setter in python. You can have public attribute:

class ProductService:

    def __init__(self, product_model: ProductModel):
        self.product_model= product_model

or private attributes…

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

And in both case the calling code will be obj.product_model or obj.product_model = new_value.

Answered By: Pierre.Sassoulas
def __init__(self, productModel: ProductModel):
    self.product_model = product_model

and not

def __init__(self, productModel: ProductModel):
    self.__product_model = product_model

product_model is the member property; __product_model is hidden inside

Answered By: Egi Messito