Skip to content

Commit ace9ef1

Browse files
committed
Fix type hints.
1 parent 134b60e commit ace9ef1

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/sssom/rdf_internal.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from curies import Converter
1010
from linkml_runtime.linkml_model.meta import SlotDefinition
1111
from linkml_runtime.utils.schemaview import SchemaView
12-
from pandas import DataFrame
12+
from pandas import DataFrame, Series
1313
from rdflib import BNode, Graph, Literal, Node, URIRef
1414
from rdflib.namespace import RDF, RDFS, XSD
1515
from typing_extensions import override
@@ -36,6 +36,7 @@
3636
__all__ = ["MappingSetRDFConverter"]
3737

3838
TRIPLE = tuple[Node, Node, Node]
39+
DICT_OR_SERIES = Union[Dict[str, Any], Series]
3940
MAPPINGS_IRI = URIRef(MAPPINGS, SSSOM_URI_PREFIX)
4041
EXTENSION_DEFINITION_IRI = URIRef(EXTENSION_DEFINITIONS, SSSOM_URI_PREFIX)
4142

@@ -563,7 +564,7 @@ def _multivalue_from_rdf(self, value: Any, slot_name: str, dest: Dict[str, Any])
563564
# Conversion to RDF
564565
#
565566

566-
def dict_to_rdf(self, g: Graph, obj: Dict[str, Any]) -> Node:
567+
def dict_to_rdf(self, g: Graph, obj: DICT_OR_SERIES) -> Node:
567568
"""Export a SSSOM object to a RDF graph.
568569
569570
:param g: The graph to export the object to.
@@ -576,10 +577,11 @@ def dict_to_rdf(self, g: Graph, obj: Dict[str, Any]) -> Node:
576577
g.add(cast(TRIPLE, [subject, RDF.type, self.object_uri]))
577578

578579
for k, v in obj.items():
579-
if self._process_slot(g, subject, k, v):
580+
key = str(k)
581+
if self._process_slot(g, subject, key, v):
580582
continue
581583

582-
slot = self.slots_by_name.get(k)
584+
slot = self.slots_by_name.get(key)
583585
if slot is not None:
584586
pred = self._get_slot_uri(slot)
585587
converter = self._get_value_converter(slot)
@@ -591,8 +593,8 @@ def dict_to_rdf(self, g: Graph, obj: Dict[str, Any]) -> Node:
591593
else:
592594
if not self._is_empty(v):
593595
g.add(cast(TRIPLE, [subject, pred, converter.to_rdf(v)]))
594-
elif not self._extension_to_rdf(g, subject, k, v):
595-
logging.warning(f"Ignoring unexpected {k}={v} slot")
596+
elif not self._extension_to_rdf(g, subject, key, v):
597+
logging.warning(f"Ignoring unexpected {key}={v} slot")
596598

597599
return subject
598600

@@ -611,7 +613,7 @@ def _is_empty(self, value: Any) -> bool:
611613
"""
612614
return value is None or (hasattr(value, "__len__") and len(value) == 0)
613615

614-
def _init_dict_to_rdf(self, g: Graph, obj: Dict[str, Any]) -> Node:
616+
def _init_dict_to_rdf(self, g: Graph, obj: DICT_OR_SERIES) -> Node:
615617
"""Create the root node representing a SSSOM object.
616618
617619
Subclasses should override this method to customize the way
@@ -897,7 +899,7 @@ def msdf_to_rdf(
897899
return g
898900

899901
@override
900-
def _init_dict_to_rdf(self, g: Graph, obj: Dict[str, Any]) -> Node:
902+
def _init_dict_to_rdf(self, g: Graph, obj: DICT_OR_SERIES) -> Node:
901903
if MAPPING_SET_ID in obj:
902904
return URIRef(obj[MAPPING_SET_ID])
903905
else:
@@ -926,7 +928,7 @@ def _process_slot(self, g: Graph, subject: Node, name: str, value: Any) -> bool:
926928
return done
927929

928930
def _mapping_to_rdf(
929-
self, g: Graph, subject: Node, mapping: Dict[str, Any], hydrate: bool
931+
self, g: Graph, subject: Node, mapping: DICT_OR_SERIES, hydrate: bool
930932
) -> None:
931933
mapping_node = self.mapping_converter.dict_to_rdf(g, mapping)
932934
g.add(cast(TRIPLE, [subject, MAPPINGS_IRI, mapping_node]))
@@ -979,7 +981,7 @@ def _multivalue_from_rdf(self, value: Any, slot_name: str, dest: Dict[str, Any])
979981
dest[slot_name] = dest[slot_name] + "|" + value
980982

981983
@override
982-
def _init_dict_to_rdf(self, g: Graph, obj: Dict[str, Any]) -> Node:
984+
def _init_dict_to_rdf(self, g: Graph, obj: DICT_OR_SERIES) -> Node:
983985
if RECORD_ID in obj:
984986
return URIRef(self.ccp().expand(obj[RECORD_ID]))
985987
else:

tests/test_parsers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pathlib import Path
1111
from tempfile import TemporaryDirectory
1212
from textwrap import dedent
13-
from typing import Any, Dict, Union, get_args
13+
from typing import Any, Dict, Union, cast, get_args
1414
from xml.dom import minidom
1515

1616
import numpy as np
@@ -329,13 +329,13 @@ def test_parse_standard_and_old_style_rdf(self) -> None:
329329
CONFIDENCE: 0.7,
330330
MAPPING_DATE: date(2025, 10, 27),
331331
}
332-
assert_dict_contains(self, expected_values, msdf.df.loc[0])
332+
assert_dict_contains(self, expected_values, cast(pd.Series, msdf.df.loc[0]))
333333

334334
msdf = parse_sssom_rdf(f"{test_data_dir}/pre-standard-rdf.ttl")
335335
self.assertEqual(msdf.metadata[MAPPING_SET_ID], "https://example.org/sets/standard-rdf")
336336
self.assertEqual(msdf.metadata[LICENSE], "https://creativecommons.org/licenses/by/4.0/")
337337
self.assertEqual(msdf.metadata[PUBLICATION_DATE], date(2025, 10, 28))
338-
assert_dict_contains(self, expected_values, msdf.df.loc[0])
338+
assert_dict_contains(self, expected_values, cast(pd.Series, msdf.df.loc[0]))
339339

340340
def test_parse_rdf_with_record_id(self) -> None:
341341
"""Test parsing a RDF file containing record IDs as named resources."""

0 commit comments

Comments
 (0)