Skip to content

Commit

Permalink
[DO NOT REVIEW] Add a bunch of typing
Browse files Browse the repository at this point in the history
This is a branch I'm maintaing with a bunch of typing, I'm planning to
integrate it with master in parts, anyone is welcome to have a look but
most of what is happening here is subject to change.
  • Loading branch information
aucampia committed Jul 19, 2022
1 parent fda98ec commit 8e745e0
Show file tree
Hide file tree
Showing 50 changed files with 1,195 additions and 541 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ tasks:
vars: { CHECK: true }
- task: black
vars: { CHECK: true }
- task: flake8
# - task: flake8

validate:static:
desc: Perform static validation
Expand Down
1 change: 1 addition & 0 deletions rdflib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"util",
"plugin",
"query",
"_typing",
]

import logging
Expand Down
23 changes: 23 additions & 0 deletions rdflib/_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# import sys
# from typing import TYPE_CHECKING, Optional, Tuple, TypeVar

# if sys.version_info >= (3, 10):
# from typing import TypeAlias
# else:
# from typing_extensions import TypeAlias

# if TYPE_CHECKING:
# from rdflib.graph import Graph
# from rdflib.term import IdentifiedNode, Identifier

# _SubjectType: TypeAlias = "IdentifiedNode"
# _PredicateType: TypeAlias = "IdentifiedNode"
# _ObjectType: TypeAlias = "Identifier"

# _TripleType = Tuple["_SubjectType", "_PredicateType", "_ObjectType"]
# _QuadType = Tuple["_SubjectType", "_PredicateType", "_ObjectType", "Graph"]
# _TriplePatternType = Tuple[
# Optional["_SubjectType"], Optional["_PredicateType"], Optional["_ObjectType"]
# ]

