if member "is spelled correctly" but doesn't exist in class, no warning is issued.
This is not how Python class instances work. What you are doing is an attribute assignment that effectively adds the attribute productNameTest
to the instance of ProductModel
in an instance of ProductService
. Python allows such attributions -as explained below- because it's dynamic and the dataclass definition doesn't forbid dynamically setting attributes on the instance.
3. Data model
Class instances
Attribute assignments and deletions update the instance’s dictionary, never a class’s dictionary. (...)
Special attributes: __dict__
is the attribute dictionary; __class__
is the instance’s class.
If you check __dict__
before and after the assignment, you can see the attribute was added and it's valid Python.
>>> the_instance = ProductService(ProductModel(1, "two"))
>>> the_instance.productModel.__dict__
{'productId': 1, 'productName': 'two'}
>>> the_instance.get_product_model("curve_data_str")
>>> the_instance.productModel.__dict__
{'productId': 1, 'productName': 'two', 'productNameTest': 'ABCD'}
How can we setup PyCharm to send a warning for no member?
There's nothing here for the PyCharm linter to warn you about, if you try a 3rd party linter there will also be no warning, it's the programmer's job to know about this. If you continue reading the documentation excerpt above the solution becomes apparent: what you can do is implement a run-time exception (which is not a linter warning):
If the class has a __setattr__()
or __delattr__()
method, this is called instead of updating the instance dictionary directly.
NB. I changed @dataclass(init=False)
to @dataclass(init=True)
just for the convenience of having the __init__
available in a single line, it doesn't change anything in regard to the attribute assignment this question is about.