Skip to content

Make from_jsonld() respect ignore_unexpected_keys - #179

Merged
apdavison merged 1 commit into
HumanBrainProject:masterfrom
apdavison:from-jsonld-unexpected-keys
Oct 20, 2026
Merged

apdavison merged 1 commit into
HumanBrainProject:masterfrom
apdavison:from-jsonld-unexpected-keys

Conversation

@apdavison

Copy link
Copy Markdown
Member

Currently, KGObject.from_jsonld() accepts an ignore_unexpected_keys argument but ignores it: keys that are not properties of the class are always silently dropped. The parameter can't simply be removed, since openMINDS Python's load(), which fairgraph classes inherit, passes it on.

With this PR, from_jsonld() behaves as in openMINDS Python: by default, a NameError is raised if the document contains keys that are neither properties of the class nor @context, @id or @type. This catches mistakes in JSON-LD documents written by hand, e.g. a misspelt property name, which are currently lost without any warning. It also applies to load() and Collection.load().

Documents from the Knowledge Graph always contain other keys (the space, revision, identifiers, …), so everywhere fairgraph reads KG documents, it passes ignore_unexpected_keys=True. Code that calls from_jsonld() on KG documents directly will need to do the same: this is noted in the release notes as a change in behaviour.

The choice applies to the whole document, including linked nodes included in it (e.g. with follow_links). Those are deserialized by openMINDS Python, which calls from_jsonld() without the argument, so the choice is passed to them through a context variable that is set for the duration of the outer call.

As in openMINDS Python, raise NameError by default if a JSON-LD document contains keys that are not properties of the class, including in the linked nodes it contains, so that mistakes in documents written by hand are caught. Documents from the Knowledge Graph contain other keys, such as the space, so fairgraph always reads them with ignore_unexpected_keys=True, which now applies to the whole document.
@apdavison apdavison added the bug Something isn't working label Oct 20, 2026
@apdavison apdavison added this to the 0.16 milestone Oct 20, 2026
@apdavison
apdavison merged commit 12c6b97 into HumanBrainProject:master Oct 20, 2026
9 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in fairgraph development Oct 20, 2026
@apdavison
apdavison deleted the from-jsonld-unexpected-keys branch October 20, 2026 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Development

Successfully merging this pull request may close these issues.

1 participant