# _GraphT = TypeVar("_GraphT", bound="Graph")
28 changes: 16 additions & 12 deletions rdflib/extras/external_graph_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"""

import logging
from typing import Any, Dict, List

from rdflib.graph import Graph

logger = logging.getLogger(__name__)

Expand All @@ -22,9 +25,9 @@ def _identity(x):


def _rdflib_to_networkx_graph(
graph,
graph: Graph,
nxgraph,
calc_weights,
calc_weights: bool,
edge_attrs,
transform_s=_identity,
transform_o=_identity,
Expand Down Expand Up @@ -70,7 +73,7 @@ def _rdflib_to_networkx_graph(


def rdflib_to_networkx_multidigraph(
graph, edge_attrs=lambda s, p, o: {"key": p}, **kwds
graph: Graph, edge_attrs=lambda s, p, o: {"key": p}, **kwds
):
"""Converts the given graph into a networkx.MultiDiGraph.
Expand Down Expand Up @@ -124,8 +127,8 @@ def rdflib_to_networkx_multidigraph(


def rdflib_to_networkx_digraph(
graph,
calc_weights=True,
graph: Graph,
calc_weights: bool = True,
edge_attrs=lambda s, p, o: {"triples": [(s, p, o)]},
**kwds,
):
Expand Down Expand Up @@ -187,8 +190,8 @@ def rdflib_to_networkx_digraph(


def rdflib_to_networkx_graph(
graph,
calc_weights=True,
graph: Graph,
calc_weights: bool = True,
edge_attrs=lambda s, p, o: {"triples": [(s, p, o)]},
**kwds,
):
Expand Down Expand Up @@ -250,9 +253,9 @@ def rdflib_to_networkx_graph(


def rdflib_to_graphtool(
graph,
v_prop_names=[str("term")],
e_prop_names=[str("term")],
graph: Graph,
v_prop_names: List[str] = [str("term")],
e_prop_names: List[str] = [str("term")],
transform_s=lambda s, p, o: {str("term"): s},
transform_p=lambda s, p, o: {str("term"): p},
transform_o=lambda s, p, o: {str("term"): o},
Expand Down Expand Up @@ -313,7 +316,8 @@ def rdflib_to_graphtool(
True
"""
import graph_tool as gt
# pytype error: Can't find module 'graph_tool'.
import graph_tool as gt # pytype: disable=import-error

g = gt.Graph()

Expand All @@ -323,7 +327,7 @@ def rdflib_to_graphtool(
eprops = [(epn, g.new_edge_property("object")) for epn in e_prop_names]
for epn, eprop in eprops:
g.edge_properties[epn] = eprop
node_to_vertex = {}
node_to_vertex: Dict[Any, Any] = {}
for s, p, o in graph:
sv = node_to_vertex.get(s)
if sv is None:
Expand Down
4 changes: 2 additions & 2 deletions rdflib/extras/infixowl.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@
from rdflib import OWL, RDF, RDFS, XSD, BNode, Literal, Namespace, URIRef, Variable
from rdflib.collection import Collection
from rdflib.graph import Graph
from rdflib.namespace import NamespaceManager
from rdflib.term import Identifier
from rdflib.namespace import OWL, RDF, RDFS, XSD, Namespace, NamespaceManager
from rdflib.term import BNode, Identifier, Literal, URIRef, Variable
from rdflib.util import first

logger = logging.getLogger(__name__)
Expand Down
23 changes: 11 additions & 12 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,7 @@ def triples(
for _s, _o in p.eval(self, s, o):
yield _s, p, _o
else:
# type error: Argument 1 to "triples" of "Store" has incompatible type "Tuple[Optional[Node], Optional[Node], Optional[Node]]"; expected "Tuple[Optional[IdentifiedNode], Optional[IdentifiedNode], Optional[Node]]"
# NOTE on type error: This is because the store typing is too narrow, willbe fixed in subsequent PR.
for (_s, _p, _o), cg in self.__store.triples((s, p, o), context=self): # type: ignore [arg-type]
for (_s, _p, _o), cg in self.__store.triples((s, p, o), context=self):
yield _s, _p, _o

def __getitem__(self, item):
Expand Down Expand Up @@ -1384,18 +1382,21 @@ def query(
query_object,
initNs,
initBindings,
self.default_union and "__UNION__" or self.identifier,
# type error: Argument 4 to "query" of "Store" has incompatible type "Union[Literal['__UNION__'], Node]"; expected "Identifier"
self.default_union and "__UNION__" or self.identifier, # type: ignore[arg-type]
**kwargs,
)
except NotImplementedError:
pass # store has no own implementation

if not isinstance(result, query.Result):
# type error: Subclass of "str" and "Result" cannot exist: would have incompatible method signatures
if not isinstance(result, query.Result): # type: ignore[unreachable]
result = plugin.get(cast(str, result), query.Result)
if not isinstance(processor, query.Processor):
processor = plugin.get(processor, query.Processor)(self)

return result(processor.query(query_object, initBindings, initNs, **kwargs))
# type error: Argument 1 to "Result" has incompatible type "Mapping[str, Any]"; expected "str"
return result(processor.query(query_object, initBindings, initNs, **kwargs)) # type: ignore[arg-type]

def update(
self,
Expand Down Expand Up @@ -1868,12 +1869,9 @@ def quads(

s, p, o, c = self._spoc(triple_or_quad)

# type error: Argument 1 to "triples" of "Store" has incompatible type "Tuple[Optional[Node], Optional[Node], Optional[Node]]"; expected "Tuple[Optional[IdentifiedNode], Optional[IdentifiedNode], Optional[Node]]"
# NOTE on type error: This is because the store typing is too narrow, willbe fixed in subsequent PR.
for (s, p, o), cg in self.store.triples((s, p, o), context=c): # type: ignore[arg-type]
for (s, p, o), cg in self.store.triples((s, p, o), context=c):
for ctx in cg:
# type error: Incompatible types in "yield" (actual type "Tuple[Optional[Node], Optional[Node], Optional[Node], Any]", expected type "Tuple[Node, Node, Node, Optional[Graph]]")
yield s, p, o, ctx # type: ignore[misc]
yield s, p, o, ctx

def triples_choices(self, triple, context=None):
"""Iterate over all the triples in the entire conjunctive graph"""
Expand Down Expand Up @@ -1905,7 +1903,8 @@ def contexts(
# the weirdness - see #225
yield context
else:
yield self.get_context(context)
# type error: Statement is unreachable
yield self.get_context(context) # type: ignore[unreachable]

def get_graph(self, identifier: Union[URIRef, BNode]) -> Union[Graph, None]:
"""Returns the graph identified by given identifier"""
Expand Down
12 changes: 8 additions & 4 deletions rdflib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
if TYPE_CHECKING:
from http.client import HTTPMessage, HTTPResponse

from rdflib import Graph
from rdflib.graph import Graph

__all__ = [
"Parser",
Expand Down Expand Up @@ -79,7 +79,11 @@ def read(self, *args, **kwargs):
def read1(self, *args, **kwargs):
if self.encoded is None:
b = codecs.getencoder(self.encoding)(self.wrapped)
self.encoded = BytesIO(b)
# NOTE on pytype error: Looks like this may be an actual bug.
# pytype error: Function BytesIO.__init__ was called with the wrong arguments
# Expected: (self, initial_bytes: Union[bytearray, bytes, memoryview] = ...)
# Actually passed: (self, initial_bytes: Tuple[bytes, int])
self.encoded = BytesIO(b) # pytype: disable=wrong-arg-types
return self.encoded.read1(*args, **kwargs)

def readinto(self, *args, **kwargs):
Expand Down Expand Up @@ -336,8 +340,8 @@ def create_input_source(
publicID: Optional[str] = None, # noqa: N803
location: Optional[str] = None,
file: Optional[Union[BinaryIO, TextIO]] = None,
data: Union[str, bytes, dict] = None,
format: str = None,
data: Optional[Union[str, bytes, dict]] = None,
format: Optional[str] = None,
) -> InputSource:
"""
Return an appropriate InputSource instance for the given
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def plugins(name: Optional[str] = ..., kind: None = ...) -> Iterator[Plugin]:

def plugins(
name: Optional[str] = None, kind: Optional[Type[PluginT]] = None
) -> Iterator[Plugin]:
) -> Iterator[Plugin[PluginT]]:
"""
A generator of the plugins.
Expand Down
3 changes: 2 additions & 1 deletion rdflib/plugins/parsers/hext.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import warnings
from typing import List, Union

from rdflib import BNode, ConjunctiveGraph, Literal, URIRef
from rdflib.graph import ConjunctiveGraph
from rdflib.parser import Parser
from rdflib.term import BNode, Literal, URIRef

__all__ = ["HextuplesParser"]

Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/parsers/nquads.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from codecs import getreader

from rdflib import ConjunctiveGraph
from rdflib.graph import ConjunctiveGraph

# Build up from the NTriples parser:
from rdflib.plugins.parsers.ntriples import (
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/parsers/trig.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rdflib import ConjunctiveGraph
from rdflib.graph import ConjunctiveGraph
from rdflib.parser import Parser

from .notation3 import RDFSink, SinkParser
Expand Down
3 changes: 1 addition & 2 deletions rdflib/plugins/parsers/trix.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""
A TriX parser for RDFLib
"""
from xml.sax import make_parser
from xml.sax import handler, make_parser
from xml.sax.handler import ErrorHandler
from xml.sax.saxutils import handler

from rdflib.exceptions import ParserError
from rdflib.graph import Graph
Expand Down
17 changes: 13 additions & 4 deletions rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

from collections import defaultdict
from functools import cmp_to_key
from typing import IO, Dict, Optional

from rdflib.exceptions import Error
from rdflib.graph import Graph
from rdflib.namespace import RDF, RDFS
from rdflib.serializer import Serializer
from rdflib.term import BNode, Literal, URIRef
Expand Down Expand Up @@ -184,15 +186,15 @@ class TurtleSerializer(RecursiveSerializer):
short_name = "turtle"
indentString = " "

def __init__(self, store):
self._ns_rewrite = {}
def __init__(self, store: Graph):
self._ns_rewrite: Dict[str, str] = {}
super(TurtleSerializer, self).__init__(store)
self.keywords = {RDF.type: "a"}
self.reset()
self.stream = None
self._spacious = _SPACIOUS_OUTPUT

def addNamespace(self, prefix, namespace):
def addNamespace(self, prefix: str, namespace: str):
# Turtle does not support prefix that start with _
# if they occur in the graph, rewrite to p_blah
# this is more complicated since we need to make sure p_blah
Expand Down Expand Up @@ -223,7 +225,14 @@ def reset(self):
self._started = False
self._ns_rewrite = {}

def serialize(self, stream, base=None, encoding=None, spacious=None, **args):
def serialize(
self,
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = None,
spacious: Optional[bool] = None,
**args,
):
self.reset()
self.stream = stream
# if base is given here, use that, if not and a base is set for the graph use that
Expand Down
Loading

0 comments on commit 8e745e0

Please sign in to comment.