Set custom queryset methods as manager attrs instead of method copies (#820)

Instead of copying methods over from a QuerySet passed to a basemanager
when invoking '<BaseManager>.from_queryset', any QuerySet methods are
declared as attributes on the manager.

This allows us to properly lookup any QuerySet method types via a
'get_attribute_hook' and will thus remove disorienting phantom errors
occuring from mypy trying to resolve types only existing in the module
where the _original_ (and real) queryset method was declared.
This commit is contained in:
Petter Friberg
2022-01-16 10:14:33 +01:00
committed by GitHub
parent 1da693ebff
commit 99f28387fb
6 changed files with 323 additions and 48 deletions

View File

@@ -34,7 +34,7 @@ from mypy.plugin import (
MethodContext,
SemanticAnalyzerPluginInterface,
)
from mypy.plugins.common import add_method
from mypy.plugins.common import add_method_to_class
from mypy.semanal import SemanticAnalyzer
from mypy.types import AnyType, CallableType, Instance, NoneTyp, TupleType
from mypy.types import Type as MypyType
@@ -383,7 +383,9 @@ def copy_method_to_another_class(
return
arguments, return_type = build_unannotated_method_args(method_node)
add_method(ctx, new_method_name, args=arguments, return_type=return_type, self_type=self_type)
add_method_to_class(
semanal_api, ctx.cls, new_method_name, args=arguments, return_type=return_type, self_type=self_type
)
return
method_type = method_node.type
@@ -421,7 +423,9 @@ def copy_method_to_another_class(
argument.set_line(original_argument)
arguments.append(argument)
add_method(ctx, new_method_name, args=arguments, return_type=return_type, self_type=self_type)
add_method_to_class(
semanal_api, ctx.cls, new_method_name, args=arguments, return_type=return_type, self_type=self_type
)
def add_new_manager_base(api: SemanticAnalyzerPluginInterface, fullname: str) -> None: