summaryrefslogtreecommitdiff
path: root/test/utils
diff options
context:
space:
mode:
authorIwan Aucamp <aucampia@gmail.com>2022-08-12 21:47:53 +0200
committerGitHub <noreply@github.com>2022-08-12 21:47:53 +0200
commita39d1436e00affc3cba9e903b22700029d8e8163 (patch)
treeece335820197987986b191e49f467b557a9e310a /test/utils
parenta4b930553118bc835ba424f5ebf79e53053890b5 (diff)
downloadrdflib-a39d1436e00affc3cba9e903b22700029d8e8163.tar.gz
fix: handling of Literal datatype (#2076)
Check datatype against `None` instead of checking it's truthiness (i.e. `if datatype is not None:` instead of `if datatype:`). Checking truthiness instead of `is not None` causes a blank string to be treated the same as None. The consequence of this was that `Literal.datatype` could be a `str`, a `URIRef` or `None`, instead of just a `URIRef` or `None` as was seemingly intended. Other changes: - Changed the type of `Literal.datatype` to be `Optional[URIRef]` instead of `Optional[str]` now that `str` will always be converted to `URIRef` even if it is a blank string. - Changed `rdflib.util._coalesce` to make it easier and safer to use with a non-`None` default value. - Changed `rdflib.util` to avoid issues with circular imports.
Diffstat (limited to 'test/utils')
-rw-r--r--test/utils/literal.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/test/utils/literal.py b/test/utils/literal.py
new file mode 100644
index 00000000..1b3f3798
--- /dev/null
+++ b/test/utils/literal.py
@@ -0,0 +1,28 @@
+from __future__ import annotations
+
+import builtins
+from dataclasses import dataclass
+from typing import Any, Union
+
+from rdflib.term import Literal, URIRef
+
+
+@dataclass
+class LiteralChecker:
+ value: Union[builtins.ellipsis, Any] = ...
+ language: Union[builtins.ellipsis, str, None] = ...
+ datatype: Union[builtins.ellipsis, URIRef, None] = ...
+ ill_typed: Union[builtins.ellipsis, bool, None] = ...
+ lexical: Union[builtins.ellipsis, str] = ...
+
+ def check(self, actual: Literal) -> None:
+ if self.value is not Ellipsis:
+ assert self.value == actual.value
+ if self.lexical is not Ellipsis:
+ assert self.lexical == f"{actual}"
+ if self.ill_typed is not Ellipsis:
+ assert self.ill_typed == actual.ill_typed
+ if self.language is not Ellipsis:
+ assert self.language == actual.language
+ if self.datatype is not Ellipsis:
+ assert self.datatype == actual.datatype