Skip to content

Type and documentation improvements to JSON-LD API #3169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 60 additions & 22 deletions rdflib/plugins/shared/jsonld/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@
# https://github.com/RDFLib/rdflib-jsonld/blob/feature/json-ld-1.1/rdflib_jsonld/context.py
from __future__ import annotations

from collections import namedtuple
from collections.abc import Collection, Generator
from typing import (
TYPE_CHECKING,
Any,
Optional,
Union,
)
from typing import TYPE_CHECKING, Any, NamedTuple, Optional, Union
from urllib.parse import urljoin, urlsplit

from rdflib.namespace import RDF
Expand Down Expand Up @@ -73,27 +67,45 @@ class Defined(int):


class Context:
"""
A JSON-LD context, which contains term definitions
"""

_base: str | None
_alias: dict[str, list[str]]
_lookup: dict[tuple[str, Any, Defined | str, bool], Term]
_prefixes: dict[str, Any]
_context_cache: dict[str, Any]

version: float
language: str | None
doc_base: str | None
vocab: str | None
active: bool
propagate: bool
terms: dict[str, Any]
parent: Context | None

def __init__(
self,
source: _ContextSourceType = None,
base: str | None = None,
version: float | None = 1.1,
):
self.version: float = version or 1.1
self.version = version or 1.1
self.language = None
self.vocab: str | None = None
self._base: str | None
self.vocab = None
self.base = base
self.doc_base = base
self.terms: dict[str, Any] = {}
self.terms = {}
# _alias maps NODE_KEY to list of aliases
self._alias: dict[str, list[str]] = {}
self._lookup: dict[tuple[str, Any, Defined | str, bool], Term] = {}
self._prefixes: dict[str, Any] = {}
self._alias = {}
self._lookup = {}
self._prefixes = {}
self.active = False
self.parent: Context | None = None
self.parent = None
self.propagate = True
self._context_cache: dict[str, Any] = {}
self._context_cache = {}
if source:
self.load(source)

Expand Down Expand Up @@ -668,9 +680,35 @@ def to_dict(self) -> dict[str, Any]:
return r


Term = namedtuple(
"Term",
"id, name, type, container, index, language, reverse, context," "prefix, protected",
)

Term.__new__.__defaults__ = (UNDEF, UNDEF, UNDEF, UNDEF, False, UNDEF, False, False)
class Term(NamedTuple):
"""
Describes how a JSON key should be interpreted when parsed as RDF
"""

#: The IRI or CURIE of the term.
id: str
#: The name of the term, ie an alias for the id.
name: str
#: The type of the term, such as @id, @json, @none or @vocab
type: Defined | str = UNDEF
#: The container type, such as @graph, @id, @index, @language, @list, @set or @type,
container: Collection[Any] | str | Defined = UNDEF
#: A predicate IRI that should be used to interpret keys of this object,
#: when used alongside `@container: @index`.
#: See https://www.w3.org/TR/json-ld11/#property-based-data-indexing
index: str | Defined | None = None
#: The language to be used for values of this term
language: str | Defined | None = UNDEF
#: Indicates that this term is a reverse property, so subject and object are swapped.
#: https://www.w3.org/TR/json-ld11/#reverse-properties
reverse: bool = False
#: A scoped context used inside values that use this term.
#: See https://www.w3.org/TR/json-ld11/#scoped-contexts
context: Any = UNDEF
#: If true, indicates that this should be used during compaction.
#: If false, indicates that this term cannot be used in compaction.
#: See https://www.w3.org/TR/json-ld11/#compact-iris
prefix: bool | None = None
#: If true, marks the term as protected, meaning it cannot be overridden by a subcontext.
#: See https://www.w3.org/TR/json-ld11/#protected-term-definitions
protected: bool = False
Loading