Skip to content

Commit fa00b7d

Browse files
committed
Convert Term to a typed NamedTuple; expose types of Context fields
1 parent 4d9935c commit fa00b7d

File tree

1 file changed

+69
-17
lines changed

1 file changed

+69
-17
lines changed

rdflib/plugins/shared/jsonld/context.py

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Set,
2121
Tuple,
2222
Union,
23+
NamedTuple,
2324
)
2425
from urllib.parse import urljoin, urlsplit
2526

@@ -74,27 +75,47 @@ class Defined(int):
7475

7576

7677
class Context:
78+
"""
79+
A JSON-LD context, which contains term definitions
80+
"""
81+
82+
_base: str | None
83+
#: _alias maps NODE_KEY to list of aliases
84+
_alias: dict[str, list[str]]
85+
_lookup: dict[tuple[str, Any, Defined | str, bool], Term]
86+
_prefixes: dict[str, Any]
87+
_context_cache: dict[str, Any]
88+
89+
version: float
90+
language: str | None
91+
doc_base: str | None
92+
vocab: str | None
93+
active: bool
94+
propagate: bool
95+
terms: dict[str, Any]
96+
parent: Context | None
97+
7798
def __init__(
7899
self,
79100
source: _ContextSourceType = None,
80101
base: Optional[str] = None,
81102
version: Optional[float] = 1.1,
82103
):
83-
self.version: float = version or 1.1
104+
self._alias = {}
105+
self._lookup = {}
106+
self._prefixes = {}
107+
self._context_cache = {}
108+
109+
self.version = version or 1.1
84110
self.language = None
85-
self.vocab: Optional[str] = None
86-
self._base: Optional[str]
87-
self.base = base
88111
self.doc_base = base
89-
self.terms: Dict[str, Any] = {}
90-
# _alias maps NODE_KEY to list of aliases
91-
self._alias: Dict[str, List[str]] = {}
92-
self._lookup: Dict[Tuple[str, Any, Union[Defined, str], bool], Term] = {}
93-
self._prefixes: Dict[str, Any] = {}
94112
self.active = False
95-
self.parent: Optional[Context] = None
96113
self.propagate = True
97-
self._context_cache: Dict[str, Any] = {}
114+
self.vocab = None
115+
self.base = base
116+
self.terms = {}
117+
self.parent = None
118+
98119
if source:
99120
self.load(source)
100121

@@ -668,9 +689,40 @@ def to_dict(self) -> Dict[str, Any]:
668689
return r
669690

670691

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)
692+
class Term(NamedTuple):
693+
"""
694+
Describes how a JSON key should be interpreted when parsed as RDF
695+
"""
696+
697+
#: The IRI or CURIE of the term.
698+
id: str
699+
#: The name of the term, ie an alias for the id.
700+
name: str
701+
#: The type of the term, such as @id, @json, @none or @vocab
702+
type: Defined | str = UNDEF
703+
#: The container type, such as @graph, @id, @index, @language, @list, @set or @type,
704+
container: Collection[Any] | str | Defined = UNDEF
705+
#: A predicate IRI that should be used to interpret keys of this object,
706+
#: when used alongside `@container: @index`.
707+
#: See https://www.w3.org/TR/json-ld11/#property-based-data-indexing
708+
#: Ideally this wouldn't be called 'index' as it overrides the tuple's builtin index() method
709+
#: Hence the pyright ignore comment
710+
index: str | Defined | None = None # pyright: ignore[reportIncompatibleMethodOverride]
711+
#: The language to be used for values of this term
712+
language: str | Defined | None = UNDEF
713+
#: Indicates that this term is a reverse property, so subject and object are swapped.
714+
#: https://www.w3.org/TR/json-ld11/#reverse-properties
715+
reverse: bool = False
716+
#: A scoped context used inside values that use this term.
717+
#: See https://www.w3.org/TR/json-ld11/#scoped-contexts
718+
context: Any = UNDEF
719+
#: If true, indicates that this should be used during compaction.
720+
#: If false, indicates that this term cannot be used in compaction.
721+
#: See https://www.w3.org/TR/json-ld11/#compact-iris
722+
prefix: bool | None = None
723+
#: If true, marks the term as protected, meaning it cannot be overridden by a subcontext.
724+
#: See https://www.w3.org/TR/json-ld11/#protected-term-definitions
725+
protected: bool = False
726+
727+
728+
Context.terms

0 commit comments

Comments
 (0)