diff options
author | Iwan Aucamp <aucampia@gmail.com> | 2023-03-12 10:08:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-12 10:08:36 +0100 |
commit | e9a81ceb510ff5d16fd7e7e5e3eb0f52182d1f98 (patch) | |
tree | 92a00074b26d7ed34bb20edf239554c5d22b965c /rdflib/tools | |
parent | 09b90693ee519ba84e14f17a3a57d3d884c3082f (diff) | |
download | rdflib-e9a81ceb510ff5d16fd7e7e5e3eb0f52182d1f98.tar.gz |
feat: diverse type hints (#2264)
Add some small diverse type hints. Type hints make RDFLib safer to use
and change, as changes and usage can be validated using static
analysers like mypy.
This change does not have a runtime impact.
Diffstat (limited to 'rdflib/tools')
-rw-r--r-- | rdflib/tools/chunk_serializer.py | 3 | ||||
-rw-r--r-- | rdflib/tools/defined_namespace_creator.py | 34 |
2 files changed, 27 insertions, 10 deletions
diff --git a/rdflib/tools/chunk_serializer.py b/rdflib/tools/chunk_serializer.py index cb18d399..9f9f133a 100644 --- a/rdflib/tools/chunk_serializer.py +++ b/rdflib/tools/chunk_serializer.py @@ -98,7 +98,8 @@ def serialize_in_chunks( row_bytes = _nt_row(t).encode("utf-8") if len(row_bytes) > max_file_size: raise ValueError( - f"cannot write triple {t!r} as it's serialized size of {row_bytes / 1000} exceeds max_file_size_kb = {max_file_size_kb}" + # type error: Unsupported operand types for / ("bytes" and "int") + f"cannot write triple {t!r} as it's serialized size of {row_bytes / 1000} exceeds max_file_size_kb = {max_file_size_kb}" # type: ignore[operator] ) if i == 0: fp, fhb = xstack.enter_context(_start_new_file(file_no)) diff --git a/rdflib/tools/defined_namespace_creator.py b/rdflib/tools/defined_namespace_creator.py index 2cfe99f2..0c93ea75 100644 --- a/rdflib/tools/defined_namespace_creator.py +++ b/rdflib/tools/defined_namespace_creator.py @@ -1,3 +1,5 @@ +from __future__ import annotations + """ This rdflib Python script creates a DefinedNamespace Python file from a given RDF file @@ -14,20 +16,24 @@ import argparse import datetime import sys from pathlib import Path +from typing import TYPE_CHECKING, Iterable, List, Tuple sys.path.append(str(Path(__file__).parent.absolute().parent.parent)) -from rdflib import Graph -from rdflib.namespace import DCTERMS, OWL, RDFS, SKOS -from rdflib.util import guess_format +from rdflib.graph import Graph # noqa: E402 +from rdflib.namespace import DCTERMS, OWL, RDFS, SKOS # noqa: E402 +from rdflib.util import guess_format # noqa: E402 + +if TYPE_CHECKING: + from rdflib.query import ResultRow -def validate_namespace(namespace): +def validate_namespace(namespace: str) -> None: if not namespace.endswith(("/", "#")): raise ValueError("The supplied namespace must end with '/' or '#'") -def validate_object_id(object_id): +def validate_object_id(object_id: str) -> None: for c in object_id: if not c.isupper(): raise ValueError("The supplied object_id must be an all-capitals string") @@ -66,7 +72,9 @@ def validate_object_id(object_id): # return classes -def get_target_namespace_elements(g, target_namespace): +def get_target_namespace_elements( + g: Graph, target_namespace: str +) -> Tuple[List[Tuple[str, str]], List[str]]: namespaces = {"dcterms": DCTERMS, "owl": OWL, "rdfs": RDFS, "skos": SKOS} q = """ SELECT DISTINCT ?s ?def @@ -85,13 +93,15 @@ def get_target_namespace_elements(g, target_namespace): """.replace( "xxx", target_namespace ) - elements = [] + elements: List[Tuple[str, str]] = [] for r in g.query(q, initNs=namespaces): + if TYPE_CHECKING: + assert isinstance(r, ResultRow) elements.append((str(r[0]), str(r[1]))) elements.sort(key=lambda tup: tup[0]) - elements_strs = [] + elements_strs: List[str] = [] for e in elements: desc = e[1].replace("\n", " ") elements_strs.append( @@ -101,7 +111,13 @@ def get_target_namespace_elements(g, target_namespace): return elements, elements_strs -def make_dn_file(output_file_name, target_namespace, elements_strs, object_id, fail): +def make_dn_file( + output_file_name: Path, + target_namespace: str, + elements_strs: Iterable[str], + object_id: str, + fail: bool, +) -> None: header = f'''from rdflib.term import URIRef from rdflib.namespace import DefinedNamespace, Namespace |