Merge pull request #29 from syastrov/queryset_in_bulk

QuerySet.in_bulk returns Dict with values of correct model type.
This commit is contained in:
Maxim Kurnikov
2019-02-22 23:23:36 +03:00
committed by GitHub
3 changed files with 27 additions and 5 deletions

View File

@@ -84,9 +84,7 @@ class QuerySet(Iterable[_T], Sized):
def latest(self, *fields: Any, field_name: Optional[Any] = ...) -> _T: ...
def first(self) -> Optional[_T]: ...
def last(self) -> Optional[_T]: ...
def in_bulk(
self, id_list: Any = ..., *, field_name: str = ..., **kwargs: Any
) -> Dict[Union[int, str], models.Model]: ...
def in_bulk(self, id_list: Iterable[Any] = ..., *, field_name: str = ...) -> Dict[Any, _T]: ...
def delete(self) -> Tuple[int, Dict[str, int]]: ...
def update(self, **kwargs: Any) -> int: ...
def _update(self, values: Any) -> Optional[Any]: ...

View File

@@ -214,6 +214,10 @@ IGNORED_ERRORS = {
'logging_tests': [
re.compile('"(setUpClass|tearDownClass)" undefined in superclass')
],
'lookup': [
'Unexpected keyword argument "headline__startswith" for "in_bulk" of "QuerySet"',
'note: '
],
'many_to_one': [
'Incompatible type for "parent" of "Child" (got "None", expected "Union[Parent, Combinable]")'
],
@@ -661,6 +665,10 @@ def check_with_mypy(abs_path: Path, config_file_path: Path) -> int:
return int(error_happened)
def get_absolute_path_for_test(test_dirname: str):
return (PROJECT_DIRECTORY / tests_root / test_dirname).absolute()
if __name__ == '__main__':
mypy_config_file = (PROJECT_DIRECTORY / 'scripts' / 'mypy.ini').absolute()
repo_directory = PROJECT_DIRECTORY / 'django-sources'
@@ -675,8 +683,14 @@ if __name__ == '__main__':
repo.remotes['origin'].pull(DJANGO_BRANCH)
repo.git.checkout(DJANGO_COMMIT_SHA)
for dirname in TESTS_DIRS:
abs_path = (PROJECT_DIRECTORY / tests_root / dirname).absolute()
if len(sys.argv) > 1:
tests_to_run = sys.argv[1:]
else:
tests_to_run = TESTS_DIRS
for dirname in tests_to_run:
abs_path = get_absolute_path_for_test(dirname)
print(f'Checking {abs_path}')
rc = check_with_mypy(abs_path, mypy_config_file)

View File

@@ -0,0 +1,10 @@
[CASE test_queryset]
from django.db import models
class Blog(models.Model):
slug = models.CharField(max_length=100)
reveal_type(Blog.objects.in_bulk([1])) # E: Revealed type is 'builtins.dict[Any, main.Blog*]'
reveal_type(Blog.objects.in_bulk()) # E: Revealed type is 'builtins.dict[Any, main.Blog*]'
reveal_type(Blog.objects.in_bulk(['beatles_blog'], field_name='slug')) # E: Revealed type is 'builtins.dict[Any, main.Blog*]'
[out]