From a5bf90596af550d50e851776e0c41b6f0ea01637 Mon Sep 17 00:00:00 2001 From: hsolbrig Date: Thu, 3 Jun 2021 12:51:36 -0500 Subject: [PATCH] Fix for issue #6 Deprecated loc function in preference to internal _loc and do a few more tests on yaml_loc and the new suffix --- Pipfile | 1 + linkml_runtime/utils/yamlutils.py | 12 ++++++---- tests/test_issues/test_issue_6.py | 39 +++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/test_issues/test_issue_6.py diff --git a/Pipfile b/Pipfile index bed30eff..1141f915 100644 --- a/Pipfile +++ b/Pipfile @@ -18,6 +18,7 @@ rdflib-jsonld = "~=0.5" click = "*" prefixcommons = "*" shexjsg = "~=0.7" +deprecated = "*" [dev-packages] diff --git a/linkml_runtime/utils/yamlutils.py b/linkml_runtime/utils/yamlutils.py index c379c0cb..597c9ef4 100644 --- a/linkml_runtime/utils/yamlutils.py +++ b/linkml_runtime/utils/yamlutils.py @@ -1,9 +1,9 @@ from copy import copy -from dataclasses import dataclass from json import JSONDecoder from typing import Union, Any, List, Optional, Type, Callable, Dict import yaml +from deprecated.classic import deprecated from jsonasobj2 import JsonObj, as_json, as_dict, JsonObjTypes, items from rdflib import Graph from yaml.constructor import ConstructorError @@ -291,14 +291,18 @@ def add_node(self, node): self._len = node.end_mark.index - node.start_mark.index return self + @deprecated(reason="Use yaml_loc instead") def loc(self) -> str: + return self._loc() + + def _loc(self) -> str: return f'File "{self._s.name}", line {self._s.line + 1}, col {self._s.column + 1}' if self._s else '' @staticmethod - def yaml_loc(loc_str: Optional[Union["TypedNode", str]] = None) -> str: + def yaml_loc(loc_str: Optional[Union["TypedNode", str]] = None, suffix: Optional[str] = ": ") -> str: """ Return the yaml file and location of loc_str if it exists """ - return '' if loc_str is None or not hasattr(loc_str, "loc" or not callable(loc_str.loc)) else\ - (loc_str.loc() + ": ") + return '' if loc_str is None or not hasattr(loc_str, "_loc" or not callable(loc_str._loc)) else\ + (loc_str._loc() + suffix) class extended_str(str, TypedNode): diff --git a/tests/test_issues/test_issue_6.py b/tests/test_issues/test_issue_6.py new file mode 100644 index 00000000..f31294cb --- /dev/null +++ b/tests/test_issues/test_issue_6.py @@ -0,0 +1,39 @@ +import unittest +from contextlib import redirect_stderr +from io import StringIO + +import hbreader +import yaml + +from utils.yamlutils import DupCheckYamlLoader, TypedNode + +inp_yaml = """ +foo: + x: 17 + y: I yam that I yam + z: 12.43 +""" + + +class Issue6TestCase(unittest.TestCase): + def test_loc_function(self): + inp = yaml.load(hbreader.hbread(inp_yaml), DupCheckYamlLoader) + self.assertEqual('File "", line 3, col 8: ', TypedNode.yaml_loc(inp['foo']['x'])) + self.assertEqual('File "", line 3, col 8', TypedNode.yaml_loc(inp['foo']['x'], suffix='')) + self.assertEqual('File "", line 4, col 8: ', TypedNode.yaml_loc(inp['foo']['y'])) + self.assertEqual('File "", line 4, col 8I yam that I yam', + TypedNode.yaml_loc(inp['foo']['y'], suffix=inp['foo']['y'])) + self.assertEqual('File "", line 5, col 8: ', TypedNode.yaml_loc(inp['foo']['z'])) + + outs = StringIO() + with redirect_stderr(outs): + self.assertEqual('File "", line 3, col 8', TypedNode.loc(inp['foo']['x'])) + self.assertIn('Call to deprecated method loc. (Use yaml_loc instead)', outs.getvalue()) + + self.assertEqual('', TypedNode.yaml_loc(None)) + self.assertEqual('', TypedNode.yaml_loc("abc")) + self.assertEqual('', TypedNode.yaml_loc(['a', 'b'])) + + +if __name__ == '__main__': + unittest.main()