Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholascar authored Jan 6, 2025
2 parents fe6e6fd + 6747715 commit 31ddb93
Show file tree
Hide file tree
Showing 21 changed files with 471 additions and 268 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
ci:
# https://pre-commit.ci/#configuration
autoupdate_schedule: weekly
autoupdate_schedule: quarterly
autofix_prs: false

# https://pre-commit.com/#adding-pre-commit-plugins-to-your-project
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# WARNING: Ruff version should be the same as in `pyproject.toml`
rev: v0.7.1
rev: v0.7.2
hooks:
- id: ruff
args: ["--fix"]
Expand Down
2 changes: 1 addition & 1 deletion devtools/requirements-poetry.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Fixing this here as readthedocs can't use the compiled requirements-poetry.txt
# due to conflicts.
poetry==1.8.4
poetry==1.8.5
2 changes: 1 addition & 1 deletion docker/latest/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/library/python:3.13.0-slim@sha256:2ec5a4a5c3e919570f57675471f081d6299668d909feabd8d4803c6c61af666c
FROM docker.io/library/python:3.13.1-slim@sha256:1127090f9fff0b8e7c3a1367855ef8a3299472d2c9ed122948a576c39addeaf1

COPY docker/latest/requirements.txt /var/tmp/build/

Expand Down
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.13.0-slim@sha256:751d8bece269ba9e672b3f2226050e7e6fb3f3da3408b5dcb5d415a054fcb061
FROM docker.io/library/python:3.13.1-slim@sha256:1127090f9fff0b8e7c3a1367855ef8a3299472d2c9ed122948a576c39addeaf1

# This file is generated from docker:unstable in Taskfile.yml
COPY var/requirements.txt /var/tmp/build/
Expand Down
11 changes: 4 additions & 7 deletions docs/developers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,17 @@ our black.toml config file:
poetry run black .
Check style and conventions with `flake8 <https://flake8.pycqa.org/en/latest/>`_:
Check style and conventions with `ruff <https://docs.astral.sh/ruff/linter/>`_:

.. code-block:: bash
poetry run flake8 rdflib
poetry run ruff check
We also provide a `flakeheaven <https://pypi.org/project/flakeheaven/>`_
baseline that ignores existing flake8 errors and only reports on newly
introduced flake8 errors:
Any issues that are found can potentially be fixed automatically using:

.. code-block:: bash
poetry run flakeheaven
poetry run ruff check --fix
Check types with `mypy <http://mypy-lang.org/>`_:

Expand Down
325 changes: 171 additions & 154 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pytest-cov = ">=4,<7"
coverage = {version = "^7.0.1", extras = ["toml"]}
types-setuptools = ">=68.0.0.3,<72.0.0.0"
setuptools = ">=68,<72"
wheel = ">=0.42,<0.45"
wheel = ">=0.42,<0.46"

[tool.poetry.group.docs.dependencies]
sphinx = ">=7.1.2,<8"
Expand All @@ -73,7 +73,7 @@ sphinx-autodoc-typehints = ">=2.3.0,<2.4.0"
typing-extensions = "^4.11.0"

[tool.poetry.group.lint.dependencies]
ruff = ">=0.7.1,<0.8.0"
ruff = ">=0.7.2,<1"

[tool.poetry.extras]
berkeleydb = ["berkeleydb"]
Expand Down
3 changes: 2 additions & 1 deletion rdflib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"BNode",
"IdentifiedNode",
"Literal",
"Node",
"Variable",
"Namespace",
"Dataset",
Expand Down Expand Up @@ -195,7 +196,7 @@
XSD,
Namespace,
)
from rdflib.term import BNode, IdentifiedNode, Literal, URIRef, Variable
from rdflib.term import BNode, IdentifiedNode, Literal, Node, URIRef, Variable

