Skip to content

Commit

Permalink
Merge pull request #1634 from aucampia/iwana-20211028T2358-jsonld_types
Browse files Browse the repository at this point in the history
Add some type annotations to JSON-LD code
  • Loading branch information
nicholascar committed Jan 2, 2022
2 parents f7c6146 + 3bfd4b8 commit 4b32c15
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
3 changes: 2 additions & 1 deletion rdflib/plugins/parsers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# NOTE: This code reads the entire JSON object into memory before parsing, but
# we should consider streaming the input to deal with arbitrarily large graphs.

from typing import Optional
import warnings
from rdflib.graph import ConjunctiveGraph
from rdflib.parser import URLInputSource
Expand Down Expand Up @@ -130,7 +131,7 @@ def to_rdf(
dataset,
base=None,
context_data=None,
version=None,
version: Optional[float] = None,
generalized_rdf=False,
allow_lists_of_lists=None,
):
Expand Down
64 changes: 46 additions & 18 deletions rdflib/plugins/shared/jsonld/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# https://github.com/RDFLib/rdflib-jsonld/blob/feature/json-ld-1.1/rdflib_jsonld/context.py

from collections import namedtuple
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Union

from rdflib.namespace import RDF

from .keys import (
Expand Down Expand Up @@ -56,30 +58,36 @@ class Defined(int):


class Context(object):
def __init__(self, source=None, base=None, version=None):
def __init__(
self,
source: Optional[Any] = None,
base: Optional[str] = None,
version: Optional[float] = None,
):
self.version = version or 1.0
self.language = None
self.vocab = None
self._base: Optional[str]
self.base = base
self.doc_base = base
self.terms = {}
self.terms: Dict[str, Any] = {}
# _alias maps NODE_KEY to list of aliases
self._alias = {}
self._lookup = {}
self._prefixes = {}
self._alias: Dict[str, List[str]] = {}
self._lookup: Dict[Tuple[str, Any, Union[Defined, str], bool], Any] = {}
self._prefixes: Dict[str, Any] = {}
self.active = False
self.parent = None
self.propagate = True
self._context_cache = {}
self._context_cache: Dict[str, Any] = {}
if source:
self.load(source)

@property
def base(self):
def base(self) -> Optional[str]:
return self._base

@base.setter
def base(self, base):
def base(self, base: Optional[str]):
if base:
hash_index = base.find("#")
if hash_index > -1:
Expand Down Expand Up @@ -177,11 +185,11 @@ def _get(self, obj, key):
return obj.get(alias)
return obj.get(key)

def get_key(self, key):
def get_key(self, key: str):
for alias in self.get_keys(key):
return alias

def get_keys(self, key):
def get_keys(self, key: str):
if key in self._alias:
for alias in self._alias[key]:
yield alias
Expand All @@ -197,9 +205,9 @@ def get_keys(self, key):

def add_term(
self,
name,
idref,
coercion=UNDEF,
name: str,
idref: str,
coercion: Union[Defined, str] = UNDEF,
container=UNDEF,
index=None,
language=UNDEF,
Expand Down Expand Up @@ -239,6 +247,7 @@ def add_term(

self.terms[name] = term

container_key: Union[Defined, str]
for container_key in (LIST, LANG, SET): # , INDEX, ID, GRAPH):
if container_key in container:
break
Expand All @@ -251,7 +260,12 @@ def add_term(
self._prefixes[idref] = name

def find_term(
self, idref, coercion=None, container=UNDEF, language=None, reverse=False
self,
idref: str,
coercion=None,
container: Union[Defined, str] = UNDEF,
language: Optional[str] = None,
reverse: bool = False,
):
lu = self._lookup

Expand Down Expand Up @@ -349,9 +363,14 @@ def to_symbol(self, iri):
return ":".join((pfx, name))
return iri

def load(self, source, base=None, referenced_contexts=None):
def load(
self,
source: Optional[Union[List[Any], Any]],
base: Optional[str] = None,
referenced_contexts: Set[Any] = None,
):
self.active = True
sources = []
sources: List[Any] = []
source = source if isinstance(source, list) else [source]
referenced_contexts = referenced_contexts or set()
self._prep_sources(base, source, sources, referenced_contexts)
Expand All @@ -361,7 +380,7 @@ def load(self, source, base=None, referenced_contexts=None):
else:
self._read_source(source, source_url, referenced_contexts)

def _accept_term(self, key):
def _accept_term(self, key: str) -> bool:
if self.version < 1.1:
return True
if key and len(key) > 1 and key[0] == "@" and key[1].isalnum():
Expand All @@ -370,7 +389,12 @@ def _accept_term(self, key):
return True

def _prep_sources(
self, base, inputs, sources, referenced_contexts, in_source_url=None
self,
base: Optional[str],
inputs: List[Any],
sources,
referenced_contexts,
in_source_url=None,
):

for source in inputs:
Expand All @@ -385,6 +409,10 @@ def _prep_sources(
continue
else:
if base:
if TYPE_CHECKING:
# if base is not None, then source_doc_base won't be
# none due to how it is assigned.
assert source_doc_base is not None
base = urljoin(source_doc_base, source_url)
source = new_ctx

Expand Down

0 comments on commit 4b32c15

Please sign in to comment.