mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-12 23:16:31 +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.
83 lines
3.5 KiB
Python
83 lines
3.5 KiB
Python
from mypy.nodes import GDEF, Decorator, FuncDef, MemberExpr, NameExpr, RefExpr, StrExpr, SymbolTableNode, TypeInfo
|
|
from mypy.plugin import ClassDefContext, DynamicClassDefContext
|
|
from mypy.types import AnyType, Instance, TypeOfAny
|
|
|
|
from mypy_django_plugin.lib import fullnames, helpers
|
|
|
|
|
|
def create_new_manager_class_from_from_queryset_method(ctx: DynamicClassDefContext) -> None:
|
|
semanal_api = helpers.get_semanal_api(ctx)
|
|
|
|
callee = ctx.call.callee
|
|
assert isinstance(callee, MemberExpr)
|
|
assert isinstance(callee.expr, RefExpr)
|
|
|
|
base_manager_info = callee.expr.node
|
|
if base_manager_info is None:
|
|
if not semanal_api.final_iteration:
|
|
semanal_api.defer()
|
|
return
|
|
|
|
assert isinstance(base_manager_info, TypeInfo)
|
|
|
|
passed_queryset = ctx.call.args[0]
|
|
assert isinstance(passed_queryset, NameExpr)
|
|
|
|
derived_queryset_fullname = passed_queryset.fullname
|
|
if derived_queryset_fullname is None:
|
|
# In some cases, due to the way the semantic analyzer works, only passed_queryset.name is available.
|
|
# But it should be analyzed again, so this isn't a problem.
|
|
return
|
|
|
|
new_manager_info = semanal_api.basic_new_typeinfo(
|
|
ctx.name, basetype_or_fallback=Instance(base_manager_info, [AnyType(TypeOfAny.unannotated)]), line=ctx.call.line
|
|
)
|
|
new_manager_info.line = ctx.call.line
|
|
new_manager_info.defn.line = ctx.call.line
|
|
new_manager_info.metaclass_type = new_manager_info.calculate_metaclass_type()
|
|
|
|
current_module = semanal_api.cur_mod_node
|
|
current_module.names[ctx.name] = SymbolTableNode(GDEF, new_manager_info, plugin_generated=True)
|
|
|
|
sym = semanal_api.lookup_fully_qualified_or_none(derived_queryset_fullname)
|
|
assert sym is not None
|
|
if sym.node is None:
|
|
if not semanal_api.final_iteration:
|
|
semanal_api.defer()
|
|
else:
|
|
# inherit from Any to prevent false-positives, if queryset class cannot be resolved
|
|
new_manager_info.fallback_to_any = True
|
|
return
|
|
|
|
derived_queryset_info = sym.node
|
|
assert isinstance(derived_queryset_info, TypeInfo)
|
|
|
|
if len(ctx.call.args) > 1:
|
|
expr = ctx.call.args[1]
|
|
assert isinstance(expr, StrExpr)
|
|
custom_manager_generated_name = expr.value
|
|
else:
|
|
custom_manager_generated_name = base_manager_info.name + "From" + derived_queryset_info.name
|
|
|
|
custom_manager_generated_fullname = ".".join(["django.db.models.manager", custom_manager_generated_name])
|
|
if "from_queryset_managers" not in base_manager_info.metadata:
|
|
base_manager_info.metadata["from_queryset_managers"] = {}
|
|
base_manager_info.metadata["from_queryset_managers"][custom_manager_generated_fullname] = new_manager_info.fullname
|
|
|
|
class_def_context = ClassDefContext(cls=new_manager_info.defn, reason=ctx.call, api=semanal_api)
|
|
self_type = Instance(new_manager_info, [])
|
|
# we need to copy all methods in MRO before django.db.models.query.QuerySet
|
|
for class_mro_info in derived_queryset_info.mro:
|
|
if class_mro_info.fullname == fullnames.QUERYSET_CLASS_FULLNAME:
|
|
break
|
|
for name, sym in class_mro_info.names.items():
|
|
if isinstance(sym.node, FuncDef):
|
|
func_node = sym.node
|
|
elif isinstance(sym.node, Decorator):
|
|
func_node = sym.node.func
|
|
else:
|
|
continue
|
|
helpers.copy_method_to_another_class(
|
|
class_def_context, self_type, new_method_name=name, method_node=func_node
|
|
)
|