From a71ac8a49cf9529d5c0bb099f34709c738bc005b Mon Sep 17 00:00:00 2001 From: STerliakov Date: Thu, 10 Jul 2025 03:37:03 +0200 Subject: [PATCH] Move `is_defined_in_stub` to shared checker API to break import cycle --- mypy/checker.py | 3 +++ mypy/checker_shared.py | 4 ++++ mypy/plugins/enums.py | 6 +++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index 64bcc0871c38..159569849061 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -7889,6 +7889,9 @@ def has_valid_attribute(self, typ: Type, name: str) -> bool: def get_expression_type(self, node: Expression, type_context: Type | None = None) -> Type: return self.expr_checker.accept(node, type_context=type_context) + def is_defined_in_stub(self, typ: Instance, /) -> bool: + return self.modules[typ.type.module_name].is_stub + def check_deprecated(self, node: Node | None, context: Context) -> None: """Warn if deprecated and not directly imported with a `from` statement.""" if isinstance(node, Decorator): diff --git a/mypy/checker_shared.py b/mypy/checker_shared.py index a9cbae643dca..65cec41d5202 100644 --- a/mypy/checker_shared.py +++ b/mypy/checker_shared.py @@ -277,6 +277,10 @@ def checking_await_set(self) -> Iterator[None]: def get_precise_awaitable_type(self, typ: Type, local_errors: ErrorWatcher) -> Type | None: raise NotImplementedError + @abstractmethod + def is_defined_in_stub(self, typ: Instance, /) -> bool: + raise NotImplementedError + class CheckerScope: # We keep two stacks combined, to maintain the relative order diff --git a/mypy/plugins/enums.py b/mypy/plugins/enums.py index 860c56c63570..d21b21fb39f8 100644 --- a/mypy/plugins/enums.py +++ b/mypy/plugins/enums.py @@ -17,7 +17,7 @@ from typing import TypeVar, cast import mypy.plugin # To avoid circular imports. -from mypy.checker import TypeChecker +from mypy.checker_shared import TypeCheckerSharedApi from mypy.nodes import TypeInfo, Var from mypy.subtypes import is_equivalent from mypy.typeops import fixup_partial_type, make_simplified_union @@ -122,8 +122,8 @@ def _infer_value_type_with_auto_fallback( def _is_defined_in_stub(ctx: mypy.plugin.AttributeContext) -> bool: - assert isinstance(ctx.api, TypeChecker) - return isinstance(ctx.type, Instance) and ctx.api.modules[ctx.type.type.module_name].is_stub + assert isinstance(ctx.api, TypeCheckerSharedApi) + return isinstance(ctx.type, Instance) and ctx.api.is_defined_in_stub(ctx.type) def _implements_new(info: TypeInfo) -> bool: