-
Notifications
You must be signed in to change notification settings - Fork 560
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
Fix issue with namespace rebinding #1800
Fix issue with namespace rebinding #1800
Conversation
Also add additional tests for namespace rebinding.
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
@gjhiggins Good spotting on this - I think we need to fix it for other stores also though, I will add an additional test to check this against other stores and then add fixes for that. |
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.
looks good, thank you
Maybe save you some time, here's a patch for diff --git a/rdflib/plugins/stores/berkeleydb.py b/rdflib/plugins/stores/berkeleydb.py
index 9ca4380b..857b950d 100644
--- a/rdflib/plugins/stores/berkeleydb.py
+++ b/rdflib/plugins/stores/berkeleydb.py
@@ -470,10 +470,17 @@ class BerkeleyDB(Store):
prefix = prefix.encode("utf-8")
namespace = namespace.encode("utf-8")
bound_prefix = self.__prefix.get(namespace)
- if override and bound_prefix:
- self.__namespace.delete(bound_prefix)
- self.__prefix[namespace] = prefix
- self.__namespace[prefix] = namespace
+ bound_namespace = self.__namespace.get(prefix)
+ if override:
+ if bound_prefix:
+ self.__namespace.delete(bound_prefix)
+ if bound_namespace:
+ self.__prefix.delete(bound_namespace)
+ self.__prefix[namespace] = prefix
+ self.__namespace[prefix] = namespace
+ else:
+ self.__prefix[bound_namespace or namespace] = bound_prefix or prefix
+ self.__namespace[bound_prefix or prefix] = bound_namespace or namespace
def namespace(self, prefix):
prefix = prefix.encode("utf-8") |
@nicholascar @gjhiggins I'm going to throw caution to the wind with this and fix some things which I would have liked to keep to separate PRs in this PR, but we really should try to keep these things less entangled in future so we can get changes in quicker. |
There are 4 stores doing about the same thing ( |
Will look at this further tomorrow, won't work that well with memory store, but there is also many cases where strings are checked for turthyness instead of |
Actually, scratch the last comments, I will fix further issues and expand tests once this is in master. But beware, there are still at least one store with this issue and then this should be checking is None instead of turthyness, and it should also use a proper null coalescing method, and I'm not entirely sure of all the consequences, but I will figure it out once this and #1686 is merged. |
self.__namespace[prefix] = namespace | ||
bound_namespace = self.__namespace.get(prefix) | ||
if override: | ||
if bound_prefix: |
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.
if bound_prefix: | |
if bound_prefix is not None: |
if override: | ||
if bound_prefix: | ||
self.__namespace.delete(bound_prefix) | ||
if bound_namespace: |
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.
if bound_namespace: | |
if bound_namespace is not None: |
self.__prefix[namespace] = prefix | ||
self.__namespace[prefix] = namespace | ||
else: | ||
self.__prefix[bound_namespace or namespace] = bound_prefix or prefix |
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.
This will not do the right thing if bound_namespace is ""
self.__namespace[prefix] = namespace | ||
else: | ||
self.__prefix[bound_namespace or namespace] = bound_prefix or prefix | ||
self.__namespace[bound_prefix or prefix] = bound_namespace or namespace |
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.
This will not do the right thing if bound_prefix is ""
bound_namespace | ||
) | ||
if override: | ||
if bound_prefix: |
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.
if bound_prefix: | |
if bound_prefix is not None: |
if override: | ||
if bound_prefix: | ||
del self.__namespace[bound_prefix] | ||
if bound_namespace: |
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.
if bound_namespace: | |
if bound_namespace is not None: |
self.__prefix[namespace] = prefix | ||
self.__namespace[prefix] = namespace | ||
else: | ||
self.__prefix[bound_namespace or namespace] = bound_prefix or prefix |
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.
This will not do the right thing if bound_namespace
is ""
self.__namespace[prefix] = namespace | ||
else: | ||
self.__prefix[bound_namespace or namespace] = bound_prefix or prefix | ||
self.__namespace[bound_prefix or prefix] = bound_namespace or namespace |
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.
This will not do the right thing if bound_prefix
is ""
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.
Should use null coalesing, something like this:
_AnyT = TypeVar("_AnyT")
def _coalesce(*args: Optional[_AnyT]) -> Optional[_AnyT]:
for arg in args:
if arg is not None:
return arg
return None
@gjhiggins I will redo this on master. |
Also add additional tests for namespace rebinding.
Note
Commits with @gjhiggins as author are taken from: