django-stubs
django-stubs copied to clipboard
Support setter properties in Model class constructor
Bug report
When a Model class includes properties with setters, Django allows assigning values to them via the Model constructor. But django-stubs does not allow that and shows a false positive error.
Example:
class MyData(models.Model):
text = models.CharField(max_length=100)
@property
def text_alias(self) -> str:
return self.text
@text_alias.setter
def text_alias(self, value: str) -> None:
self.text = value
MyData(text_alias="blah")
# ^^^^^^^^^^ Mypy: Unexpected attribute "text_alias" for model "MyData"
At one point I may look into implementing this myself, but just registering this as a missing feature for now.
System information
-
pythonversion: 3.10.5 -
djangoversion: 4.0.5 -
mypyversion: 0.961 -
django-stubsversion: 1.12.0 -
django-stubs-extversion: 0.5.0
Is there any temporary workaround for this to get the error to go away without having to ignore all "Unexpected attribute"?
Not much better, but doing the following seems to work:
data = MyData()
data.text_alias = "blah"