sbdchd / django-types

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

All model fields are being typed with `<xxx>Field[Unknown, Unknown]` (`Unknown`) #123

Open TheAfroOfDoom opened 2 years ago

TheAfroOfDoom commented 2 years ago

Fields that you would expect to be easily type-inferred from their model definitions are showing up as Unknown.

Example:

class Data(models.Model):
  timestamp = models.DateTimeField()

...results in Data().timestamp as type Unknown.

I've tried specifying the type in what feels like a "typescript-style" after seeing <xxx>Field[Unknown, Unknown] like so:

class Sample(models.Model):
  ...

class Data(models.Model):
  sample = models.ForeignKey["Data", Sample](Sample, on_delete=...)

This successfully typed Data().sample as Sample, but it seems like it shouldn't be necessary based on the docs.


It's entirely possible that my implementation of something is wrong/configuration is weird, but I've been unable to figure it out so far. Initially I thought that only my foreign keys weren't being typed properly, but I came to the realization that all fields were having this issue.

Apologies if this isn't the best place to put this; if this fork had a discussions section it'd probably be better there.

Environment:

dugagjin commented 2 years ago

Same here, when I go to the code of lets say MyModel.objects.first() by doing F12 on objects it redirects me to stubs pre installed by VSCode. I don't understand why it takes those stubs as priority instead of my installed ones. Seems like an issue with an empty py.typed in the root according to https://github.com/microsoft/pylance-release/issues/1197 but I can't confirm.

What I had to do which is also impractical, is to delete that global stubs folder and restart VSCode.

Ideally VSCode/Pylance should define a way to ignore the global stubs installed by VSCode. I don't know it it exists, I was unable to find a setting like this which made it work hence I deleted that folder.

sbdchd commented 2 years ago

related: https://github.com/microsoft/pylance-release/discussions/2663