diff --git a/caput/config.py b/caput/config.py index e1d5b280..7075a09f 100644 --- a/caput/config.py +++ b/caput/config.py @@ -462,10 +462,6 @@ def _prop(config): loglevels = ["DEBUG", "INFO", "WARNING", "ERROR", "NOTSET"] for key, level in config.items(): - # ignore hint at yaml file line number - if key == "__line__": - continue - level = level.upper() if level not in loglevels: raise ValueError( @@ -488,6 +484,12 @@ def _prop(config): return prop +class _line_dict(dict): + """A private dict subclass that also stores line numbers for debugging.""" + + __line__ = None + + class SafeLineLoader(SafeLoader): """ YAML loader that tracks line numbers. @@ -498,8 +500,10 @@ class SafeLineLoader(SafeLoader): def construct_mapping(self, node, deep=False): mapping = super(SafeLineLoader, self).construct_mapping(node, deep=deep) + mapping = _line_dict(mapping) + # Add 1 so numbering starts at 1 - mapping["__line__"] = node.start_mark.line + 1 + mapping.__line__ = node.start_mark.line + 1 return mapping @@ -521,8 +525,8 @@ class CaputConfigError(Exception): def __init__(self, message, file_=None, location=None): self.message = message self.file = file_ - if isinstance(location, dict): - self.line = location.get("__line__", None) + if isinstance(location, _line_dict): + self.line = location.__line__ else: self.line = None diff --git a/caput/pipeline.py b/caput/pipeline.py index 74237c2d..716aabef 100644 --- a/caput/pipeline.py +++ b/caput/pipeline.py @@ -672,7 +672,7 @@ def _setup_task(self, task_spec): # Check that only the expected keys are in the task spec. for key in task_spec.keys(): - if key not in ["type", "params", "requires", "in", "out", "__line__"]: + if key not in ["type", "params", "requires", "in", "out"]: raise config.CaputConfigError( "Task got an unexpected key '{}' in 'tasks' list.".format(key) ) @@ -882,9 +882,7 @@ def cacheable(self): def _from_config(cls, config): self = cls.__new__(cls) # Check for unused keys, but ignore the ones not put there by the user. - self.read_config( - config, compare_keys=["versions", "pipeline_config", "__line__"] - ) + self.read_config(config, compare_keys=["versions", "pipeline_config"]) self.__init__() return self diff --git a/caput/tests/test_metadata.py b/caput/tests/test_metadata.py index 6c360931..9bbde7f2 100644 --- a/caput/tests/test_metadata.py +++ b/caput/tests/test_metadata.py @@ -21,8 +21,6 @@ def test_default_params(self): self.assertDictEqual(man.all_tasks_params["versions"], {}) # remove line numbers pipeline_config = man.all_tasks_params["pipeline_config"] - del pipeline_config["__line__"] - del pipeline_config["pipeline"]["__line__"] self.assertDictEqual( pipeline_config, yaml.load(testconfig, Loader=yaml.SafeLoader), @@ -50,8 +48,6 @@ def test_metadata_params(self): # remove line numbers pipeline_config = man.all_tasks_params["pipeline_config"] - del pipeline_config["__line__"] - del pipeline_config["pipeline"]["__line__"] self.assertDictEqual( pipeline_config, yaml.load(testconfig, Loader=yaml.SafeLoader),