Skip to content

Instantly share code, notes, and snippets.

@aucampia
Last active August 21, 2022 19:13
Show Gist options
  • Save aucampia/c6ae2dcd1711c047590817a20c637ddd to your computer and use it in GitHub Desktop.
Save aucampia/c6ae2dcd1711c047590817a20c637ddd to your computer and use it in GitHub Desktop.
task run -- python -m pip install --upgrade strip-hints black python-minifier
PYLOGGING_LEVEL=INFO task run -- git difftool -y -x $(readlink -f devtools/diffrtpy.py) origin/iwana-20220814T2117-graph_typing..upstream/master | tee /var/tmp/compact.diff
gh gist edit  https://gist.github.com/aucampia/c6ae2dcd1711c047590817a20c637ddd -f compact.diff /var/tmp/compact.diff
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -164,14 +164,12 @@
<!-- CHANGE BARRIER: START PR #2057 -->
<!-- -->
<!-- -->
- Added type hints.
+ [PR #2057](https://github.com/RDFLib/rdflib/pull/2057).
- `rdflib.store` and builtin stores have mostly complete type hints.
- [PR #2057](https://github.com/RDFLib/rdflib/pull/2057).
- - `rdflib.graph` have mostly complete type hints.
- [PR #2080](https://github.com/RDFLib/rdflib/pull/2080).
<!-- -->
<!-- -->
<!-- CHANGE BARRIER: END PR #2057 -->
<!-- -->
--- a/devtools/diffrtpy.py
+++ b/devtools/diffrtpy.py
@@ -10,20 +10,12 @@
import black
import python_minifier
from strip_hints import strip_string_to_string
-def clean_python(input):
- code = input.read_text()
- try:
- code = strip_string_to_string(code, to_empty=True, strip_nl=True)
- except Exception:
- logging.warning(
- "failed to strip type hints from %s, falling back to using with type hints",
- input,
- )
- code = code
+def clean_python(code):
+ code = strip_string_to_string(code, to_empty=True, strip_nl=True)
code = python_minifier.minify(
code,
remove_annotations=True,
remove_pass=False,
remove_literal_statements=True,
@@ -83,16 +75,15 @@
lhs_file = Path(parse_result.lhs_file[0])
rhs_file = Path(parse_result.rhs_file[0])
logging.debug(
"base = %s, lhs_file = %s, rhs_file = %s", base, lhs_file, rhs_file
)
+ lhs_file_content = lhs_file.read_text()
+ rhs_file_content = rhs_file.read_text()
if lhs_file.name.endswith(".py") and rhs_file.name.endswith(".py"):
- lhs_file_content = clean_python(lhs_file)
- rhs_file_content = clean_python(rhs_file)
- else:
- lhs_file_content = lhs_file.read_text()
- rhs_file_content = rhs_file.read_text()
+ lhs_file_content = clean_python(lhs_file_content)
+ rhs_file_content = clean_python(rhs_file_content)
lhs_file_lines = lhs_file_content.splitlines(keepends=True)
rhs_file_lines = rhs_file_content.splitlines(keepends=True)
sys.stdout.writelines(
unified_diff(lhs_file_lines, rhs_file_lines, f"a/{base}", f"b/{base}", n=5)
)
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -60,11 +60,10 @@
("py:class", "typing.IO[bytes]"),
("py:class", "http.client.HTTPMessage"),
("py:class", "importlib.metadata.EntryPoint"),
("py:class", "xml.dom.minidom.Document"),
("py:class", "xml.dom.minidom.DocumentFragment"),
- ("py:class", "isodate.duration.Duration"),
("py:class", "rdflib.plugin.PluginT"),
("py:class", "Identifier"),
("py:class", "Diagnostics"),
("py:class", "ParseAction"),
("py:class", "ParseFailAction"),
@@ -73,27 +72,17 @@
("py:class", "db.DBEnv"),
]
if sys.version_info < (3, 9):
nitpick_ignore.extend(
[
- ("py:class", "_ContextIdentifierType"),
- ("py:class", "_ContextType"),
- ("py:class", "_GraphT"),
- ("py:class", "_NamespaceSetString"),
+ ("py:class", "_TriplePatternType"),
+ ("py:class", "_TripleType"),
("py:class", "_ObjectType"),
("py:class", "_PredicateType"),
- ("py:class", "_QuadSelectorType"),
("py:class", "_SubjectType"),
- ("py:class", "_TripleOrPathTripleType"),
- ("py:class", "_TripleOrQuadPathPatternType"),
- ("py:class", "_TripleOrQuadPatternType"),
- ("py:class", "_TriplePathPatternType"),
- ("py:class", "_TriplePathType"),
- ("py:class", "_TriplePatternType"),
- ("py:class", "_TripleSelectorType"),
- ("py:class", "_TripleType"),
- ("py:class", "_TripleOrTriplePathType"),
+ ("py:class", "_ContextType"),
+ ("py:class", "_ContextIdentifierType"),
("py:class", "TextIO"),
]
)
if sys.version_info < (3, 8):
nitpick_ignore.extend([("py:class", "importlib_metadata.EntryPoint")])
--- a/rdflib/graph.py
+++ b/rdflib/graph.py
@@ -1,24 +1,17 @@
-from __future__ import annotations
import logging
import pathlib
import random
from io import BytesIO
from typing import (
IO,
TYPE_CHECKING,
Any,
BinaryIO,
- Callable,
- Dict,
Generator,
Iterable,
- List,
- Mapping,
- NoReturn,
Optional,
- Set,
TextIO,
Tuple,
Type,
TypeVar,
Union,
@@ -38,71 +31,34 @@
from rdflib.parser import InputSource, Parser, create_input_source
from rdflib.paths import Path
from rdflib.resource import Resource
from rdflib.serializer import Serializer
from rdflib.store import Store
-from rdflib.term import (
- BNode,
- Genid,
- IdentifiedNode,
- Identifier,
- Literal,
- Node,
- RDFLibGenid,
- URIRef,
- Variable,
-)
-
-if TYPE_CHECKING:
- import typing_extensions as te
- import rdflib.query
- from rdflib.plugins.sparql.sparql import Query, Update
+from rdflib.term import BNode, Genid, IdentifiedNode, Literal, Node, RDFLibGenid, URIRef
+
_SubjectType = Node
_PredicateType = Node
_ObjectType = Node
_ContextIdentifierType = IdentifiedNode
_TripleType = Tuple["_SubjectType", "_PredicateType", "_ObjectType"]
_QuadType = Tuple["_SubjectType", "_PredicateType", "_ObjectType", "_ContextType"]
_OptionalQuadType = Tuple[
"_SubjectType", "_PredicateType", "_ObjectType", Optional["_ContextType"]
]
-_TripleOrOptionalQuadType = Union["_TripleType", "_OptionalQuadType"]
_OptionalIdentifiedQuadType = Tuple[
"_SubjectType", "_PredicateType", "_ObjectType", Optional["_ContextIdentifierType"]
]
_TriplePatternType = Tuple[
Optional["_SubjectType"], Optional["_PredicateType"], Optional["_ObjectType"]
]
-_TriplePathPatternType = Tuple[Optional["_SubjectType"], Path, Optional["_ObjectType"]]
_QuadPatternType = Tuple[
Optional["_SubjectType"],
Optional["_PredicateType"],
Optional["_ObjectType"],
- Optional["_ContextType"],
+ Optional["Graph"],
]
-_QuadPathPatternType = Tuple[
- Optional["_SubjectType"], Path, Optional["_ObjectType"], Optional["_ContextType"]
-]
-_TripleOrQuadPatternType = Union["_TriplePatternType", "_QuadPatternType"]
-_TripleOrQuadPathPatternType = Union["_TriplePathPatternType", "_QuadPathPatternType"]
-_TripleSelectorType = Tuple[
- Optional["_SubjectType"],
- Optional[Union["Path", "_PredicateType"]],
- Optional["_ObjectType"],
-]
-_QuadSelectorType = Tuple[
- Optional["_SubjectType"],
- Optional[Union["Path", "_PredicateType"]],
- Optional["_ObjectType"],
- Optional["_ContextType"],
-]
-_TripleOrQuadSelectorType = Union["_TripleSelectorType", "_QuadSelectorType"]
-_TriplePathType = Tuple["_SubjectType", Path, "_ObjectType"]
-_TripleOrTriplePathType = Union["_TripleType", "_TriplePathType"]
_GraphT = TypeVar("_GraphT", bound="Graph")
-_ConjunctiveGraphT = TypeVar("_ConjunctiveGraphT", bound="ConjunctiveGraph")
-_DatasetT = TypeVar("_DatasetT", bound="Dataset")
assert Literal
assert Namespace
if TYPE_CHECKING:
from rdflib._type_checking import _NamespaceSetString
logger = logging.getLogger(__name__)
@@ -115,35 +71,20 @@
"ModificationException",
"Dataset",
"UnSupportedAggregateOperation",
"ReadOnlyGraphAggregate",
"BatchAddGraph",
- "_ConjunctiveGraphT",
- "_ContextIdentifierType",
- "_DatasetT",
- "_GraphT",
+ "_TriplePatternType",
+ "_TripleType",
+ "_SubjectType",
"_ObjectType",
+ "_PredicateType",
+ "_QuadPatternType",
"_OptionalIdentifiedQuadType",
"_OptionalQuadType",
- "_PredicateType",
- "_QuadPathPatternType",
- "_QuadPatternType",
- "_QuadSelectorType",
"_QuadType",
- "_SubjectType",
- "_TripleOrOptionalQuadType",
- "_TripleOrTriplePathType",
- "_TripleOrQuadPathPatternType",
- "_TripleOrQuadPatternType",
- "_TripleOrQuadSelectorType",
- "_TriplePathPatternType",
- "_TriplePathType",
- "_TriplePatternType",
- "_TripleSelectorType",
- "_TripleType",
]
-_TCArgT = TypeVar("_TCArgT")
class Graph(Node):
def __init__(
self,
@@ -504,26 +445,10 @@
subject, predicate, object_ = triple
for ((s, p, o), cg) in self.store.triples_choices(
(subject, predicate, object_), context=self
):
yield (s, p, o)
-
- @overload
- def value(self, subject=..., predicate=..., object=..., default=..., any=...):
- ...
-
- @overload
- def value(self, subject=..., predicate=..., object=..., default=..., any=...):
- ...
-
- @overload
- def value(self, subject=..., predicate=..., object=..., default=..., any=...):
- ...
-
- @overload
- def value(self, subject=..., predicate=..., object=..., default=..., any=...):
- ...
def value(
self, subject=None, predicate=RDF.value, object=None, default=None, any=True
):
retval = default
@@ -852,16 +777,12 @@
def skolemize(self, new_graph=None, bnode=None, authority=None, basepath=None):
def do_skolemize(bnode, t):
s, p, o = t
if s == bnode:
- if TYPE_CHECKING:
- assert isinstance(s, BNode)
s = s.skolemize(authority=authority, basepath=basepath)
if o == bnode:
- if TYPE_CHECKING:
- assert isinstance(o, BNode)
o = o.skolemize(authority=authority, basepath=basepath)
return s, p, o
def do_skolemize2(t):
s, p, o = t
@@ -880,16 +801,12 @@
def de_skolemize(self, new_graph=None, uriref=None):
def do_de_skolemize(uriref, t):
s, p, o = t
if s == uriref:
- if TYPE_CHECKING:
- assert isinstance(s, URIRef)
s = s.de_skolemize()
if o == uriref:
- if TYPE_CHECKING:
- assert isinstance(o, URIRef)
o = o.de_skolemize()
return s, p, o
def do_de_skolemize2(t):
s, p, o = t
@@ -951,26 +868,10 @@
@overload
def _spoc(self, triple_or_quad, default=False):
...
- @overload
- def _spoc(self, triple_or_quad, default=False):
- ...
-
- @overload
- def _spoc(self, triple_or_quad, default=False):
- ...
-
- @overload
- def _spoc(self, triple_or_quad, default=False):
- ...
-
- @overload
- def _spoc(self, triple_or_quad, default=False):
- ...
-
def _spoc(self, triple_or_quad, default=False):
if triple_or_quad is None:
return None, None, None, self.default_context if default else None
if len(triple_or_quad) == 3:
c = self.default_context if default else None
@@ -1020,22 +921,10 @@
def remove(self, triple_or_quad):
s, p, o, c = self._spoc(triple_or_quad)
self.store.remove((s, p, o), context=c)
return self
-
- @overload
- def triples(self, triple_or_quad, context=...):
- ...
-
- @overload
- def triples(self, triple_or_quad, context=...):
- ...
-
- @overload
- def triples(self, triple_or_quad, context=...):
- ...
def triples(self, triple_or_quad, context=None):
s, p, o, c = self._spoc(triple_or_quad)
context = self._graph(context or c)
if self.default_union:
@@ -1342,22 +1231,10 @@
raise ModificationException()
def remove(self, triple):
raise ModificationException()
- @overload
- def triples(self, triple):
- ...
-
- @overload
- def triples(self, triple):
- ...
-
- @overload
- def triples(self, triple):
- ...
-
def triples(self, triple):
s, p, o = triple
for graph in self.graphs:
if isinstance(p, Path):
for (s, o) in p.eval(self, s, o):
@@ -1453,20 +1330,10 @@
def __reduce__(self):
raise UnSupportedAggregateOperation()
-@overload
-def _assertnode(*terms):
- ...
-
-
-@overload
-def _assertnode(*terms):
- ...
-
-
def _assertnode(*terms):
for t in terms:
assert isinstance(t, Node), "Term %s must be an rdflib term" % (t,)
return True
--- a/rdflib/plugins/stores/memory.py
+++ b/rdflib/plugins/stores/memory.py
@@ -3,11 +3,10 @@
Any,
Collection,
Dict,
Generator,
Iterator,
- Mapping,
Optional,
Set,
Tuple,
Union,
overload,
--- a/rdflib/plugins/stores/sparqlstore.py
+++ b/rdflib/plugins/stores/sparqlstore.py
@@ -7,11 +7,10 @@
Dict,
Generator,
Iterable,
Iterator,
List,
- Mapping,
Optional,
Tuple,
Union,
overload,
)
--- a/rdflib/store.py
+++ b/rdflib/store.py
@@ -1,6 +1,5 @@
-from __future__ import annotations
import pickle
from io import BytesIO
from typing import (
TYPE_CHECKING,
Any,
@@ -11,10 +10,11 @@
List,
Mapping,
Optional,
Tuple,
Union,
+ overload,
)
from rdflib.events import Dispatcher, Event
if TYPE_CHECKING:
from rdflib.graph import (
@@ -159,10 +159,22 @@
)
self.add((s, p, o), c)
def remove(self, triple, context=None):
self.dispatcher.dispatch(TripleRemovedEvent(triple=triple, context=context))
+
+ @overload
+ def triples_choices(self, triple, context=None):
+ ...
+
+ @overload
+ def triples_choices(self, triple, context=None):
+ ...
+
+ @overload
+ def triples_choices(self, triple, context=None):
+ ...
def triples_choices(self, triple, context=None):
subject, predicate, object_ = triple
if isinstance(object_, list):
assert not isinstance(subject, list), "object_ / subject are both lists"
--- a/test/utils/dawg_manifest.py
+++ b/test/utils/dawg_manifest.py
@@ -115,22 +115,20 @@
yield from cls.from_graph(uri_mapper, graph, report_prefix)
def included(self):
for includes in self.graph.objects(self.identifier, MF.include):
for include in self.graph.items(includes):
- assert isinstance(include, str)
include_local_path = self.uri_mapper.to_local_path(include)
yield from Manifest.from_sources(
self.uri_mapper,
include_local_path,
report_prefix=self.report_prefix,
)
def entires(self, entry_type, exclude=None, include=None):
for entries in self.graph.objects(self.identifier, MF.entries):
for entry_iri in self.graph.items(entries):
- assert isinstance(entry_iri, URIRef)
entry = entry_type(self, entry_iri)
if exclude is not None and entry.check_filters(exclude):
continue
if include is not None and not entry.check_filters(include):
continue
--- a/test/utils/manifest.py
+++ b/test/utils/manifest.py
@@ -1,17 +1,14 @@
-from __future__ import annotations
import logging
from test.utils.namespace import DAWGT, MF, QT, RDFT, UT
from typing import Iterable, List, NamedTuple, Optional, Tuple, Union, cast
from rdflib import RDF, RDFS, Graph
from rdflib.term import Identifier, Node, URIRef
logger = logging.getLogger(__name__)
-ResultType = Union[
- Identifier, Tuple[Optional[Node], List[Tuple[Optional[Node], Optional[Node]]]]
-]
-GraphDataType = Union[List[Optional[Node]], List[Tuple[Optional[Node], Optional[Node]]]]
+ResultType = Union[Identifier, Tuple[Identifier, List[Tuple[Identifier, Identifier]]]]
+GraphDataType = Union[List[Identifier], List[Tuple[Identifier, Identifier]]]
class RDFTest(NamedTuple):
uri: URIRef
name: str
@@ -62,18 +59,18 @@
if _type in (MF.QueryEvaluationTest, MF.CSVResultFormatTest):
a = g.value(e, MF.action)
query = g.value(a, QT.query)
data = g.value(a, QT.data)
graphdata = list(
- cast(Iterable[Optional[Node]], g.objects(a, QT.graphData))
+ cast(Iterable[Identifier], g.objects(a, QT.graphData))
)
- res = cast(Optional[ResultType], g.value(e, MF.result))
+ res = g.value(e, MF.result)
elif _type in (MF.UpdateEvaluationTest, UT.UpdateEvaluationTest):
a = g.value(e, MF.action)
query = g.value(a, UT.request)
data = g.value(a, UT.data)
- graphdata = cast(List[Tuple[Optional[Node], Optional[Node]]], [])
+ graphdata = cast(List[Tuple[Identifier, Identifier]], [])
for gd in g.objects(a, UT.graphData):
graphdata.append(
(g.value(gd, UT.graph), g.value(gd, RDFS.label))
)
r = g.value(e, MF.result)
@@ -124,22 +121,21 @@
RDFT.TestTrigEval,
RDFT.TestTrigNegativeEval,
RDFT.TestTrixEval,
):
query = g.value(e, MF.action)
- res = cast(Identifier, g.value(e, MF.result))
+ res = g.value(e, MF.result)
syntax = _type in (
RDFT.TestTurtleEval,
RDFT.TestTrigEval,
RDFT.TestTrixEval,
)
else:
logger.debug(f"Don't know {_type}")
pass
print("I dont know DAWG Test Type %s" % _type)
continue
- assert isinstance(e, URIRef)
yield (
e,
_type,
RDFTest(
e,
--- a/test/utils/sparql_checker.py
+++ b/test/utils/sparql_checker.py
@@ -8,22 +8,11 @@
from test.utils import BNodeHandling, GraphHelper
from test.utils.dawg_manifest import Manifest, ManifestEntry
from test.utils.iri import URIMapper
from test.utils.namespace import MF, QT, UT
from test.utils.result import ResultType, assert_bindings_collections_equal
-from typing import (
- Any,
- Callable,
- Dict,
- Generator,
- Optional,
- Set,
- Tuple,
- Type,
- Union,
- cast,
-)
+from typing import Any, Callable, Dict, Generator, Optional, Set, Tuple, Type, Union
from urllib.parse import urljoin
import pytest
from pytest import MonkeyPatch
import rdflib
from rdflib.graph import Dataset, Graph
@@ -140,37 +129,27 @@
self.query = self.action
assert isinstance(self.query, URIRef)
return
if self.type_info.query_type is not None:
assert self.result is not None
- self.query = cast(
- Optional[IdentifiedNode],
- self.graph.value(self.action, self.type_info.query_property),
- )
+ self.query = self.graph.value(self.action, self.type_info.query_property)
assert isinstance(self.query, URIRef)
assert self.type_info.ns is not None
- self.action_data = cast(
- Optional[IdentifiedNode],
- self.graph.value(self.action, self.type_info.ns.data),
- )
- self.expected_outcome = cast(
- Optional[URIRef],
- self.graph.value(self.action, self.type_info.expected_outcome_property),
+ self.action_data = self.graph.value(self.action, self.type_info.ns.data)
+ self.expected_outcome = self.graph.value(
+ self.action, self.type_info.expected_outcome_property
)
for action_graph_data_id in self.graph.objects(
self.action, self.type_info.ns.graphData
):
assert isinstance(action_graph_data_id, IdentifiedNode)
graph_data = GraphData.from_graph(self.graph, action_graph_data_id)
if self.action_graph_data is None:
self.action_graph_data = set()
self.action_graph_data.add(graph_data)
if isinstance(self.result, BNode):
- self.result_data = cast(
- Optional[IdentifiedNode],
- self.graph.value(self.result, self.type_info.ns.data),
- )
+ self.result_data = self.graph.value(self.result, self.type_info.ns.data)
else:
self.result_data = self.result
assert isinstance(self.result_data, URIRef)
for result_graph_data_id in self.graph.objects(
self.result, self.type_info.ns.graphData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment