Skip to content
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

Enhancement: consider adding mypy plugin for type checking data.create_instance(id=1, address__id=2) #3814

Open
sobolevn opened this issue Oct 16, 2024 · 0 comments
Labels
DTOs This is related to our DTO feature Enhancement This is a new feature or request Typing This relates to typing

Comments

@sobolevn
Copy link
Member

sobolevn commented Oct 16, 2024

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.
Fund with Polar
@sobolevn sobolevn added Enhancement This is a new feature or request Typing This relates to typing DTOs This is related to our DTO feature labels Oct 16, 2024
@github-project-automation github-project-automation bot moved this to Triage in Overview Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DTOs This is related to our DTO feature Enhancement This is a new feature or request Typing This relates to typing
Projects
Status: Triage
Development

No branches or pull requests

1 participant