-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retain literal types in collection backend #165
Changes from 10 commits
b06c4dc
fd5ce36
c168252
4c3d1d6
9a36d5d
5ebeed0
22a686c
99823a6
003b072
3f20be1
4f532a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,33 +294,35 @@ def parse( | |
self, | ||
source=None, | ||
format=None, | ||
backend=None, | ||
backend_kwargs=None, | ||
fallback_backend="rdflib", | ||
fallback_backend_kwargs=None, | ||
Comment on lines
+297
to
+298
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we didn't need to change backend/backend_kwargs to fallback_backend/fallback_backend_kwargs in the tests, it seems that we do not have adeguate tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, the collection backend does not provide a parse() method, so it will use the fallback backend. That is tested when calling ts.parse() in tests/backends/test_collection.py. |
||
**kwargs, # pylint: disable=redefined-builtin | ||
) -> None: | ||
"""Parse source and add the resulting triples to triplestore. | ||
|
||
Parameters: | ||
source: File-like object or file name. | ||
format: Needed if format can not be inferred from source. | ||
backend: If given, use the parse() method of the specified | ||
backend instead of the current backend. | ||
backend_kwargs: Dict with additional keyword arguments passed | ||
when instantiating `backend`. | ||
fallback_backend: If the current backend doesn't implement | ||
parse, use the `fallback_backend` instead. | ||
fallback_backend_kwargs: Dict with additional keyword arguments | ||
for initialising `fallback_backend`. | ||
Comment on lines
+306
to
+309
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not see where these are tested. According to codecov, they are, so currently it should be OK. But it is important that this is tested on purpose and not "by accident", e.g. by using a backend that right now does not contain parse but might in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are tested in tests/backends/test_collection.py There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I prefer tests to be a bit more explicit. |
||
kwargs: Keyword arguments passed to the backend. | ||
The rdflib backend supports e.g. `location` (absolute | ||
or relative URL) and `data` (string containing the | ||
data to be parsed) arguments. | ||
""" | ||
if backend and backend != self.backend_name: | ||
if backend_kwargs is None: | ||
backend_kwargs = {} | ||
ts = Triplestore(backend=backend, **backend_kwargs) | ||
ts.parse(source=source, format=format, **kwargs) | ||
self.add_triples(ts.triples()) | ||
else: | ||
if hasattr(self.backend, "parse"): | ||
self._check_method("parse") | ||
self.backend.parse(source=source, format=format, **kwargs) | ||
else: | ||
if fallback_backend_kwargs is None: | ||
fallback_backend_kwargs = {} | ||
ts = Triplestore( | ||
backend=fallback_backend, **fallback_backend_kwargs | ||
) | ||
ts.parse(source=source, format=format, **kwargs) | ||
self.add_triples(ts.triples()) | ||
|
||
if hasattr(self.backend, "namespaces"): | ||
for prefix, namespace in self.backend.namespaces().items(): | ||
|
@@ -331,8 +333,8 @@ def serialize( | |
self, | ||
destination=None, | ||
format="turtle", # pylint: disable=redefined-builtin | ||
backend=None, | ||
backend_kwargs=None, | ||
fallback_backend="rdflib", | ||
fallback_backend_kwargs=None, | ||
**kwargs, | ||
) -> "Union[None, str]": | ||
"""Serialise triplestore. | ||
|
@@ -342,28 +344,28 @@ def serialize( | |
serialisation is returned. | ||
format: Format to serialise as. Supported formats, depends on | ||
the backend. | ||
backend: If given, use the parse() method of the specified | ||
backend instead of the current backend. | ||
backend_kwargs: Dict with additional keyword arguments passed | ||
when instantiating `backend`. | ||
fallback_backend: If the current backend doesn't implement | ||
serialisation, use the `fallback_backend` instead. | ||
fallback_backend_kwargs: Dict with additional keyword arguments | ||
for initialising `fallback_backend`. | ||
Comment on lines
+347
to
+350
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as for parse above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested in tests/backends/test_collection.py |
||
kwargs: Passed to the backend serialize() method. | ||
|
||
Returns: | ||
Serialized string if `destination` is None. | ||
""" | ||
if backend and backend != self.backend_name: | ||
if backend_kwargs is None: | ||
backend_kwargs = {} | ||
ts = Triplestore(backend=backend, **backend_kwargs) | ||
ts.add_triples(self.triples()) | ||
return ts.serialize( | ||
if hasattr(self.backend, "parse"): | ||
self._check_method("serialize") | ||
return self.backend.serialize( | ||
destination=destination, format=format, **kwargs | ||
) | ||
|
||
self._check_method("serialize") | ||
return self.backend.serialize( | ||
destination=destination, format=format, **kwargs | ||
) | ||
if fallback_backend_kwargs is None: | ||
fallback_backend_kwargs = {} | ||
ts = Triplestore(backend=fallback_backend, **fallback_backend_kwargs) | ||
ts.add_triples(self.triples()) | ||
for prefix, iri in self.namespaces.items(): | ||
ts.bind(prefix, iri) | ||
return ts.serialize(destination=destination, format=format, **kwargs) | ||
|
||
def query(self, query_object, **kwargs) -> "List[Tuple[str, ...]]": | ||
"""SPARQL query. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have thested locally and with a draft pull request, I do not get any errors with mypy. Do you have an updated version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem came when I updated mypy. I got annoyed with it and changed the return type of utils.parse_object() from
Union[str, Literal]
toUnion[str, Any]
to get rid of these errors...The source of the problem is that mypy suddenly has got the idea that
Literal
has got a special meaning, since it was added totyping
.It even doesn't understand if I change the return type of utils.parse_object() to
Union[str, tripper.Literal]
.Using
Any
in the return works around the problem. I have moved and updated the comment about buggy mypy to parse_object() and removed the# type: ignore
as you suggested.