Enhancement: consider adding mypy plugin for type checking `data.create_instance(id=1, address__id=2)`
sobolevn opened this issue · 0 comments
Summary
Right now create_instance
can take any **kwargs
.
But, mypy has no way of actually checking that id=1, address__id=2
are valid keywords for this call.
It can be caught when executed, sure. But typechecking is much faster than writing code + writing tests + running them.
In Django we have a similar pattern of passing keywords like this to filters. Like: User.objects.filter(id=1, settings__profile="public")
. For this we use a custom mypy plugin: https://github.com/typeddjango/django-stubs/blob/c9c729073417d0936cb944ab8585ad236ab30321/mypy_django_plugin/transformers/orm_lookups.py#L10
What it does?
- It checks that simple keyword arguments are indeed the correct ones
- It checks that nested
__
ones also exist on the nested model - It still allows
**custom_data
unpacking - It generates an error that can be silenced with
type: ignore[custom-code]
- All checks like this can be turned off when
custom-code
is disabled in mypy checks - It does not affect anything else
- It slows down type-checking a bit for users who added this plugin
- For users without a plugin - nothing happens
- Pyright and PyCharm are unaffected
- It is better to bundle this plugin, but it can be a 3rd party (really hard to maintain)
Plus, in the future more goodies can be added, included DI proper checking, URL params, etc.
It will require its own set of tests via typing.assert_type
and maintaince time. Mypy sometimes break plugin-facing APIs.
I can write this plugin if others are interested :)
Basic Example
No response
Drawbacks and Impact
No response
Unresolved questions
No response
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
- If you would like to see an issue prioritized, make a pledge towards it!
- We receive the pledge once the issue is completed & verified
- This, along with engagement in the community, helps us know which features are a priority to our users.