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

Beginner questions

Open rvanlaar opened this issue 2 years ago • 2 comments

Started using Pyright and VScode. Used mypy a lot, but since typing support in VSCode is done via pyright, here I am.

Is there a recommend pyright config?

On my Django models I have a class Meta defined and there's an error that: "Meta" overrides symbol of same name in class "Model"... reportIncompatibleVariableOverride

Didn't see it mentioned in the documentation. Is there a recommended way to handle this?

I have multiple choices. Example:

class study(models.Model):
    LEVEL_CHOICES = (
        ("e", _("Elementary"),
        ("h", _("HighSchool")
    )
    level = models.CharField(
        max_length=1
        choices= LEVEL_CHOICES,
        blank= True)

    def info(self):
        return "%s" % self.get_level_display() # ReportUnkownMemberType and ReportGeneralTypeIssues

What's the recommended solution?

rvanlaar avatar Aug 28 '23 14:08 rvanlaar

Hmm good question, I'm not super familiar with using Pyright for type checking, I usually use it for autocomplete and run mypy in CI, but:

  1. might just have to ignore it
  2. I think you could manually write a definition for get_level_display inline?

Like:

class study(models.Model):
    LEVEL_CHOICES = (
        ("e", _("Elementary"),
        ("h", _("HighSchool")
    )
    level = models.CharField(
        max_length=1
        choices= LEVEL_CHOICES,
        blank= True)
    if TYPING:
        def get_level_display(self) -> SomeTypeHereIDK: ...

    def info(self):
        return "%s" % self.get_level_display() # ReportUnkownMemberType and ReportGeneralTypeIssues

tricky part of Django ORM are these generated / metaprogrammed/ monkey patched things that can't be expressed statically

sbdchd avatar Aug 28 '23 22:08 sbdchd

Appreciate the response.

After trying django-types and pyright on a branch, I decided against going further with it.

The application I'm working on relies on the meta programmed parts. I don't see django-types helping me. Instead I have to sprinkle the models everywhere with backlinks, autogenerated functions, 'id' and more.

Especially since django-stubs knows how to do it.

rvanlaar avatar Sep 12 '23 10:09 rvanlaar