-
Notifications
You must be signed in to change notification settings - Fork 561
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
Bind prefixes choices #1686
Bind prefixes choices #1686
Conversation
rdflib/namespace/__init__.py
Outdated
else: # bind_namespaces is None | ||
pass # bind nothing |
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.
May be a bit better to rather distinguish between explicit None
and invalid/unknown string so that sending the wrong string results in a failure, will make a PR against your branch tomorrow to show you what I mean.
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 think I get it. I haven't finished all the implementation here of course, so was going to add checks for None
and other strings too but just haven't got to it so far.
Feel free to put in a PR or commits to the branch!
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.
PR here: #1693
Co-authored-by: Iwan Aucamp <aucampia@gmail.com>
Noticed this dependency in the PR: |
Co-authored-by: Iwan Aucamp <aucampia@gmail.com>
I agree with @wmelder's comments - I don't think having prefix.cc lookup should be core functionality. It would be really awesome if we could get this PR merged as currently these additional prefixes are causing a lot of problems for us. |
I'm held up in completing this PR for, due to my testing, I've think I'm seeing that from rdflib import Graph
from rdflib.namespace import OWL
def test_rebinding():
g = Graph() # 'core' bind_namespaces (default)
print()
# 'owl' should be bound
assert "owl" in [x for x, y in list(g.namespaces())]
# replace 'owl' with 'sowa'
# 'sowa' should be bound
# 'owl' should not be bound
g.bind("sowa", OWL, replace=True)
assert "sowa" in [x for x, y in list(g.namespaces())]
assert "owl" not in [x for x, y in list(g.namespaces())] |
I believe that's a correctly-failing test according to the implementation, if not according to the API spec. It rather looks like In
Um, wrong again. Turns out that the persistence-backed stores handle this (sort of) gracefully and automatically perform a def bind(self, prefix, namespace):
prefix = prefix.encode("utf-8")
namespace = namespace.encode("utf-8")
bound_prefix = self.__prefix.get(namespace)
if bound_prefix:
self.__namespace.delete(bound_prefix)
self.__prefix[namespace] = prefix
self.__namespace[prefix] = namespace So, for persistence stores using that architecture, the test passes but the def bind(self, prefix, namespace):
self.__prefix[namespace] = prefix
self.__namespace[prefix] = namespace Making the following (fairly straightforward) change to diff --git a/rdflib/plugins/stores/memory.py b/rdflib/plugins/stores/memory.py
index 5a137b5a..135f2a22 100644
--- a/rdflib/plugins/stores/memory.py
+++ b/rdflib/plugins/stores/memory.py
@@ -149,6 +149,9 @@ class SimpleMemory(Store):
return i
def bind(self, prefix, namespace):
+ bound_prefix = self.__prefix.get(namespace)
+ if bound_prefix:
+ self.__namespace.delete(bound_prefix)
self.__prefix[namespace] = prefix
self.__namespace[prefix] = namespace
@@ -400,6 +403,9 @@ class Memory(Store):
yield triple, self.__contexts(triple)
def bind(self, prefix, namespace):
+ bound_prefix = self.__prefix.get(namespace)
+ if bound_prefix:
+ del self.__namespace[bound_prefix]
self.__prefix[namespace] = prefix
self.__namespace[prefix] = namespace
def test_rebinding():
g = Graph() # 'core' bind_namespaces (default)
# 'owl' should be bound
assert "owl" in [x for x, y in list(g.namespaces())]
NOWL = g.store.namespace('owl') # see ¹
# replace 'owl' with 'sowa'
# 'sowa' should be bound
# 'owl' should not be bound
g.bind("sowa", NOWL)
assert "sowa" in [x for x, y in list(g.namespaces())]
assert "owl" not in [x for x, y in list(g.namespaces())] ¹ |
That's what I though I was seeing, that it was Store-specific and that the Memory stores were failing! I was hoping to be able to restrict my PR changes to the namespace module but clearly it's not possible to sort this our without also touching the Memory stores. OK, I'll make the updates as outlined above later today. |
Happy to help here if you don't find time, can try make a separate PR to fix it tomorrow if you want, just ping me. |
Thanks for the offer @aucampia. I think I'd like to give this a go initially. The fix for Memory stores is easy, even though I was trying to avoid having to fiddle with the Stores! - but I'm keen to test out other stores too, especially the SPARQL stores. So I'll give it a shot shortly and reach out to you if I need a hand. |
@aucampia any ideas as to why everything's failing? It looks like a system fault with errors such as Running locally, everything passes so I think I'm done with the fixes for Stores. |
mypy is failing, will make a fix to your branch in a moment. |
@nicholascar let me know if you want me to split things off from this PR, can easily use rebase to keep your commits the same just put them in a seperate branch with you as author, I think a lot of these doc changes are very helpful and very unobjectionable. Up to you though. |
Will look at tying up all loose ends here next on 2022-04-18 if there is no further feedback. |
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.
Well in hand now.
Will rebase and fix the docs warning. |
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
This is only failing the documentation building check, but that is being addressed elsewhere, so I'm going to merge this in now. |
Happy with that, I figured out what the problem is and will make a PR to fix on master shortly. |
it would be awesome to have a new release with this PR in - I'm currently blocked on upgrading rdflib in a few projects as the namespace pollution is causing issues |
@cmungall we're going to get a release out ASAP. Yes, I'm in the same boat as you! |
Just a friendly ping to see if there was a timeline for a new release with the namespace pollution removed? |
… back the `wgs` binding <RDFLib#1686> inadvertently changed prefix for `https://schema.org/` from `schema` to `sdo` and removed the `wgs` prefix. This PR changes the `https://schema.org/` binding back to `schema` and adds the `wgs` binding back. - Closes <RDFLib#2196>.
… the `wgs` binding back <RDFLib#1686> inadvertently changed the prefix for `https://schema.org/` from `schema` to `sdo` and removed the `wgs` prefix. This PR changes the `https://schema.org/` binding back to `schema` and adds the `wgs` binding back. - Closes <RDFLib#2196>.
… the `wgs` binding back <RDFLib#1686> inadvertently changed the prefix for `https://schema.org/` from `schema` to `sdo` and removed the `wgs` prefix. This PR changes the `https://schema.org/` binding back to `schema` and adds the `wgs` binding back. - Closes <RDFLib#2196>.
… the `wgs` binding back <RDFLib#1686> inadvertently changed the prefix for `https://schema.org/` from `schema` to `sdo` and removed the `wgs` prefix. This PR changes the `https://schema.org/` binding back to `schema` and adds the `wgs` binding back. - Closes <RDFLib#2196>.
… the `wgs` binding back <RDFLib#1686> inadvertently changed the prefix for `https://schema.org/` from `schema` to `sdo` and removed the `wgs` prefix. This PR changes the `https://schema.org/` binding back to `schema` and adds the `wgs` binding back. - Closes <RDFLib#2196>.
<RDFLib#1686> inadvertently removed the `wgs` prefix. This change adds it back. - Closes <RDFLib#2196>.
Fixes #1679
Proposed Changes
Graph()
:bind_namespaces
Since this PR is really needed - see the comments - I'm pushing it up to review before the prefix.cc option is implemented.