Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 66f626c

Browse files
committedJan 26, 2025·
notation3.py: don't normalize float representation
fix behavior of the n3 parser family to avoid normalizing raw float string representation which makes it impossible to roundtrip the exact original string representation of e.g. 1e10
1 parent 8702405 commit 66f626c

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed
 

‎rdflib/plugins/parsers/notation3.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ def unicodeExpand(m: Match) -> str:
376376
langcode = re.compile(r"[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*")
377377

378378

379+
class sfloat(str):
380+
""" don't normalize raw XSD.double string representation """
381+
382+
379383
class SinkParser:
380384
def __init__(
381385
self,
@@ -1522,7 +1526,7 @@ def nodeOrLiteral(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
15221526
m = exponent_syntax.match(argstr, i)
15231527
if m:
15241528
j = m.end()
1525-
res.append(float(argstr[i:j]))
1529+
res.append(sfloat(argstr[i:j]))
15261530
return j
15271531

15281532
m = decimal_syntax.match(argstr, i)
@@ -1913,15 +1917,15 @@ def normalise(self, f: Formula | Graph | None, n: int) -> Literal: ...
19131917
def normalise(self, f: Formula | Graph | None, n: Decimal) -> Literal: ...
19141918

19151919
@overload
1916-
def normalise(self, f: Formula | Graph | None, n: float) -> Literal: ...
1920+
def normalise(self, f: Formula | Graph | None, n: sfloat) -> Literal: ...
19171921

19181922
@overload
19191923
def normalise(self, f: Formula | Graph | None, n: Node) -> Node: ...
19201924

19211925
def normalise(
19221926
self,
19231927
f: Formula | Graph | None,
1924-
n: Union[tuple[int, str], bool, int, Decimal, float, Node, _AnyT],
1928+
n: Union[tuple[int, str], bool, int, Decimal, sfloat, Node, _AnyT],
19251929
) -> Union[URIRef, Literal, BNode, Node, _AnyT]:
19261930
if isinstance(n, tuple):
19271931
return URIRef(str(n[1]))
@@ -1941,7 +1945,7 @@ def normalise(
19411945
s = Literal(value, datatype=DECIMAL_DATATYPE)
19421946
return s
19431947

1944-
if isinstance(n, float):
1948+
if isinstance(n, sfloat):
19451949
s = Literal(str(n), datatype=DOUBLE_DATATYPE)
19461950
return s
19471951

0 commit comments

Comments
 (0)
Please sign in to comment.