sbdchd / django-types

:doughnut: Type stubs for Django
MIT License
187 stars 62 forks source link

Beginner questions #186

Open rvanlaar opened 1 year ago

rvanlaar commented 1 year ago

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

  1. 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?

2. 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?

sbdchd commented 1 year ago

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

rvanlaar commented 1 year ago

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.