Fix issue with namespace rebinding#1800
Fix issue with namespace rebinding#1800aucampia wants to merge 3 commits intoRDFLib:default_prefixesfrom
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. |
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.
| 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.
| 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.
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.
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.
| 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.
| 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.
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.
This will not do the right thing if bound_prefix is ""
There was a problem hiding this comment.
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: