typeddjango / django-stubs

PEP-484 stubs for Django
MIT License
1.6k stars 450 forks source link

Warning from pycharm on accessing postgres json fields #221

Open lahwran opened 5 years ago

lahwran commented 5 years ago

Bug report

What's wrong

I get a warning from pycharm when I have a model class with a json field and I try to access that json field, eg (pseudocode I can't easily test for repro, unfortunately, but it demonstrates what I'm pretty sure is a repro case of the problem from our private code):

from django.contrib.postgres.fields import JSONField

class ProbablyIrrelevantSuperclass(models.Model):
    # misc probably-irrelevant stuff
    class Meta:
        abstract = True

class SomeTableWithJson(ProbablyIrrelevantSuperclass):
    # ... lots of misc fields that probably don't matter ...

    some_json_value = JSONField(default=dict, blank=True, null=True)

    def some_func(self):
         # warning on next line: Unresolved attribute reference 'get' for class 'JSONField'
        return some_json_value.get("some_key")

How is that should be

JSONField should act as a union between JSONField and either the type of the default, or the possible types of a json value, depending on which the authors of django-stubs feel is appropriate.

System information

[mypy.plugins.django-stubs] django_settings_module = app.settings.local

[mypy~.migrations.]

ignore_errors = True```

lahwran commented 5 years ago

Update: something similar occurs with ArrayField.

(It's conceivable I might fix this myself, but it's not currently obvious to me how to do that from the way the generic types are defined.)