Skip to content

Commit

Permalink
Merge pull request #7 from linkml/issue_6
Browse files Browse the repository at this point in the history
Fix for issue #6
  • Loading branch information
hsolbrig authored Jun 3, 2021
2 parents d820b44 + a5bf905 commit 54e19cb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rdflib-jsonld = "~=0.5"
click = "*"
prefixcommons = "*"
shexjsg = "~=0.7"
deprecated = "*"

[dev-packages]

Expand Down
12 changes: 8 additions & 4 deletions linkml_runtime/utils/yamlutils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
39 changes: 39 additions & 0 deletions tests/test_issues/test_issue_6.py
Original file line number Diff line number Diff line change
@@ -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 "<unicode string>", line 3, col 8: ', TypedNode.yaml_loc(inp['foo']['x']))
self.assertEqual('File "<unicode string>", line 3, col 8', TypedNode.yaml_loc(inp['foo']['x'], suffix=''))
self.assertEqual('File "<unicode string>", line 4, col 8: ', TypedNode.yaml_loc(inp['foo']['y']))
self.assertEqual('File "<unicode string>", line 4, col 8I yam that I yam',
TypedNode.yaml_loc(inp['foo']['y'], suffix=inp['foo']['y']))
self.assertEqual('File "<unicode string>", line 5, col 8: ', TypedNode.yaml_loc(inp['foo']['z']))

outs = StringIO()
with redirect_stderr(outs):
self.assertEqual('File "<unicode string>", 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()

0 comments on commit 54e19cb

Please sign in to comment.