-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathchecker_state.py
More file actions
30 lines (21 loc) · 858 Bytes
/
checker_state.py
File metadata and controls
30 lines (21 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from __future__ import annotations
from collections.abc import Iterator
from contextlib import contextmanager
from typing import Final
from mypy.checker_shared import TypeCheckerSharedApi
# This is global mutable state. Don't add anything here unless there's a very
# good reason.
class TypeCheckerState:
# Wrap this in a class since it's faster that using a module-level attribute.
def __init__(self, type_checker: TypeCheckerSharedApi | None) -> None:
# Value varies by file being processed
self.type_checker = type_checker
@contextmanager
def set(self, value: TypeCheckerSharedApi) -> Iterator[None]:
saved = self.type_checker
self.type_checker = value
try:
yield
finally:
self.type_checker = saved
checker_state: Final = TypeCheckerState(type_checker=None)