mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-10 05:51:53 +08:00
Support returning the correct values for the different QuerySet methods when using .values() and .values_list(). (#33)
* Support returning the correct values for the different QuerySet methods when using .values() and .values_list(). * Fix slicing on QuerySet. Fix django queries test, and remove some ignored errors that are no longer needed. * Remove accidental change in RawQuerySet. * Readded some still-necessary ignores to aggregation django test. * Add more tests of first/last/earliest/last/__getitem__, per mkurnikov's comments. - Fix .iterator() * Re-add Iterator as base-class of QuerySet. * Make QuerySet a Collection. * - Fix return type for QuerySet.select_for_update(). - Use correct return type for QuerySet.dates() / QuerySet.datetimes(). - Use correct type params in return type for QuerySet.__and__ / QuerySet.__or__ - Re-add Sized as base class for QuerySet. - Add test of .all() for all _Row types. - Add test of .get() for all _Row types. - Remove some redundant QuerySet method tests. * Automatically fill in second type parameter for QuerySet. ... if second parameter is omitted.
This commit is contained in:
committed by
Maxim Kurnikov
parent
86c63d790b
commit
324b961d74
@@ -1,3 +1,5 @@
|
||||
from functools import partial
|
||||
|
||||
import os
|
||||
from typing import Callable, Dict, Optional, Union, cast
|
||||
|
||||
@@ -6,7 +8,7 @@ from mypy.nodes import MemberExpr, NameExpr, TypeInfo
|
||||
from mypy.options import Options
|
||||
from mypy.plugin import (
|
||||
AttributeContext, ClassDefContext, FunctionContext, MethodContext, Plugin,
|
||||
)
|
||||
AnalyzeTypeContext)
|
||||
from mypy.types import (
|
||||
AnyType, CallableType, Instance, NoneTyp, Type, TypeOfAny, TypeType, UnionType,
|
||||
)
|
||||
@@ -80,6 +82,18 @@ def determine_proper_manager_type(ctx: FunctionContext) -> Type:
|
||||
return ret
|
||||
|
||||
|
||||
def set_first_generic_param_as_default_for_second(fullname: str, ctx: AnalyzeTypeContext) -> Type:
|
||||
if not ctx.type.args:
|
||||
return ctx.api.named_type(fullname, [AnyType(TypeOfAny.explicit),
|
||||
AnyType(TypeOfAny.explicit)])
|
||||
args = ctx.type.args
|
||||
if len(args) == 1:
|
||||
args = [args[0], args[0]]
|
||||
|
||||
analyzed_args = [ctx.api.analyze_type(arg) for arg in args]
|
||||
return ctx.api.named_type(fullname, analyzed_args)
|
||||
|
||||
|
||||
def return_user_model_hook(ctx: FunctionContext) -> Type:
|
||||
api = cast(TypeChecker, ctx.api)
|
||||
setting_expr = helpers.get_setting_expr(api, 'AUTH_USER_MODEL')
|
||||
@@ -266,6 +280,14 @@ class DjangoPlugin(Plugin):
|
||||
else:
|
||||
return {}
|
||||
|
||||
def _get_current_queryset_bases(self) -> Dict[str, int]:
|
||||
model_sym = self.lookup_fully_qualified(helpers.QUERYSET_CLASS_FULLNAME)
|
||||
if model_sym is not None and isinstance(model_sym.node, TypeInfo):
|
||||
return (helpers.get_django_metadata(model_sym.node)
|
||||
.setdefault('queryset_bases', {helpers.QUERYSET_CLASS_FULLNAME: 1}))
|
||||
else:
|
||||
return {}
|
||||
|
||||
def get_function_hook(self, fullname: str
|
||||
) -> Optional[Callable[[FunctionContext], Type]]:
|
||||
if fullname == 'django.contrib.auth.get_user_model':
|
||||
@@ -344,6 +366,14 @@ class DjangoPlugin(Plugin):
|
||||
|
||||
return extract_and_return_primary_key_of_bound_related_field_parameter
|
||||
|
||||
def get_type_analyze_hook(self, fullname: str
|
||||
) -> Optional[Callable[[AnalyzeTypeContext], Type]]:
|
||||
queryset_bases = self._get_current_queryset_bases()
|
||||
if fullname in queryset_bases:
|
||||
return partial(set_first_generic_param_as_default_for_second, fullname)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def plugin(version):
|
||||
return DjangoPlugin
|
||||
|
||||
Reference in New Issue
Block a user