django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

Support setter properties in Model class constructor

Open intgr opened this issue 3 years ago • 2 comments

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

  • python version: 3.10.5
  • django version: 4.0.5
  • mypy version: 0.961
  • django-stubs version: 1.12.0
  • django-stubs-ext version: 0.5.0

intgr avatar Jun 29 '22 14:06 intgr

Is there any temporary workaround for this to get the error to go away without having to ignore all "Unexpected attribute"?

c0x65o avatar Mar 02 '23 05:03 c0x65o

Not much better, but doing the following seems to work:

data = MyData()
data.text_alias = "blah"

grjones avatar May 31 '24 05:05 grjones