typing: improve CaptureQueriesContext and test decorators in django.t…#3233
Open
01-Harshit wants to merge 2 commits intotypeddjango:masterfrom
Open
typing: improve CaptureQueriesContext and test decorators in django.t…#323301-Harshit wants to merge 2 commits intotypeddjango:masterfrom
01-Harshit wants to merge 2 commits intotypeddjango:masterfrom
Conversation
UnknownPlatypus
requested changes
Mar 28, 2026
| _DecoratedTest: TypeAlias = Callable[..., Any] | _TestClass | ||
| _DT = TypeVar("_DT", bound=_DecoratedTest) | ||
| _C = TypeVar("_C", bound=Callable) # Any callable | ||
| _C = TypeVar("_C", bound=Callable[..., Any]) # Any callable |
Contributor
There was a problem hiding this comment.
Updated _DecoratedTest, _C, new_checks, deployment_checks, and filter_func to Callable[..., Any] for better Mypy and Pyright support.
There is no typing improvement, these are equivalent. But it’s slightly better because it would pass mypy no_any_generic check.
However, can we have a more narrow type here or is this the only option?
Author
There was a problem hiding this comment.
- Regarding Callable[..., Any], this change does not improve typing precision but satisfies mypy's no_any_generic check. Narrower typing is challenging because decorated functions can have varying signatures in Django.
- I believe this is the best practical balance between type safety and usability in this context.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…est.utils
I have made things!
This PR improves Django type stubs by strengthening type hints and replacing private API usage, while keeping runtime behavior unchanged.
Changes:
1. Private API replacement:
2. Stronger callable typing:
These improvements preserve runtime behavior while enhancing IDE autocomplete and type safety.
Verification:
Successfully ran: python -m mypy.stubtest django.test.utils
Output: Success: no issues found in 1 module