diff --git a/mypy/semanal.py b/mypy/semanal.py index c079a865af1de..c8a1295f1c141 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -5972,6 +5972,7 @@ def bind_name_expr(self, expr: NameExpr, sym: SymbolTableNode) -> None: ): self.fail(f'"{expr.name}" is a type variable and only valid in type context', expr) elif isinstance(sym.node, PlaceholderNode): + expr.fullname = sym.fullname or "" self.process_placeholder(expr.name, "name", expr) else: expr.kind = sym.kind diff --git a/test-data/unit/check-custom-plugin.test b/test-data/unit/check-custom-plugin.test index 504c153145537..1f1adc00c213f 100644 --- a/test-data/unit/check-custom-plugin.test +++ b/test-data/unit/check-custom-plugin.test @@ -723,6 +723,56 @@ class GenericQuerySet(Generic[T]): \[mypy] plugins=/test-data/unit/plugins/dyn_class_from_method.py +[case testDynamicClassHookFromImportedPlaceholder] +# flags: --config-file tmp/mypy.ini +from models import Row + +reveal_type(Row.objects.base_value()) # N: Revealed type is "builtins.int" +reveal_type(Row.objects.child_value()) # N: Revealed type is "builtins.str" +Row.objects.missing() # E: "ChildManager" has no attribute "missing" + +[file models.py] +from managers import ChildManager + +class Row: + objects = ChildManager() + +[file managers.py] +from base_managers import BaseManager, BaseQuerySet + +class ChildQuerySet(BaseQuerySet): + def child_value(self) -> str: + return "child" + +_ChildManager = BaseManager.from_queryset(ChildQuerySet) + +class ChildManager(_ChildManager): + pass + +[file base_managers.py] +from typing import Any, Generic, TypeVar +from models import Row + +T = TypeVar("T") + +class QuerySet(Generic[T]): + @classmethod + def from_queryset(cls, queryset_cls: Any) -> Any: ... + +class BaseQuerySet(QuerySet[Row]): + def base_value(self) -> int: + return 1 + +_BaseManager = QuerySet.from_queryset(BaseQuerySet) + +class BaseManager(_BaseManager): + pass + +[builtins fixtures/classmethod.pyi] +[file mypy.ini] +\[mypy] +plugins=/test-data/unit/plugins/dyn_class_from_method.py + [case testBaseClassPluginHookWorksIncremental] # flags: --config-file tmp/mypy.ini import a diff --git a/test-data/unit/plugins/dyn_class_from_method.py b/test-data/unit/plugins/dyn_class_from_method.py index 2630b16be66e4..8009ce99f9848 100644 --- a/test-data/unit/plugins/dyn_class_from_method.py +++ b/test-data/unit/plugins/dyn_class_from_method.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Callable +from collections.abc import Callable from mypy.nodes import ( GDEF, @@ -9,6 +9,7 @@ IndexExpr, MemberExpr, NameExpr, + PlaceholderNode, RefExpr, SymbolTable, SymbolTableNode, @@ -31,19 +32,36 @@ def get_dynamic_class_hook( def add_info_hook(ctx: DynamicClassDefContext) -> None: + fullname = ctx.api.qualified_name(ctx.name) + current = ctx.api.lookup_fully_qualified_or_none(fullname) + if current is not None and isinstance(current.node, TypeInfo): + return + assert isinstance(ctx.call.args[0], RefExpr) + queryset_type_fullname = ctx.call.args[0].fullname + queryset_node = ( + ctx.api.lookup_fully_qualified_or_none(queryset_type_fullname) + if queryset_type_fullname + else None + ) + if ( + queryset_node is None + or not isinstance(queryset_node.node, TypeInfo) + or not queryset_node.node.mro + ): + assert not ctx.api.final_iteration + if current is None or not isinstance(current.node, PlaceholderNode): + placeholder = PlaceholderNode(fullname, ctx.call, ctx.call.line, becomes_typeinfo=True) + ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, placeholder)) + ctx.api.defer() + return + queryset_info = queryset_node.node + class_def = ClassDef(ctx.name, Block([])) - class_def.fullname = ctx.api.qualified_name(ctx.name) + class_def.fullname = fullname info = TypeInfo(SymbolTable(), class_def, ctx.api.cur_mod_id) class_def.info = info - assert isinstance(ctx.call.args[0], RefExpr) - queryset_type_fullname = ctx.call.args[0].fullname - queryset_node = ctx.api.lookup_fully_qualified_or_none(queryset_type_fullname) - assert queryset_node is not None - queryset_info = queryset_node.node - assert isinstance(queryset_info, TypeInfo) - obj = ctx.api.named_type("builtins.object") - info.mro = [info, queryset_info, obj.type] + info.mro = [info, *queryset_info.mro] info.bases = [Instance(queryset_info, [])] ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info))