Skip to content

Commit a1cf0ce

Browse files
committed
refactor to remove import resolution; correct incorrect test results
1 parent b5cbc5a commit a1cf0ce

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

linkml_runtime/utils/schemaview.py

+12-21
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,6 @@ def is_absolute_path(path: str) -> bool:
108108
drive, tail = os.path.splitdrive(norm_path)
109109
return bool(drive and tail)
110110

111-
def _resolve_import(source_sch: str, imported_sch: str) -> str:
112-
if os.path.isabs(imported_sch):
113-
# Absolute import paths are not modified
114-
return imported_sch
115-
if urlparse(imported_sch).scheme:
116-
# File with URL schemes are not modified
117-
return imported_sch
118-
119-
if WINDOWS:
120-
path = PurePath(os.path.normpath(PurePath(source_sch).parent / imported_sch)).as_posix()
121-
else:
122-
path = os.path.normpath(str(Path(source_sch).parent / imported_sch))
123-
124-
if imported_sch.startswith(".") and not path.startswith("."):
125-
# Above condition handles cases where both source schema and imported schema are relative paths: these should remain relative
126-
return f"./{path}"
127-
128-
return path
129-
130111

131112
@dataclass
132113
class SchemaUsage:
@@ -319,14 +300,24 @@ def imports_closure(self, imports: bool = True, traverse: Optional[bool] = None,
319300
# path, and the target import doesn't have : (as in a curie or a URI)
320301
# we prepend the relative path. This WILL make the key in the `schema_map` not
321302
# equal to the literal text specified in the importing schema, but this is
322-
# essential to sensible deduplication: eg. for
303+
# essential to sensible deduplication: e.g. for
323304
# - main.yaml (imports ./types.yaml, ./subdir/subschema.yaml)
324305
# - types.yaml
325306
# - subdir/subschema.yaml (imports ./types.yaml)
326307
# - subdir/types.yaml
327308
# we should treat the two `types.yaml` as separate schemas from the POV of the
328309
# origin schema.
329-
i = _resolve_import(sn, i)
310+
311+
# if i is not a CURIE and sn looks like a path with at least one parent folder,
312+
# normalise i with respect to sn
313+
if "/" in sn and ":" not in i:
314+
if WINDOWS:
315+
# This cannot be simplified. os.path.normpath() must be called before .as_posix()
316+
i = PurePath(
317+
os.path.normpath(PurePath(sn).parent / i)
318+
).as_posix()
319+
else:
320+
i = os.path.normpath(str(Path(sn).parent / i))
330321
todo.append(i)
331322

332323
# add item to closure

tests/test_utils/test_schemaview.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ def test_imports_relative():
544544
'../../L0_1/L1_1_0/index',
545545
'../../L0_1/cousin',
546546
'../L1_0_1/dupe',
547-
'L2_0_0_0/child',
548-
'L2_0_0_1/child',
547+
'./L2_0_0_0/child',
548+
'./L2_0_0_1/child',
549549
'L2_0_0_2/two',
550550
'L2_0_0_2/one',
551551
'L2_0_0_2/four',

0 commit comments

Comments
 (0)