-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Make Options work under mypyc #5518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
2fdf6fd
069649a
3f14f68
3b9618b
b336dc8
b73125d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,54 +4,59 @@ | |
import sys | ||
|
||
from typing import Dict, List, Mapping, Optional, Pattern, Set, Tuple | ||
MYPY = False | ||
if MYPY: | ||
from typing import ClassVar | ||
|
||
from mypy import defaults | ||
from mypy.util import get_class_descriptors, replace_object_state | ||
|
||
|
||
class BuildType: | ||
STANDARD = 0 | ||
MODULE = 1 | ||
PROGRAM_TEXT = 2 | ||
STANDARD = 0 # type: ClassVar[int] | ||
MODULE = 1 # type: ClassVar[int] | ||
PROGRAM_TEXT = 2 # type: ClassVar[int] | ||
|
||
|
||
PER_MODULE_OPTIONS = { | ||
# Please keep this list sorted | ||
"always_false", | ||
"always_true", | ||
"check_untyped_defs", | ||
"debug_cache", | ||
"disallow_any_decorated", | ||
"disallow_any_explicit", | ||
"disallow_any_expr", | ||
"disallow_any_generics", | ||
"disallow_any_unimported", | ||
"disallow_incomplete_defs", | ||
"disallow_subclassing_any", | ||
"disallow_untyped_calls", | ||
"disallow_untyped_decorators", | ||
"disallow_untyped_defs", | ||
"follow_imports", | ||
"follow_imports_for_stubs", | ||
"ignore_errors", | ||
"ignore_missing_imports", | ||
"local_partial_types", | ||
"no_implicit_optional", | ||
"show_none_errors", | ||
"strict_boolean", | ||
"strict_optional", | ||
"strict_optional_whitelist", | ||
"warn_no_return", | ||
"warn_return_any", | ||
"warn_unused_ignores", | ||
} | ||
|
||
OPTIONS_AFFECTING_CACHE = ((PER_MODULE_OPTIONS | | ||
{"quick_and_dirty", "platform", "bazel"}) | ||
- {"debug_cache"}) | ||
|
||
|
||
class Options: | ||
"""Options collected from flags.""" | ||
|
||
PER_MODULE_OPTIONS = { | ||
# Please keep this list sorted | ||
"always_false", | ||
"always_true", | ||
"check_untyped_defs", | ||
"debug_cache", | ||
"disallow_any_decorated", | ||
"disallow_any_explicit", | ||
"disallow_any_expr", | ||
"disallow_any_generics", | ||
"disallow_any_unimported", | ||
"disallow_incomplete_defs", | ||
"disallow_subclassing_any", | ||
"disallow_untyped_calls", | ||
"disallow_untyped_decorators", | ||
"disallow_untyped_defs", | ||
"follow_imports", | ||
"follow_imports_for_stubs", | ||
"ignore_errors", | ||
"ignore_missing_imports", | ||
"local_partial_types", | ||
"no_implicit_optional", | ||
"show_none_errors", | ||
"strict_boolean", | ||
"strict_optional", | ||
"strict_optional_whitelist", | ||
"warn_no_return", | ||
"warn_return_any", | ||
"warn_unused_ignores", | ||
} | ||
|
||
OPTIONS_AFFECTING_CACHE = ((PER_MODULE_OPTIONS | | ||
{"quick_and_dirty", "platform", "bazel"}) | ||
- {"debug_cache"}) | ||
|
||
def __init__(self) -> None: | ||
# Cache for clone_for_module() | ||
self.per_module_cache = None # type: Optional[Dict[str, Options]] | ||
|
@@ -209,12 +214,16 @@ def __init__(self) -> None: | |
|
||
def snapshot(self) -> object: | ||
"""Produce a comparable snapshot of this Option""" | ||
d = dict(self.__dict__) | ||
# Under mypyc, we don't have a __dict__, so we need to do worse things. | ||
d = dict(getattr(self, '__dict__', ())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So IIUC under mypyc d will start out empty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah |
||
for k in get_class_descriptors(Options): | ||
if hasattr(self, k): | ||
d[k] = getattr(self, k) | ||
del d['per_module_cache'] | ||
return d | ||
|
||
def __eq__(self, other: object) -> bool: | ||
return self.__class__ == other.__class__ and self.__dict__ == other.__dict__ | ||
return isinstance(other, Options) and self.snapshot() == other.snapshot() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remembered from when I was looking into this while doing the original snapshot stuff that the answer was "not often", but I looked more closely and the answer was: in exactly one test. I just removed |
||
|
||
def __ne__(self, other: object) -> bool: | ||
return not self == other | ||
|
@@ -224,8 +233,10 @@ def __repr__(self) -> str: | |
|
||
def apply_changes(self, changes: Dict[str, object]) -> 'Options': | ||
new_options = Options() | ||
new_options.__dict__.update(self.__dict__) | ||
new_options.__dict__.update(changes) | ||
# Under mypyc, we don't have a __dict__, so we need to do worse things. | ||
replace_object_state(new_options, self, copy_dict=True) | ||
for key, value in changes.items(): | ||
setattr(new_options, key, value) | ||
return new_options | ||
|
||
def build_per_module_cache(self) -> None: | ||
|
@@ -281,7 +292,7 @@ def clone_for_module(self, module: str) -> 'Options': | |
""" | ||
if self.per_module_cache is None: | ||
self.build_per_module_cache() | ||
assert self.per_module_cache is not None | ||
assert self.per_module_cache is not None | ||
|
||
# If the module just directly has a config entry, use it. | ||
if module in self.per_module_cache: | ||
|
@@ -327,4 +338,4 @@ def compile_glob(self, s: str) -> Pattern[str]: | |
return re.compile(expr + '\\Z') | ||
|
||
def select_options_affecting_cache(self) -> Mapping[str, object]: | ||
return {opt: getattr(self, opt) for opt in self.OPTIONS_AFFECTING_CACHE} | ||
return {opt: getattr(self, opt) for opt in OPTIONS_AFFECTING_CACHE} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a short comment why
PER_MODULE_OPTIONS
andOPTIONS_AFFECTING_CACHE
can't beClassVar
s inOptions
(similar to what you did forfastparse.py
IIRC)?