from rdflib import plugin, query, util # isort:skip
from rdflib.container import * # isort:skip # noqa: F403
18 changes: 9 additions & 9 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,19 +966,19 @@ def triples_choices(
self,
triple: (
tuple[
list[_SubjectType] | tuple[_SubjectType],
list[_SubjectType] | tuple[_SubjectType, ...],
_PredicateType,
_ObjectType | None,
]
| tuple[
_SubjectType | None,
list[_PredicateType] | tuple[_PredicateType],
list[_PredicateType] | tuple[_PredicateType, ...],
_ObjectType | None,
]
| tuple[
_SubjectType | None,
_PredicateType,
list[_ObjectType] | tuple[_ObjectType],
list[_ObjectType] | tuple[_ObjectType, ...],
]
),
context: _ContextType | None = None,
Expand Down Expand Up @@ -2208,19 +2208,19 @@ def triples_choices(
self,
triple: (
tuple[
list[_SubjectType] | tuple[_SubjectType],
list[_SubjectType] | tuple[_SubjectType, ...],
_PredicateType,
_ObjectType | None,
]
| tuple[
_SubjectType | None,
list[_PredicateType] | tuple[_PredicateType],
list[_PredicateType] | tuple[_PredicateType, ...],
_ObjectType | None,
]
| tuple[
_SubjectType | None,
_PredicateType,
list[_ObjectType] | tuple[_ObjectType],
list[_ObjectType] | tuple[_ObjectType, ...],
]
),
context: _ContextType | None = None,
Expand Down Expand Up @@ -2962,19 +2962,19 @@ def triples_choices(
self,
triple: (
tuple[
list[_SubjectType] | tuple[_SubjectType],
list[_SubjectType] | tuple[_SubjectType, ...],
_PredicateType,
_ObjectType | None,
]
| tuple[
_SubjectType | None,
list[_PredicateType] | tuple[_PredicateType],
list[_PredicateType] | tuple[_PredicateType, ...],
_ObjectType | None,
]
| tuple[
_SubjectType | None,
_PredicateType,
list[_ObjectType] | tuple[_ObjectType],
list[_ObjectType] | tuple[_ObjectType, ...],
]
),
context: _ContextType | None = None,
Expand Down
9 changes: 8 additions & 1 deletion rdflib/plugins/parsers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# we should consider streaming the input to deal with arbitrarily large graphs.
from __future__ import annotations

import secrets
import warnings
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, Union
Expand Down Expand Up @@ -215,6 +216,7 @@ def __init__(
if allow_lists_of_lists is not None
else ALLOW_LISTS_OF_LISTS
)
self.invalid_uri_to_bnode: dict[str, BNode] = {}

def parse(self, data: Any, context: Context, dataset: Graph) -> Graph:
topcontext = False
Expand Down Expand Up @@ -623,7 +625,12 @@ def _to_rdf_id(self, context: Context, id_val: str) -> IdentifiedNode | None:
uri = context.resolve(id_val)
if not self.generalized_rdf and ":" not in uri:
return None
return URIRef(uri)
node: IdentifiedNode = URIRef(uri)
if not str(node):
if id_val not in self.invalid_uri_to_bnode:
self.invalid_uri_to_bnode[id_val] = BNode(secrets.token_urlsafe(20))
node = self.invalid_uri_to_bnode[id_val]
return node

def _get_bnodeid(self, ref: str) -> str | None:
if not ref.startswith("_:"):
Expand Down
17 changes: 15 additions & 2 deletions rdflib/plugins/serializers/longturtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

from typing import IO, Any, Optional

from rdflib.compare import to_canonical_graph
from rdflib.exceptions import Error
from rdflib.graph import Graph
from rdflib.namespace import RDF
from rdflib.term import BNode, Literal, URIRef

Expand All @@ -42,7 +44,16 @@ class LongTurtleSerializer(RecursiveSerializer):

def __init__(self, store):
self._ns_rewrite = {}
super(LongTurtleSerializer, self).__init__(store)
store = to_canonical_graph(store)
content = store.serialize(format="application/n-triples")
lines = content.split("\n")
lines.sort()
graph = Graph()
graph.parse(
data="\n".join(lines), format="application/n-triples", skolemize=True
)
graph = graph.de_skolemize()
super(LongTurtleSerializer, self).__init__(graph)
self.keywords = {RDF.type: "a"}
self.reset()
self.stream = None
Expand Down Expand Up @@ -186,7 +197,7 @@ def s_squared(self, subject):
return False
self.write("\n" + self.indent() + "[]")
self.predicateList(subject, newline=False)
self.write(" ;\n.")
self.write("\n.")
return True

def path(self, node, position, newline=False):
Expand Down Expand Up @@ -303,6 +314,8 @@ def objectList(self, objects):
if count > 1:
if not isinstance(objects[0], BNode):
self.write("\n" + self.indent(1))
else:
self.write(" ")
first_nl = True
self.path(objects[0], OBJECT, newline=first_nl)
for obj in objects[1:]:
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/sparql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ def expandCollection(terms: ParseResults) -> list[list[Any]]:
AskQuery = Comp(
"AskQuery",
Keyword("ASK")
+ Param("datasetClause", ZeroOrMore(DatasetClause))
+ ZeroOrMore(ParamList("datasetClause", DatasetClause))
+ WhereClause
+ SolutionModifier
+ ValuesClause,
Expand Down
6 changes: 3 additions & 3 deletions rdflib/plugins/stores/sparqlstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,19 @@ def triples_choices(
self,
_: (
tuple[
list[_SubjectType] | tuple[_SubjectType],
list[_SubjectType] | tuple[_SubjectType, ...],
_PredicateType,
_ObjectType | None,
]
| tuple[
_SubjectType | None,
list[_PredicateType] | tuple[_PredicateType],
list[_PredicateType] | tuple[_PredicateType, ...],
_ObjectType | None,
]
| tuple[
_SubjectType | None,
_PredicateType,
list[_ObjectType] | tuple[_ObjectType],
list[_ObjectType] | tuple[_ObjectType, ...],
]
),
context: _ContextType | None = None,
Expand Down
12 changes: 6 additions & 6 deletions rdflib/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,19 @@ def triples_choices(
self,
triple: (
tuple[
list[_SubjectType] | tuple[_SubjectType],
list[_SubjectType] | tuple[_SubjectType, ...],
_PredicateType,
_ObjectType | None,
]
| tuple[
_SubjectType | None,
list[_PredicateType] | tuple[_PredicateType],
list[_PredicateType] | tuple[_PredicateType, ...],
_ObjectType | None,
]
| tuple[
_SubjectType | None,
_PredicateType,
list[_ObjectType] | tuple[_ObjectType],
list[_ObjectType] | tuple[_ObjectType, ...],
]
),
context: _ContextType | None = None,
Expand All @@ -301,9 +301,9 @@ def triples_choices(
time from the default 'fallback' implementation, which will iterate
over each term in the list and dispatch to triples
"""
subject: _SubjectType | list[_SubjectType] | tuple[_SubjectType] | None
predicate: _PredicateType | list[_PredicateType] | tuple[_PredicateType]
object_: _ObjectType | list[_ObjectType] | tuple[_ObjectType] | None
subject: _SubjectType | list[_SubjectType] | tuple[_SubjectType, ...] | None
predicate: _PredicateType | list[_PredicateType] | tuple[_PredicateType, ...]
object_: _ObjectType | list[_ObjectType] | tuple[_ObjectType, ...] | None
subject, predicate, object_ = triple
if isinstance(object_, (list, tuple)):
# MyPy thinks these are unreachable due to the triple pattern signature.
Expand Down
72 changes: 72 additions & 0 deletions test/data/longturtle/longturtle-target.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX schema: <https://schema.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

<http://example.com/nicholas>
a schema:Person ;
schema:age 41 ;
schema:alternateName
[
schema:name "Dr N.J. Car" ;
] ,
"N.J. Car" ,
"Nick Car" ;
schema:name
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
schema:hasPart
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
schema:hasPart
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
rdf:value "Car" ;
] ,
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
rdf:value "Maxov" ;
] ;
] ,
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
rdf:value "Nicholas" ;
] ,
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
rdf:value "John" ;
] ;
] ;
schema:worksFor <https://kurrawong.ai> ;
.

<https://kurrawong.ai>
a schema:Organization ;
schema:location <https://kurrawong.ai/hq> ;
.

<https://kurrawong.ai/hq>
a schema:Place ;
schema:address
[
a schema:PostalAddress ;
schema:addressCountry
[
schema:identifier "au" ;
schema:name "Australia" ;
] ;
schema:addressLocality "Shorncliffe" ;
schema:addressRegion "QLD" ;
schema:postalCode 4017 ;
schema:streetAddress (
72
"Yundah"
"Street"
) ;
] ;
schema:geo
[
schema:polygon "POLYGON((153.082403 -27.325801, 153.08241 -27.32582, 153.082943 -27.325612, 153.083010 -27.325742, 153.083543 -27.325521, 153.083456 -27.325365, 153.082403 -27.325801))"^^geo:wktLiteral ;
] ;
schema:name "KurrawongAI HQ" ;
.
11 changes: 11 additions & 0 deletions test/jsonld/local-suite/manifest.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
"purpose": "Multiple @id aliases. Issue #2164",
"input": "toRdf-twoimports-in.jsonld",
"expect": "toRdf-twoimports-out.nq"
},
{
"@id": "#toRdf-two-invalid-ids",
"@type": ["jld:PositiveEvaluationTest", "jld:ToRDFTest"],
"name": "Two invalid identifiers",
"purpose": "Multiple nodes with invalid @ids are not merged together.",
"option": {
"produceGeneralizedRdf": true
},
"input": "toRdf-twoinvalidids-in.jsonld",
"expect": "toRdf-twoinvalidids-out.nq"
}
]
}
Loading

0 comments on commit 31ddb93

Please sign in to comment.