mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-13 07:21:56 +08:00
* QuerySet.annotate returns self-type. Attribute access falls back to Any. - QuerySets that have an annotated model do not report errors during .filter() when called with invalid fields. - QuerySets that have an annotated model return ordinary dict rather than TypedDict for .values() - QuerySets that have an annotated model return Any rather than typed Tuple for .values_list() * Fix .annotate so it reuses existing annotated types. Fixes error in typechecking Django testsuite. * Fix self-typecheck error * Fix flake8 * Fix case of .values/.values_list before .annotate. * Extra ignores for Django 2.2 tests (false positives due to tests assuming QuerySet.first() won't return None) Fix mypy self-check. * More tests + more precise typing in case annotate called before values_list. Cleanup tests. * Test and fix annotate in combination with values/values_list with no params. * Remove line that does nothing :) * Formatting fixes * Address code review * Fix quoting in tests after mypy changed things * Use Final * Use typing_extensions.Final * Fixes after ValuesQuerySet -> _ValuesQuerySet refactor. Still not passing tests yet. * Fix inheritance of _ValuesQuerySet and remove unneeded type ignores. This allows the test "annotate_values_or_values_list_before_or_after_annotate_broadens_type" to pass. * Make it possible to annotate user code with "annotated models", using PEP 583 Annotated type. * Add docs * Make QuerySet[_T] an external alias to _QuerySet[_T, _T]. This currently has the drawback that error messages display the internal type _QuerySet, with both type arguments. See also discussion on #661 and #608. Fixes #635: QuerySet methods on Managers (like .all()) now return QuerySets rather than Managers. Address code review by @sobolevn. * Support passing TypedDicts to WithAnnotations * Add an example of an error to README regarding WithAnnotations + TypedDict. * Fix runtime behavior of ValuesQuerySet alias (you can't extend Any, for example). Fix some edge case with from_queryset after QuerySet changed to be an alias to _QuerySet. Can't make a minimal test case as this only occurred on a large internal codebase. * Fix issue when using from_queryset in some cases when having an argument with a type annotation on the QuerySet. The mypy docstring on anal_type says not to call defer() after it.
60 lines
2.4 KiB
Python
60 lines
2.4 KiB
Python
from mypy.plugin import MethodContext
|
|
from mypy.types import AnyType, Instance
|
|
from mypy.types import Type as MypyType
|
|
from mypy.types import TypeOfAny
|
|
|
|
from mypy_django_plugin.django.context import DjangoContext
|
|
from mypy_django_plugin.lib import fullnames, helpers
|
|
from mypy_django_plugin.lib.helpers import is_annotated_model_fullname
|
|
|
|
|
|
def typecheck_queryset_filter(ctx: MethodContext, django_context: DjangoContext) -> MypyType:
|
|
lookup_kwargs = ctx.arg_names[1]
|
|
provided_lookup_types = ctx.arg_types[1]
|
|
|
|
assert isinstance(ctx.type, Instance)
|
|
|
|
if not ctx.type.args or not isinstance(ctx.type.args[0], Instance):
|
|
return ctx.default_return_type
|
|
|
|
model_cls_fullname = ctx.type.args[0].type.fullname
|
|
model_cls = django_context.get_model_class_by_fullname(model_cls_fullname)
|
|
if model_cls is None:
|
|
return ctx.default_return_type
|
|
|
|
for lookup_kwarg, provided_type in zip(lookup_kwargs, provided_lookup_types):
|
|
if lookup_kwarg is None:
|
|
continue
|
|
if isinstance(provided_type, Instance) and provided_type.type.has_base(
|
|
"django.db.models.expressions.Combinable"
|
|
):
|
|
provided_type = resolve_combinable_type(provided_type, django_context)
|
|
|
|
lookup_type: MypyType
|
|
if is_annotated_model_fullname(model_cls_fullname):
|
|
lookup_type = AnyType(TypeOfAny.implementation_artifact)
|
|
else:
|
|
lookup_type = django_context.resolve_lookup_expected_type(ctx, model_cls, lookup_kwarg)
|
|
# Managers as provided_type is not supported yet
|
|
if isinstance(provided_type, Instance) and helpers.has_any_of_bases(
|
|
provided_type.type, (fullnames.MANAGER_CLASS_FULLNAME, fullnames.QUERYSET_CLASS_FULLNAME)
|
|
):
|
|
return ctx.default_return_type
|
|
|
|
helpers.check_types_compatible(
|
|
ctx,
|
|
expected_type=lookup_type,
|
|
actual_type=provided_type,
|
|
error_message=f"Incompatible type for lookup {lookup_kwarg!r}:",
|
|
)
|
|
|
|
return ctx.default_return_type
|
|
|
|
|
|
def resolve_combinable_type(combinable_type: Instance, django_context: DjangoContext) -> MypyType:
|
|
if combinable_type.type.fullname != fullnames.F_EXPRESSION_FULLNAME:
|
|
# Combinables aside from F expressions are unsupported
|
|
return AnyType(TypeOfAny.explicit)
|
|
|
|
return django_context.resolve_f_expression_type(combinable_type)
|