-
Notifications
You must be signed in to change notification settings - Fork 25.4k
[dynamo] annotate config with @compile_ignored
#111303
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7d61a2b
[dynamo] annotate config with `@compile_ignored`
jon-chuang 15c1dec
Update on "[dynamo] annotate config with `@compile_ignored`"
jon-chuang bd6ea6c
Update on "[dynamo] annotate config with `@compile_ignored`"
jon-chuang bdcaaa2
Update on "[dynamo] annotate config with `@compile_ignored`"
jon-chuang 4e90866
Update on "[dynamo] annotate config with `@compile_ignored`"
jon-chuang 65ea1db
Update on "[dynamo] annotate config with `@compile_ignored`"
jon-chuang 9d56493
Update on "[dynamo] annotate config with `@compile_ignored`"
jon-chuang 33b73b2
Update on "[dynamo] annotate config with `@compile_ignored`"
jon-chuang 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import contextlib | ||
|
||
import copy | ||
import inspect | ||
import io | ||
import pickle | ||
import tokenize | ||
import unittest | ||
import warnings | ||
from types import FunctionType, ModuleType | ||
from typing import Any, Dict, Set | ||
from unittest import mock | ||
|
@@ -42,13 +46,61 @@ def visit(source, dest, prefix): | |
|
||
config = dict() | ||
default = dict() | ||
|
||
compile_ignored_keys = get_assignments_with_compile_ignored_comments(module) | ||
|
||
visit(module, module, "") | ||
module._config = config | ||
module._default = default | ||
module._allowed_keys = set(config.keys()) | ||
module._compile_ignored_keys = compile_ignored_keys | ||
module.__class__ = ConfigModuleInstance | ||
|
||
|
||
COMPILE_IGNORED_MARKER = "@compile_ignored" | ||
|
||
|
||
# Gets all the keys (i.e. assignments) with a @compile_ignored comment | ||
def get_assignments_with_compile_ignored_comments(module): | ||
source_code = inspect.getsource(module) | ||
assignments = set() | ||
|
||
# Tokenize the source code to retrieve comments | ||
tokens = tokenize.tokenize(io.BytesIO(source_code.encode("utf-8")).readline) | ||
current_comment = "", -1 | ||
jon-chuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
prev_name = "" | ||
prev_assigned = "", -1 | ||
|
||
for token in tokens: | ||
jon-chuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if token.type == tokenize.COMMENT: | ||
maybe_current = token.string.strip() | ||
if COMPILE_IGNORED_MARKER in maybe_current: | ||
assert current_comment == ( | ||
"", | ||
-1, | ||
), f"unconsumed {COMPILE_IGNORED_MARKER}" | ||
current_comment = maybe_current, token.start[0] | ||
if token.start[0] == prev_assigned[1]: | ||
# Check if the current assignment is followed with | ||
# a same-line comment with COMPILE_IGNORED_MARKER | ||
assignments.add(prev_assigned[0]) | ||
current_comment = "", -1 # reset | ||
elif token.type == tokenize.NAME: | ||
prev_name = token.string | ||
elif token.type == tokenize.OP and token.string == "=": | ||
prev_assigned = prev_name, token.start[0] | ||
# Check if the current assignment follows a comment | ||
# with COMPILE_IGNORED_MARKER | ||
if ( | ||
COMPILE_IGNORED_MARKER in current_comment[0] | ||
and current_comment[1] == token.start[0] - 1 | ||
): | ||
assignments.add(prev_name) | ||
current_comment = "", -1 # reset | ||
assert current_comment == ("", -1), f"unconsumed {COMPILE_IGNORED_MARKER}" | ||
return assignments | ||
|
||
|
||
class ConfigModule(ModuleType): | ||
# The default values of the configuration settings. This can be used to | ||
# determine if the config has been changed or not. | ||
|
@@ -59,6 +111,7 @@ class ConfigModule(ModuleType): | |
_config: Dict[str, Any] | ||
_allowed_keys: Set[str] | ||
_bypass_keys: Set[str] | ||
_compile_ignored_keys: Set[str] | ||
|
||
def __init__(self): | ||
raise NotImplementedError( | ||
|
@@ -106,12 +159,24 @@ def codegen_config(self): | |
lines.append(f"{mod}.{k} = {v!r}") | ||
return "\n".join(lines) | ||
|
||
def load_config(self, data): | ||
"""Restore from a prior call to save_config()""" | ||
self.to_dict().update(pickle.loads(data)) | ||
|
||
def to_dict(self): | ||
return self._config | ||
warnings.warn( | ||
( | ||
"config.to_dict() has been deprecated. It may no longer change the underlying config.", | ||
"use config.shallow_copy_dict() or config.get_config_copy() instead", | ||
), | ||
DeprecationWarning, | ||
) | ||
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. ok this seems legit |
||
return self.shallow_copy_dict() | ||
|
||
def shallow_copy_dict(self): | ||
return {**self._config} | ||
|
||
def load_config(self, config): | ||
"""Restore from a prior call to save_config() or shallow_copy_dict()""" | ||
if not isinstance(config, dict): | ||
config = pickle.loads(config) | ||
self._config.update(config) | ||
|
||
def get_config_copy(self): | ||
return copy.deepcopy(self._config) | ||
|
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.
Hmm, is this BC breaking?
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.
I added a deprecation warning to
to_dict