Skip to content

Commit

Permalink
Improve system check registry hints
Browse files Browse the repository at this point in the history
  • Loading branch information
intgr committed Nov 11, 2022
1 parent cb5f201 commit a4a1974
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 12 additions & 8 deletions django-stubs/core/checks/registry.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Callable, List, Optional, Sequence, Set, TypeVar, Union
from typing import Any, Callable, List, Optional, Sequence, Set, TypeVar, Union, overload

from django.apps.config import AppConfig
from django.apps.registry import Apps
from django.core.checks.messages import CheckMessage
from typing_extensions import Protocol

Expand All @@ -18,7 +19,9 @@ class Tags:
translation: str = ...
urls: str = ...

_CheckCallable = Callable[..., Sequence[CheckMessage]]
class _CheckCallable(Protocol):
def __call__(self, app_configs: Apps, **kwargs: Any) -> Sequence[CheckMessage]: ...

_C = TypeVar("_C", bound=_CheckCallable)

class _ProcessedCheckCallable(Protocol[_C]):
Expand All @@ -29,9 +32,10 @@ class CheckRegistry:
registered_checks: Set[_ProcessedCheckCallable] = ...
deployment_checks: Set[_ProcessedCheckCallable] = ...
def __init__(self) -> None: ...
def register(
self, check: Optional[Union[_CheckCallable, str]] = ..., *tags: str, **kwargs: Any
) -> Union[Callable[[_CheckCallable], _ProcessedCheckCallable], _ProcessedCheckCallable]: ...
@overload
def register(self, __check: _C) -> _ProcessedCheckCallable[_C]: ...
@overload
def register(self, *tags: str, **kwargs: Any) -> Callable[[_C], _ProcessedCheckCallable[_C]]: ...
def run_checks(
self,
app_configs: Optional[Sequence[AppConfig]] = ...,
Expand All @@ -44,6 +48,6 @@ class CheckRegistry:
def get_checks(self, include_deployment_checks: bool = ...) -> List[_ProcessedCheckCallable]: ...

registry: CheckRegistry = ...
register: Any = ...
run_checks: Any = ...
tag_exists: Any = ...
register = registry.register
run_checks = registry.run_checks
tag_exists = registry.tag_exists
20 changes: 20 additions & 0 deletions tests/typecheck/core/test_checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- case: test_checks_register
main: |
from typing import Any, List, Sequence
from django.apps.registry import Apps
from django.core.checks import register, Warning, CheckMessage
@register("foo", deploy=True)
def check_foo(app_configs: Apps, **kwargs: Any) -> List[Warning]: ...
reveal_type(check_foo) # N: Revealed type is "django.core.checks.registry._ProcessedCheckCallable[def (app_configs: django.apps.registry.Apps, **kwargs: Any) -> builtins.list[django.core.checks.messages.Warning]]"
reveal_type(check_foo.tags) # N: Revealed type is "typing.Sequence[builtins.str]"
@register
def check_bar(app_configs: Apps, **kwargs: Any) -> Sequence[CheckMessage]: ...
reveal_type(check_bar) # N: Revealed type is "django.core.checks.registry._ProcessedCheckCallable[def (app_configs: django.apps.registry.Apps, **kwargs: Any) -> typing.Sequence[django.core.checks.messages.CheckMessage]]"
@register() # E: Value of type variable "_C" of function cannot be "Callable[[int], Sequence[CheckMessage]]"
def wrong_args(bla: int) -> Sequence[CheckMessage]: ...

0 comments on commit a4a1974

Please sign in to comment.