Skip to content
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

notation3.py: don't normalize float representation #3020

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions rdflib/plugins/parsers/notation3.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ def unicodeExpand(m: Match) -> str:
langcode = re.compile(r"[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*")


class sfloat(str):
""" don't normalize raw XSD.double string representation """


class SinkParser:
def __init__(
self,
Expand Down Expand Up @@ -1522,7 +1526,7 @@ def nodeOrLiteral(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
m = exponent_syntax.match(argstr, i)
if m:
j = m.end()
res.append(float(argstr[i:j]))
res.append(sfloat(argstr[i:j]))
return j

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

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

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

def normalise(
self,
f: Formula | Graph | None,
n: Union[tuple[int, str], bool, int, Decimal, float, Node, _AnyT],
n: Union[tuple[int, str], bool, int, Decimal, sfloat, Node, _AnyT],
) -> Union[URIRef, Literal, BNode, Node, _AnyT]:
if isinstance(n, tuple):
return URIRef(str(n[1]))
Expand All @@ -1941,7 +1945,7 @@ def normalise(
s = Literal(value, datatype=DECIMAL_DATATYPE)
return s

if isinstance(n, float):
if isinstance(n, sfloat):
s = Literal(str(n), datatype=DOUBLE_DATATYPE)
return s

Expand Down
26 changes: 26 additions & 0 deletions test/test_n3.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,32 @@ def test_empty_prefix(self):
g2
), "Document with declared empty prefix must match default #"

def test_float_no_norm(self):
import rdflib
_ps = rdflib.NORMALIZE_LITERALS
try:
bads = []
for norm_lit in (True, False):
rdflib.NORMALIZE_LITERALS = norm_lit
g1 = Graph()
g1.parse(data=":a :b 1e10, 1e0 .", format="n3")
strep = [str(o) for o in g1.objects()]
if norm_lit:
if '1e10' not in strep and '1e0' not in strep:
pass
else:
bads.append(('NOT normalized when should have been', strep))
else:
if '1e10' in strep and '1e0' in strep:
pass
else:
bads.append(('normalized when it should NOT have been', strep))

finally:
rdflib.NORMALIZE_LITERALS = _ps

assert not bads, bads


class TestRegularExpressions:
def test_exponents(self):
Expand Down
Loading