-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2fdf6fd
Make Options work under mypyc
msullivan 069649a
fix it to *also* still work under cpython...
msullivan 3f14f68
fix lint
msullivan 3b9618b
Drop __eq__ on Options
msullivan b336dc8
add a comment
msullivan b73125d
Revert "add a comment"
msullivan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,23 +214,23 @@ 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__ | ||
|
||
def __ne__(self, other: object) -> bool: | ||
return not self == other | ||
|
||
def __repr__(self) -> str: | ||
return 'Options({})'.format(pprint.pformat(self.snapshot())) | ||
|
||
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 +286,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 +332,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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)?