Schemaview: follow paths in multi-layer relative imports #330
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: linkml/linkml#1228
Specifically: linkml/linkml#1228 (comment)
#1228 describes two problems, one having to do with relative imports when using an already-loaded
SchemaDefinition
object withSchemaView
, which has already been fixed, and another, where multiple layers of relative imports wouldn't work.eg. Say you have three schema such that...
main.yaml
imports./a/a_schema.yaml
./a/a_schema.yaml
imports./b/b_schema.yaml
(or,./a/b/b_schema.yaml
from the POV ofmain.yaml
)Then
a_schema
would be correctly imported, butb_schema
wouldn't be, since it would previously be resolved relative to the directory ofmain.yaml
Additionally, one might anticipate schema developers to use an
index.yaml
style of creating nested schemas, where there might potentially be many schemas with a filenameindex.yaml
in different directories - we need to preserve those same-named schemas while also not getting into import cycles.This simple lil PR does that, and tests a bunch of different permutations of nth-layer imports (see
tests/test_utils/input/imports_relative/README.md
)This has to be done in the
imports_closure
method (rather than in the schema_wrap method, or makingimports_closure
pass the loaded schema as thefrom_schema
argument) in order to preserve same-named schemas in theimport_map
, which uses the string of the import (rather than eg.schema.id
orschema.name
) as the key. Note that while this does mean that the key inimport_map
will be different than the literal import string in the nth-layer schema (eg. in the above example, the key would be./a/b/b_schema.yaml
while the actual import statement was./b/b_schema.yaml
), that string is not meaningful to the origin schema, and the relative import from its location is unambiguous and allows for deduplication (which is tested)yet another "two lines of code but 1000 lines of tests" PR lol