Skip to content

Commit

Permalink
Clean up NotRequired class
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Gilbert committed Jun 10, 2024
1 parent efeeefd commit 6218c46
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 35 deletions.
10 changes: 1 addition & 9 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ConstraintAttacherProcessor,
)
from timeloopfe.common.processor import Processor
from timeloopfe.common.nodes import DictNode, ParseError, not_required_
from timeloopfe.common.nodes import DictNode, ParseError
from timeloopfe.v4.specification import Specification
from timeloopfe.v4.arch import (
Component,
Expand Down Expand Up @@ -74,14 +74,6 @@ def process(self, spec: Specification):
print(f"Checking {e.name} {e.__class__.__name__}")
e.pop("for_testing_ignoreme", None)

def remove_not_required(x):
if isinstance(x, DictNode):
to_remove = [ k for k, v in x.items() if v is not_required_ ]
for k in to_remove:
x.pop(k)

spec.recursive_apply(remove_not_required, self_first=True)


spec = self.get_spec(processors=[Test])
spec.architecture.nodes.insert(
Expand Down
27 changes: 2 additions & 25 deletions timeloopfe/common/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,7 @@ def __repr__(self):
return "REQUIRED"


class NotRequired:
"""
Class to represent a value that is not required in parsed output.
See how it is used as `default` in `TypeSpecifier`.
"""

def __copy__(self):
return self

def __deepcopy__(self, memo):
return self

default_unspecified_ = Unspecified()
not_required_ = NotRequired()


T = TypeVar("T", bound="Node")
Expand Down Expand Up @@ -136,16 +122,13 @@ def removed_by_str(self):
return ""

def cast_check_type(self, value: Any, node: "Node", key: str) -> Any:
if value is default_unspecified_ and self.default is not not_required_:
if value is default_unspecified_:
assert self.default is not default_unspecified_, (
"Can not call cast_check_type() with default_unspecified_"
"if default value is also default_unspecified_."
)
value = copy.deepcopy(self.default)

if self.default is not_required_ and value is default_unspecified_:
return not_required_

try:
casted = self.cast(value)
except Exception as exc:
Expand Down Expand Up @@ -211,9 +194,6 @@ def check_type(self, value: Any, node: "Node", key: str):
else (self.required_type,)
)

if self.default is not_required_ and value is not_required_:
return True

for s in t:
if isinstance(s, str) and str(value) == s:
return True
Expand Down Expand Up @@ -556,11 +536,8 @@ def _parse_elem(
f'Could not combine values in indices "{key}" and '
f'"{newkey}" in {self.get_name()}. {exc}' # type: ignore
) from exc
elif v is not not_required_:
self[key] = v
else:
assert v is not_required_
self.pop(key)
self[key] = v
self.__currently_parsing_index = None

def _parse_elems(self):
Expand Down
1 change: 0 additions & 1 deletion timeloopfe/v4/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from .sparse_optimizations import SparseOptimizations
from .globals import Globals
from .mapspace import Mapspace
from ..common.nodes import not_required_, NotRequired
from ..common.processor import ProcessorError, References2CopiesProcessor
from .output_parsing import parse_timeloop_output, OutputStats

Expand Down

0 comments on commit 6218c46

Please sign in to comment.