MCPcopy Index your code
hub / github.com/python/mypy / Scope

Class Scope

mypy/scope.py:17–125  ·  view source on GitHub ↗

Track which target we are processing at any given time.

Source from the content-addressed store, hash-verified

15
16
17class Scope:
18 """Track which target we are processing at any given time."""
19
20 def __init__(self) -> None:
21 self.module: str | None = None
22 self.classes: list[TypeInfo] = []
23 self.function: FuncBase | None = None
24 self.functions: list[FuncBase] = []
25 # Number of nested scopes ignored (that don't get their own separate targets)
26 self.ignored = 0
27
28 def current_module_id(self) -> str:
29 assert self.module
30 return self.module
31
32 def current_target(self) -> str:
33 """Return the current target (non-class; for a class return enclosing module)."""
34 assert self.module
35 if self.function:
36 fullname = self.function.fullname
37 return fullname or ""
38 return self.module
39
40 def current_full_target(self) -> str:
41 """Return the current target (may be a class)."""
42 assert self.module
43 if self.function:
44 return self.function.fullname
45 if self.classes:
46 return self.classes[-1].fullname
47 return self.module
48
49 def current_type_name(self) -> str | None:
50 """Return the current type's short name if it exists"""
51 return self.classes[-1].name if self.classes else None
52
53 def current_function_name(self) -> str | None:
54 """Return the current function's short name if it exists"""
55 return self.function.name if self.function else None
56
57 @contextmanager
58 def module_scope(self, prefix: str) -> Iterator[None]:
59 self.module = prefix
60 self.classes = []
61 self.function = None
62 self.ignored = 0
63 yield
64 assert self.module
65 self.module = None
66
67 @contextmanager
68 def function_scope(self, fdef: FuncBase) -> Iterator[None]:
69 self.functions.append(fdef)
70 if not self.function:
71 self.function = fdef
72 else:
73 # Nested functions are part of the topmost function target.
74 self.ignored += 1

Callers 4

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…