Skip to content

Add mypy plugin support to stubtest configuration #13948

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

huynguyengl99
Copy link

@huynguyengl99 huynguyengl99 commented May 6, 2025

Add mypy plugin support for stubtest

This PR adds support for mypy plugins in stubtest configuration, allowing packages that depend on plugins (like Django) to be properly validated. Implements mypy_plugins and mypy_plugins_config fields in METADATA.toml.

Fixes #13935

@huynguyengl99
Copy link
Author

Hi @sobolevn and @srittau,

This PR adds support for mypy plugins in stubtest configuration, which enables packages like Django-based stubs to run stubtest correctly.

I've demonstrated this functionality in the recently created channels stubs package (PR #13949), which supports the well-known django-channels upstream package. The implementation successfully passes tests.

Key additions:

  1. mypy_plugins field to specify mypy plugins
  2. mypy_plugins_config field to provide plugin configuration (e.g., Django's django_settings_module)
  3. Added support for django_settings.py in the @tests folder (by allowing this specific .py file where Python files are normally not permitted), providing a standard location for Django plugin configuration

I'd appreciate your feedback on this implementation and any suggestions for improvement. Thanks for your time!

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one nit below. I think this is a useful addition to our test infrastructure, but I'll wait for other maintainer's opinions before merging.

@@ -42,6 +42,10 @@ def _is_list_of_strings(obj: object) -> TypeGuard[list[str]]:
return isinstance(obj, list) and all(isinstance(item, str) for item in obj)


def _is_nested_dict(obj: object) -> TypeGuard[dict[str, dict[str, Any]]]:
return isinstance(obj, dict) and all(isinstance(item, dict) for item in obj.values())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's test the key also while we're here:

Suggested change
return isinstance(obj, dict) and all(isinstance(item, dict) for item in obj.values())
return isinstance(obj, dict) and all(isinstance(k, str) and isinstance(v, dict) for k, v in obj.items())

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Affirmative, thank you

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that supporting more complex stubs is great, if and only if other type checkers do not have any problems with that in the result.

The code looks good to me, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for mypy plugins to enable django-stubs testing
3 participants