Skip to content

Commit a794ae3

Browse files
authored
Move is_defined_in_stub to shared checker API to break import cycle (#19417)
Follow-up after #19352 as requested by @JukkaL
1 parent 7ea925d commit a794ae3

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

mypy/checker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7889,6 +7889,9 @@ def has_valid_attribute(self, typ: Type, name: str) -> bool:
78897889
def get_expression_type(self, node: Expression, type_context: Type | None = None) -> Type:
78907890
return self.expr_checker.accept(node, type_context=type_context)
78917891

7892+
def is_defined_in_stub(self, typ: Instance, /) -> bool:
7893+
return self.modules[typ.type.module_name].is_stub
7894+
78927895
def check_deprecated(self, node: Node | None, context: Context) -> None:
78937896
"""Warn if deprecated and not directly imported with a `from` statement."""
78947897
if isinstance(node, Decorator):

mypy/checker_shared.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ def checking_await_set(self) -> Iterator[None]:
277277
def get_precise_awaitable_type(self, typ: Type, local_errors: ErrorWatcher) -> Type | None:
278278
raise NotImplementedError
279279

280+
@abstractmethod
281+
def is_defined_in_stub(self, typ: Instance, /) -> bool:
282+
raise NotImplementedError
283+
280284

281285
class CheckerScope:
282286
# We keep two stacks combined, to maintain the relative order

mypy/plugins/enums.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from typing import TypeVar, cast
1818

1919
import mypy.plugin # To avoid circular imports.
20-
from mypy.checker import TypeChecker
20+
from mypy.checker_shared import TypeCheckerSharedApi
2121
from mypy.nodes import TypeInfo, Var
2222
from mypy.subtypes import is_equivalent
2323
from mypy.typeops import fixup_partial_type, make_simplified_union
@@ -122,8 +122,8 @@ def _infer_value_type_with_auto_fallback(
122122

123123

124124
def _is_defined_in_stub(ctx: mypy.plugin.AttributeContext) -> bool:
125-
assert isinstance(ctx.api, TypeChecker)
126-
return isinstance(ctx.type, Instance) and ctx.api.modules[ctx.type.type.module_name].is_stub
125+
assert isinstance(ctx.api, TypeCheckerSharedApi)
126+
return isinstance(ctx.type, Instance) and ctx.api.is_defined_in_stub(ctx.type)
127127

128128

129129
def _implements_new(info: TypeInfo) -> bool:

0 commit comments

Comments
 (0)