From 6218c4669e4b67d5b084f280f75ed9a4b7e2b02d Mon Sep 17 00:00:00 2001 From: Michael Gilbert Date: Mon, 10 Jun 2024 14:32:54 -0400 Subject: [PATCH] Clean up NotRequired class --- tests/test_node.py | 10 +--------- timeloopfe/common/nodes.py | 27 ++------------------------- timeloopfe/v4/specification.py | 1 - 3 files changed, 3 insertions(+), 35 deletions(-) diff --git a/tests/test_node.py b/tests/test_node.py index 423dfc7..1814624 100644 --- a/tests/test_node.py +++ b/tests/test_node.py @@ -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, @@ -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( diff --git a/timeloopfe/common/nodes.py b/timeloopfe/common/nodes.py index 15764d1..ba5787c 100644 --- a/timeloopfe/common/nodes.py +++ b/timeloopfe/common/nodes.py @@ -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") @@ -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: @@ -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 @@ -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): diff --git a/timeloopfe/v4/specification.py b/timeloopfe/v4/specification.py index e54d8ad..6d7bb3a 100644 --- a/timeloopfe/v4/specification.py +++ b/timeloopfe/v4/specification.py @@ -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