|
4 | 4 | import sys
|
5 | 5 |
|
6 | 6 | from typing import Dict, List, Mapping, Optional, Pattern, Set, Tuple
|
| 7 | +MYPY = False |
| 8 | +if MYPY: |
| 9 | + from typing import ClassVar |
7 | 10 |
|
8 | 11 | from mypy import defaults
|
| 12 | +from mypy.util import get_class_descriptors, replace_object_state |
9 | 13 |
|
10 | 14 |
|
11 | 15 | class BuildType:
|
12 |
| - STANDARD = 0 |
13 |
| - MODULE = 1 |
14 |
| - PROGRAM_TEXT = 2 |
| 16 | + STANDARD = 0 # type: ClassVar[int] |
| 17 | + MODULE = 1 # type: ClassVar[int] |
| 18 | + PROGRAM_TEXT = 2 # type: ClassVar[int] |
| 19 | + |
| 20 | + |
| 21 | +PER_MODULE_OPTIONS = { |
| 22 | + # Please keep this list sorted |
| 23 | + "always_false", |
| 24 | + "always_true", |
| 25 | + "check_untyped_defs", |
| 26 | + "debug_cache", |
| 27 | + "disallow_any_decorated", |
| 28 | + "disallow_any_explicit", |
| 29 | + "disallow_any_expr", |
| 30 | + "disallow_any_generics", |
| 31 | + "disallow_any_unimported", |
| 32 | + "disallow_incomplete_defs", |
| 33 | + "disallow_subclassing_any", |
| 34 | + "disallow_untyped_calls", |
| 35 | + "disallow_untyped_decorators", |
| 36 | + "disallow_untyped_defs", |
| 37 | + "follow_imports", |
| 38 | + "follow_imports_for_stubs", |
| 39 | + "ignore_errors", |
| 40 | + "ignore_missing_imports", |
| 41 | + "local_partial_types", |
| 42 | + "no_implicit_optional", |
| 43 | + "show_none_errors", |
| 44 | + "strict_boolean", |
| 45 | + "strict_optional", |
| 46 | + "strict_optional_whitelist", |
| 47 | + "warn_no_return", |
| 48 | + "warn_return_any", |
| 49 | + "warn_unused_ignores", |
| 50 | +} |
| 51 | + |
| 52 | +OPTIONS_AFFECTING_CACHE = ((PER_MODULE_OPTIONS | |
| 53 | + {"quick_and_dirty", "platform", "bazel"}) |
| 54 | + - {"debug_cache"}) |
15 | 55 |
|
16 | 56 |
|
17 | 57 | class Options:
|
18 | 58 | """Options collected from flags."""
|
19 | 59 |
|
20 |
| - PER_MODULE_OPTIONS = { |
21 |
| - # Please keep this list sorted |
22 |
| - "always_false", |
23 |
| - "always_true", |
24 |
| - "check_untyped_defs", |
25 |
| - "debug_cache", |
26 |
| - "disallow_any_decorated", |
27 |
| - "disallow_any_explicit", |
28 |
| - "disallow_any_expr", |
29 |
| - "disallow_any_generics", |
30 |
| - "disallow_any_unimported", |
31 |
| - "disallow_incomplete_defs", |
32 |
| - "disallow_subclassing_any", |
33 |
| - "disallow_untyped_calls", |
34 |
| - "disallow_untyped_decorators", |
35 |
| - "disallow_untyped_defs", |
36 |
| - "follow_imports", |
37 |
| - "follow_imports_for_stubs", |
38 |
| - "ignore_errors", |
39 |
| - "ignore_missing_imports", |
40 |
| - "local_partial_types", |
41 |
| - "no_implicit_optional", |
42 |
| - "show_none_errors", |
43 |
| - "strict_boolean", |
44 |
| - "strict_optional", |
45 |
| - "strict_optional_whitelist", |
46 |
| - "warn_no_return", |
47 |
| - "warn_return_any", |
48 |
| - "warn_unused_ignores", |
49 |
| - } |
50 |
| - |
51 |
| - OPTIONS_AFFECTING_CACHE = ((PER_MODULE_OPTIONS | |
52 |
| - {"quick_and_dirty", "platform", "bazel"}) |
53 |
| - - {"debug_cache"}) |
54 |
| - |
55 | 60 | def __init__(self) -> None:
|
56 | 61 | # Cache for clone_for_module()
|
57 | 62 | self.per_module_cache = None # type: Optional[Dict[str, Options]]
|
@@ -209,23 +214,23 @@ def __init__(self) -> None:
|
209 | 214 |
|
210 | 215 | def snapshot(self) -> object:
|
211 | 216 | """Produce a comparable snapshot of this Option"""
|
212 |
| - d = dict(self.__dict__) |
| 217 | + # Under mypyc, we don't have a __dict__, so we need to do worse things. |
| 218 | + d = dict(getattr(self, '__dict__', ())) |
| 219 | + for k in get_class_descriptors(Options): |
| 220 | + if hasattr(self, k): |
| 221 | + d[k] = getattr(self, k) |
213 | 222 | del d['per_module_cache']
|
214 | 223 | return d
|
215 | 224 |
|
216 |
| - def __eq__(self, other: object) -> bool: |
217 |
| - return self.__class__ == other.__class__ and self.__dict__ == other.__dict__ |
218 |
| - |
219 |
| - def __ne__(self, other: object) -> bool: |
220 |
| - return not self == other |
221 |
| - |
222 | 225 | def __repr__(self) -> str:
|
223 | 226 | return 'Options({})'.format(pprint.pformat(self.snapshot()))
|
224 | 227 |
|
225 | 228 | def apply_changes(self, changes: Dict[str, object]) -> 'Options':
|
226 | 229 | new_options = Options()
|
227 |
| - new_options.__dict__.update(self.__dict__) |
228 |
| - new_options.__dict__.update(changes) |
| 230 | + # Under mypyc, we don't have a __dict__, so we need to do worse things. |
| 231 | + replace_object_state(new_options, self, copy_dict=True) |
| 232 | + for key, value in changes.items(): |
| 233 | + setattr(new_options, key, value) |
229 | 234 | return new_options
|
230 | 235 |
|
231 | 236 | def build_per_module_cache(self) -> None:
|
@@ -281,7 +286,7 @@ def clone_for_module(self, module: str) -> 'Options':
|
281 | 286 | """
|
282 | 287 | if self.per_module_cache is None:
|
283 | 288 | self.build_per_module_cache()
|
284 |
| - assert self.per_module_cache is not None |
| 289 | + assert self.per_module_cache is not None |
285 | 290 |
|
286 | 291 | # If the module just directly has a config entry, use it.
|
287 | 292 | if module in self.per_module_cache:
|
@@ -327,4 +332,4 @@ def compile_glob(self, s: str) -> Pattern[str]:
|
327 | 332 | return re.compile(expr + '\\Z')
|
328 | 333 |
|
329 | 334 | def select_options_affecting_cache(self) -> Mapping[str, object]:
|
330 |
| - return {opt: getattr(self, opt) for opt in self.OPTIONS_AFFECTING_CACHE} |
| 335 | + return {opt: getattr(self, opt) for opt in OPTIONS_AFFECTING_CACHE} |
0 commit comments