From 2a1c8db88eaffb4eace4f53e764527ea66b377e2 Mon Sep 17 00:00:00 2001 From: Antoine Lambert <anlambert@softwareheritage.org> Date: Mon, 13 Jan 2025 18:28:35 +0100 Subject: [PATCH] metatadata_dictionary: Fix mypy errors since rdflib 7.1.2 release --- requirements.txt | 2 +- swh/indexer/bibtex.py | 15 +++++++++++---- swh/indexer/metadata_dictionary/base.py | 4 ++-- swh/indexer/metadata_dictionary/cff.py | 8 +++++--- swh/indexer/metadata_dictionary/utils.py | 18 +++++++++--------- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/requirements.txt b/requirements.txt index c5529a47..87c9f405 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,7 @@ frozendict != 2.1.2 iso8601 pybtex pyld -rdflib +rdflib >= 7.1.2 sentry-sdk typing-extensions xmltodict diff --git a/swh/indexer/bibtex.py b/swh/indexer/bibtex.py index 0ba65b07..6a4db887 100644 --- a/swh/indexer/bibtex.py +++ b/swh/indexer/bibtex.py @@ -1,4 +1,4 @@ -# Copyright (C) 2023-2024 The Software Heritage developers +# Copyright (C) 2023-2025 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -8,7 +8,7 @@ import collections import json import secrets import sys -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List, Optional, Union import uuid import iso8601 @@ -29,6 +29,10 @@ it is not used outside :func:`codemeta_to_bibtex`. MACRO_PREFIX = "macro" + secrets.token_urlsafe(16).replace("_", "") +GraphObjectType = Union[ + rdflib.term.IdentifiedNode, rdflib.term.Literal, rdflib.term.Variable +] + class BibTeXWithMacroWriter(Writer): @@ -88,7 +92,7 @@ def codemeta_to_bibtex( def add_person( persons: List[Person], - person_id: rdflib.term.Node, + person_id: GraphObjectType, role_property: rdflib.term.URIRef, ) -> None: # If the node referenced by 'person_id' is actually a Role node, we need to look @@ -115,7 +119,9 @@ def codemeta_to_bibtex( if str(person) and person not in persons: persons.append(person) - def add_affiliations(person: rdflib.term.Node) -> None: + def add_affiliations( + person: GraphObjectType, + ) -> None: for _, _, organization in g.triples((person, SCHEMA.affiliation, None)): add_person( persons["organization"], organization, role_property=SCHEMA.affiliation @@ -136,6 +142,7 @@ def codemeta_to_bibtex( if author_or_author_list == RDF.nil: # Workaround for https://github.com/RDFLib/rdflib/pull/2818 continue + assert isinstance(author_or_author_list, rdflib.term.IdentifiedNode) for author in rdflib.collection.Collection(g, author_or_author_list): add_person(persons["author"], author, role_property=SCHEMA.author) add_affiliations(author) diff --git a/swh/indexer/metadata_dictionary/base.py b/swh/indexer/metadata_dictionary/base.py index f356ed9e..3bad339a 100644 --- a/swh/indexer/metadata_dictionary/base.py +++ b/swh/indexer/metadata_dictionary/base.py @@ -1,4 +1,4 @@ -# Copyright (C) 2017-2024 The Software Heritage developers +# Copyright (C) 2017-2025 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -275,7 +275,7 @@ class DictMapping(BaseMapping): return self.normalize_translation(translated_metadata) def _translate_to_graph( - self, graph: rdflib.Graph, root: rdflib.term.Identifier, content_dict: Dict + self, graph: rdflib.Graph, root: rdflib.term.IdentifiedNode, content_dict: Dict ) -> None: """ Translates content by parsing content from a dict object diff --git a/swh/indexer/metadata_dictionary/cff.py b/swh/indexer/metadata_dictionary/cff.py index 15a87481..c5f9e71b 100644 --- a/swh/indexer/metadata_dictionary/cff.py +++ b/swh/indexer/metadata_dictionary/cff.py @@ -1,4 +1,4 @@ -# Copyright (C) 2021-2024 The Software Heritage developers +# Copyright (C) 2021-2025 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -29,8 +29,10 @@ class CffMapping(YamlMapping, SingleFileIntrinsicMapping): date_fields = ["date-released"] uri_fields = ["url", "repository-code"] - def _translate_author(self, graph: Graph, author: dict) -> rdflib.term.Node: - node: rdflib.term.Node + def _translate_author( + self, graph: Graph, author: dict + ) -> rdflib.term.IdentifiedNode: + node: rdflib.term.IdentifiedNode if ( "orcid" in author and isinstance(author["orcid"], str) diff --git a/swh/indexer/metadata_dictionary/utils.py b/swh/indexer/metadata_dictionary/utils.py index 6aaf4fd5..437cec12 100644 --- a/swh/indexer/metadata_dictionary/utils.py +++ b/swh/indexer/metadata_dictionary/utils.py @@ -1,4 +1,4 @@ -# Copyright (C) 2022 The Software Heritage developers +# Copyright (C) 2022-2025 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -31,9 +31,9 @@ def prettyprint_graph(graph: Graph, root: URIRef): def add_list( graph: Graph, - subject: rdflib.term.Node, - predicate: rdflib.term.Identifier, - objects: Sequence[rdflib.term.Node], + subject: rdflib.term.IdentifiedNode, + predicate: rdflib.term.IdentifiedNode, + objects: Sequence[rdflib.term.IdentifiedNode], ) -> None: """Adds triples to the ``graph`` so that they are equivalent to this JSON-LD object:: @@ -63,9 +63,9 @@ TValue = TypeVar("TValue") def add_map( graph: Graph, - subject: rdflib.term.Node, - predicate: rdflib.term.Identifier, - f: Callable[[Graph, TValue], Optional[rdflib.term.Node]], + subject: rdflib.term.IdentifiedNode, + predicate: rdflib.term.IdentifiedNode, + f: Callable[[Graph, TValue], Optional[rdflib.term.IdentifiedNode]], values: Iterable[TValue], ) -> None: """Helper for :func:`add_list` that takes a mapper function ``f``.""" @@ -75,8 +75,8 @@ def add_map( def add_url_if_valid( graph: Graph, - subject: rdflib.term.Node, - predicate: rdflib.term.Identifier, + subject: rdflib.term.IdentifiedNode, + predicate: rdflib.term.IdentifiedNode, url: Any, ) -> None: """Adds ``(subject, predicate, url)`` to the graph if ``url`` is well-formed. -- GitLab