Skip to content

Commit cbecf23

Browse files
committed
Convert Term to a typed NamedTuple; expose types of Context fields
1 parent 53405b7 commit cbecf23

File tree

1 file changed

+60
-15
lines changed

1 file changed

+60
-15
lines changed

rdflib/plugins/shared/jsonld/context.py

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Any,
1313
Optional,
1414
Union,
15+
NamedTuple
1516
)
1617
from urllib.parse import urljoin, urlsplit
1718

@@ -73,27 +74,44 @@ class Defined(int):
7374

7475

7576
class Context:
77+
"""
78+
A JSON-LD context, which contains term definitions
79+
"""
80+
_base: str | None
81+
_alias: dict[str, list[str]]
82+
_lookup: dict[tuple[str, Any, Defined | str, bool], Term]
83+
_prefixes: dict[str, Any]
84+
_context_cache: dict[str, Any]
85+
86+
version: float
87+
language: str | None
88+
doc_base: str | None
89+
vocab: str | None
90+
active: bool
91+
propagate: bool
92+
terms: dict[str, Any]
93+
parent: Context | None
94+
7695
def __init__(
7796
self,
7897
source: _ContextSourceType = None,
7998
base: str | None = None,
8099
version: float | None = 1.1,
81100
):
82-
self.version: float = version or 1.1
101+
self.version = version or 1.1
83102
self.language = None
84-
self.vocab: str | None = None
85-
self._base: str | None
103+
self.vocab = None
86104
self.base = base
87105
self.doc_base = base
88-
self.terms: dict[str, Any] = {}
106+
self.terms = {}
89107
# _alias maps NODE_KEY to list of aliases
90-
self._alias: dict[str, list[str]] = {}
91-
self._lookup: dict[tuple[str, Any, Defined | str, bool], Term] = {}
92-
self._prefixes: dict[str, Any] = {}
108+
self._alias = {}
109+
self._lookup = {}
110+
self._prefixes = {}
93111
self.active = False
94-
self.parent: Context | None = None
112+
self.parent = None
95113
self.propagate = True
96-
self._context_cache: dict[str, Any] = {}
114+
self._context_cache = {}
97115
if source:
98116
self.load(source)
99117

@@ -668,9 +686,36 @@ def to_dict(self) -> dict[str, Any]:
668686
return r
669687

670688

671-
Term = namedtuple(
672-
"Term",
673-
"id, name, type, container, index, language, reverse, context," "prefix, protected",
674-
)
675-
676-
Term.__new__.__defaults__ = (UNDEF, UNDEF, UNDEF, UNDEF, False, UNDEF, False, False)
689+
class Term(NamedTuple):
690+
"""
691+
Describes how a JSON key should be interpreted when parsed as RDF
692+
"""
693+
#: The IRI or CURIE of the term.
694+
id: str
695+
#: The name of the term, ie an alias for the id.
696+
name: str
697+
#: The type of the term, such as @id, @json, @none or @vocab
698+
type: Defined | str = UNDEF
699+
#: The container type, such as @graph, @id, @index, @language, @list, @set or @type,
700+
container: Collection[Any] | str | Defined = UNDEF
701+
#: A predicate IRI that should be used to interpret keys of this object,
702+
#: when used alongside `@container: @index`.
703+
#: See https://www.w3.org/TR/json-ld11/#property-based-data-indexing
704+
index: str | Defined | None = None
705+
#: The language to be used for values of this term
706+
language: str | Defined | None = UNDEF
707+
#: Indicates that this term is a reverse property, so subject and object are swapped.
708+
#: https://www.w3.org/TR/json-ld11/#reverse-properties
709+
reverse: bool = False
710+
#: A scoped context used inside values that use this term.
711+
#: See https://www.w3.org/TR/json-ld11/#scoped-contexts
712+
context: Any = UNDEF
713+
#: If true, indicates that this should be used during compaction.
714+
#: If false, indicates that this term cannot be used in compaction.
715+
#: See https://www.w3.org/TR/json-ld11/#compact-iris
716+
prefix: bool | None = None
717+
#: If true, marks the term as protected, meaning it cannot be overridden by a subcontext.
718+
#: See https://www.w3.org/TR/json-ld11/#protected-term-definitions
719+
protected: bool = False
720+
721+
Context.terms

0 commit comments

Comments
 (0)