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

from_db_value not defined in models.JSONField interface

Open RobertKingOctopus opened this issue 3 years ago • 0 comments

Bug report

What's wrong

When attempting to call the superclass from_db_value in a class that inherits from django.db.models.JSONField, mypy does not believe that from_db_value is defined but it is.

from django.db import models

class CustomJSONField(models.JSONField):
    def from_db_value(self,value,expression,connection):
        super_value = super().from_db_value(value,expression,connection)
        # Convert value from JSON field further...
        return super_value 

gives the mypy error

 error: "from_db_value" undefined in superclass

How is that should be

Django's JSONField implementation explicitly defines a from_db_value, to handle the serialisation of a python dictionary to a json string. As such it probably should also be defined in the JSONField interface provided by django-stubs? (it is not defined in the default Field interface as it is not a mandatory method to have defined).

Please let me know if there is a better way to inherit from this class that avoids this error?

System information

  • OS:
  • python version: 3.8.5
  • django version: 3.2.12
  • mypy version: 0.942
  • django-stubs version: 1.9.0

RobertKingOctopus avatar Apr 26 '22 11:04 RobertKingOctopus