Skip to content

Commit

Permalink
Cleanup literal comparison ops (#863) (#2745)
Browse files Browse the repository at this point in the history
Remove unnecessary handling of py3 literal comparison
ops since Literal provides total ordering.
  • Loading branch information
wallberg committed Mar 20, 2024
1 parent cf427b0 commit 84ed09a
Showing 1 changed file with 1 addition and 30 deletions.
31 changes: 1 addition & 30 deletions rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from __future__ import annotations

from collections import defaultdict
from functools import cmp_to_key
from typing import (
IO,
TYPE_CHECKING,
Expand All @@ -32,34 +31,6 @@
__all__ = ["RecursiveSerializer", "TurtleSerializer"]


def _object_comparator(a: Node, b: Node) -> int:
"""
for nice clean output we sort the objects of triples,
some of them are literals,
these are sorted according to the sort order of the underlying python objects
in py3 not all things are comparable.
This falls back on comparing string representations when not.
"""

try:
# type error: Unsupported left operand type for > ("Node")
if a > b: # type: ignore[operator]
return 1
# type error: Unsupported left operand type for < ("Node")
if a < b: # type: ignore[operator]
return -1
return 0

except TypeError:
# type error: Incompatible types in assignment (expression has type "str", variable has type "Node") [assignment]
a = str(a) # type: ignore[assignment]
# type error: Incompatible types in assignment (expression has type "str", variable has type "Node")
b = str(b) # type: ignore[assignment]
# type error: Unsupported left operand type for > ("Node") [operator]
# type error: Unsupported left operand type for < ("Node")
return (a > b) - (a < b) # type: ignore[operator]


class RecursiveSerializer(Serializer):
topClasses = [RDFS.Class]
predicateOrder = [RDF.type, RDFS.label]
Expand Down Expand Up @@ -168,7 +139,7 @@ def sortProperties(
Sort the lists of values. Return a sorted list of properties."""
# Sort object lists
for prop, objects in properties.items():
objects.sort(key=cmp_to_key(_object_comparator))
objects.sort()

# Make sorted list of properties
propList: List[_PredicateType] = []
Expand Down

0 comments on commit 84ed09a

Please sign in to comment.