Skip to content

Commit f5d5961

Browse files
committed
allow relative imports to be imported without ./ at the start
1 parent 727b31c commit f5d5961

File tree

8 files changed

+80
-4
lines changed

8 files changed

+80
-4
lines changed

Diff for: linkml_runtime/utils/schemaview.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,14 @@ def imports_closure(self, imports: bool = True, traverse: Optional[bool] = None,
298298
# path, and the target import doesn't have : (as in a curie or a URI)
299299
# we prepend the relative path. This WILL make the key in the `schema_map` not
300300
# equal to the literal text specified in the importing schema, but this is
301-
# essential to sensible deduplication: eg. for
301+
# essential to sensible deduplication: e.g. for
302302
# - main.yaml (imports ./types.yaml, ./subdir/subschema.yaml)
303303
# - types.yaml
304304
# - subdir/subschema.yaml (imports ./types.yaml)
305305
# - subdir/types.yaml
306306
# we should treat the two `types.yaml` as separate schemas from the POV of the
307307
# origin schema.
308-
if sn.startswith('.') and ':' not in i:
308+
if '/' in sn and ':' not in i:
309309
if WINDOWS:
310310
# This cannot be simplified. os.path.normpath() must be called before .as_posix()
311311
i = PurePath(os.path.normpath(PurePath(sn).parent / i)).as_posix()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
id: four
2+
name: import_four
3+
title: Import Four
4+
description: |
5+
Import loaded by the StepChild class.
6+
imports:
7+
- linkml:types
8+
classes:
9+
Four:
10+
attributes:
11+
value:
12+
range: string
13+
ifabsent: "Four"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
id: one
2+
name: import_one
3+
title: Import One
4+
description: |
5+
Import loaded by the StepChild class.
6+
imports:
7+
- linkml:types
8+
- two
9+
classes:
10+
One:
11+
attributes:
12+
value:
13+
range: string
14+
ifabsent: "One"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
id: stepchild
2+
name: stepchild
3+
title: stepchild
4+
description: |
5+
Child class that imports files in the same directory as itself without consistently using `./` in the link notation.
6+
imports:
7+
- linkml:types
8+
- one
9+
- two
10+
- ./three
11+
classes:
12+
StepChild:
13+
attributes:
14+
value:
15+
range: string
16+
ifabsent: "StepChild"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
id: three
2+
name: import_three
3+
title: Import Three
4+
description: |
5+
Import loaded by the StepChild class.
6+
imports:
7+
- linkml:types
8+
- ./four
9+
classes:
10+
Three:
11+
attributes:
12+
value:
13+
range: string
14+
ifabsent: "Three"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
id: two
2+
name: import_two
3+
title: Import Two
4+
description: |
5+
Import loaded by the StepChild class.
6+
imports:
7+
- linkml:types
8+
classes:
9+
Two:
10+
attributes:
11+
value:
12+
range: string
13+
ifabsent: "Two"

Diff for: tests/test_utils/input/imports_relative/L0_0/L1_0_0/main.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ imports:
88
- ../../L0_1/cousin
99
- ./L2_0_0_0/child
1010
- ./L2_0_0_1/child
11+
- L2_0_0_2/stepchild
1112
classes:
1213
Main:
1314
description: "Our intrepid main class!"
1415
attributes:
1516
value:
1617
range: string
17-
ifabsent: "Main"
18+
ifabsent: "Main"

Diff for: tests/test_utils/test_schemaview.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ def test_imports_relative():
539539
'../L1_0_1/dupe',
540540
'./L2_0_0_0/child',
541541
'./L2_0_0_1/child',
542+
'L2_0_0_2/two',
543+
'L2_0_0_2/one',
544+
'L2_0_0_2/four',
545+
'L2_0_0_2/three',
546+
'L2_0_0_2/stepchild',
542547
'main'
543548
]
544549

@@ -896,4 +901,4 @@ def test_materialize_nonscalar_slot_usage():
896901
assert cls.attributes["tempo"].annotations.expected_value.value == "a number between 0 and 200"
897902
assert cls.attributes["tempo"].annotations.preferred_unit.value == "BPM"
898903
assert cls.attributes["tempo"].domain_of == ["DJController"]
899-
assert cls.slot_usage["tempo"].domain_of == []
904+
assert cls.slot_usage["tempo"].domain_of == []

0 commit comments

Comments
 (0)