Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/poetry-1.8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholascar committed Mar 20, 2024
2 parents dded980 + a8f37cc commit 6e809a9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion docker/unstable/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/library/python:3.12.2-slim@sha256:5c73034c2bc151596ee0f1335610735162ee2b148816710706afec4757ad5b1e
FROM docker.io/library/python:3.12.2-slim@sha256:36d57d7f9948fefe7b6092cfe8567da368033e71ba281b11bb9eeffce3d45bc6

# This file is generated from docker:unstable in Taskfile.yml
COPY var/requirements.txt /var/tmp/build/
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,18 @@ def parse(
if format is None:
format = "turtle"
could_not_guess_format = True
parser = plugin.get(format, Parser)()
try:
parser = plugin.get(format, Parser)()
except plugin.PluginException:
# Handle the case when a URLInputSource returns RDF but with the headers
# as a format that does not exist in the plugin system.
# Use guess_format to guess the format based on the input's file suffix.
format = rdflib.util.guess_format(
source if not isinstance(source, InputSource) else str(source)
)
if format is None:
raise
parser = plugin.get(format, Parser)()
try:
# TODO FIXME: Parser.parse should have **kwargs argument.
parser.parse(source, self, **args)
Expand Down
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
13 changes: 13 additions & 0 deletions test/test_graph/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,19 @@ def test_guess_format_for_parse_http(
checker.check(len(graph))


@pytest.mark.webtest
def test_guess_format_for_parse_http_text_plain():
# Any raw url of a file from GitHub will return the content-type with text/plain.
url = "https://raw.githubusercontent.com/AGLDWG/vocpub-profile/master/validators/validator.ttl"
graph = Graph().parse(url)
assert len(graph) > 0

# A url that returns content-type text/html.
url = "https://github.com/RDFLib/rdflib/issues/2734"
with pytest.raises(PluginException):
graph = Graph().parse(url)


def test_parse_file_uri(make_graph: GraphFactory):
EG = Namespace("http://example.org/#") # noqa: N806
g = make_graph()
Expand Down

0 comments on commit 6e809a9

Please sign in to comment.