-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Options for cooperation with django-stubs #207
Comments
I found this in Django wiki (GSoC 2023): https://code.djangoproject.com/wiki/SummerOfCode2023#Mergingdjango-typesbackintodjango-stubs I'm wondering if you agree with this characterisation:
|
Here's also a description about django-stubs/django-types differences: |
Tbh it was a while ago but I think the original intention was to get autocomplete to work in VSCode to make my job (and coworkers) working in the Django codebase easier. I was cool with maintaining the project solo since we didn’t use that many Django features, a small surface area. I think we only used Django as a JSON API so the admin and related tools weren’t that important. So once the models and query sets were working I considered the project good enough and would occasionally add some more types if autocomplete wasn’t working in some cases. Ideally I was hoping for Django to incorporate types but I think they said no a while back. I no longer work for that company or work in Django regularly so this project is more of a, “whenever I check GitHub and have the time to merge a PR” I’m open to merging the projects, I think there would need to be parity since people are using this project as a dependency |
One key difference between the two libraries can be found in related fields:
But starting at
With class Model(models.Model):
article = models.ForeignKey["Article"]("Article", on_delete=models.CASCADE)
Model().article # revealed type is Article but this is unfortunately not possible with class Model(models.Model):
article = models.ForeignKey["Article", "Article"]("Article", on_delete=models.CASCADE)
Model().article # revealed type is Article, revealed type is Any if using the first code snippet Is there any reason for Edit: forgot that you could set |
Which can be practically worked around by setting |
FWIW, when I use |
Could you be more specific? Is it unknown when accessing the foreign key field from an instance? From the class? By unkown, do you mean the actual |
That's exactly the point of this issue: to make |
Yep. I have looked into the tricks that |
@tylerlaprade Thanks for the clarification. Both mypy and pyright should support the provided snippet with Actually class ForeignKey(ForeignObject[_ST, _GT]):
# As an extra overload:
def __init__(
self,
to: type[_GT],
...
) But the set type can't really be parametrized this way, and this doesn't take into account nullability anyway. FYI, I'm working on an alternative solution to support |
I apologize for appearing in the middle of the discussion, but this might be related. I was recently searching for how to get type hinting for ForeignKey fields using string references in Vscode (which uses django-types), and I got some advice in this discussion. |
After further investigation and looking at the descriptor implementation, you can't set the foreign field by it's primary key, so I don't think having two type vars for foreign fields is necessary? Or at least make both the set and get type the same typevar. Or am I missing something? Edit: It seems to be related with |
You can find more info about this in the following article: https://viicos.github.io/posts/an-alternative-to-the-django-mypy-plugin/
Would love getting some feedback on this.
I've been looking at this when implementing |
@Viicos, interesting idea, but I don't understand why it's necessary. Pyright already knows the underlying type of a |
Well as I said here, it doesn't support string references to models, which are pretty common (I would even recommend using them over direct reference to the actual related class for different reasons). The article also explains a bit about this. Apart from support for
|
Ah I see, thanks for clarifying! |
Hi! I'm one of the maintainers of the django-stubs project.
I sense that there is significant duplication of effort between django-stubs and django-types. Our pace of development has picked up recently, but still we're struggling to cover the Django API surface. If we joined forces, we could get more done.
If you're open to that, we need an understanding about the differences between the projects, here's what I'm aware of:
_pyi_private_...
attributes in Model fields for our magic. I wouldn't think their existence interferes when using pyright though?(1) is solved and the rest don't look insurmountable.
Some relevant discussion:
Ping @sobolevn for visibility.
The text was updated successfully, but these errors were encountered: