Skip to content

typing: improve CaptureQueriesContext and test decorators in django.t…#3233

Open
01-Harshit wants to merge 2 commits intotypeddjango:masterfrom
01-Harshit:fix/test-util-typing-enhancement
Open

typing: improve CaptureQueriesContext and test decorators in django.t…#3233
01-Harshit wants to merge 2 commits intotypeddjango:masterfrom
01-Harshit:fix/test-util-typing-enhancement

Conversation

@01-Harshit
Copy link
Copy Markdown

…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:

  • Replaced _SupportsContains with Iterable in CaptureQueriesContext to make type-checking safer.

2. Stronger callable typing:

  • Updated _DecoratedTest, _C, new_checks, deployment_checks, and filter_func to Callable[..., Any] for better Mypy and Pyright support.

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

_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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants