diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8bf5f39..c952e8e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,14 @@ repos: -- repo: https://github.com/psf/black + - repo: https://github.com/psf/black rev: 22.3.0 hooks: - - id: black + - id: black + - repo: https://github.com/pycqa/flake8 + rev: 4.0.1 + hooks: + - id: flake8 + - repo: https://github.com/pycqa/isort + rev: 5.10.1 + hooks: + - id: isort + name: isort (python) diff --git a/case_utils/__init__.py b/case_utils/__init__.py index e8bc7ac..f4e6ddb 100644 --- a/case_utils/__init__.py +++ b/case_utils/__init__.py @@ -13,4 +13,4 @@ __version__ = "0.5.0" -from . import local_uuid +from . import local_uuid # noqa: F401 diff --git a/case_utils/case_file/__init__.py b/case_utils/case_file/__init__.py index 61e2bb7..a91038c 100644 --- a/case_utils/case_file/__init__.py +++ b/case_utils/case_file/__init__.py @@ -28,7 +28,14 @@ import rdflib # type: ignore import case_utils -from case_utils.namespace import * +from case_utils.namespace import ( + NS_RDF, + NS_UCO_CORE, + NS_UCO_OBSERVABLE, + NS_UCO_TYPES, + NS_UCO_VOCABULARY, + NS_XSD, +) DEFAULT_PREFIX = "http://example.org/kb/" @@ -147,7 +154,7 @@ def create_file_node( sha1obj.update(buf) sha256obj.update(buf) sha512obj.update(buf) - if not stashed_error is None: + if stashed_error is not None: raise stashed_error current_hashdict = HashDict( byte_tally, @@ -182,7 +189,7 @@ def create_file_node( # Add confirmed hashes into graph. for key in successful_hashdict._fields: - if not key in ("md5", "sha1", "sha256", "sha512"): + if key not in ("md5", "sha1", "sha256", "sha512"): continue n_hash = rdflib.BNode() graph.add((n_contentdata_facet, NS_UCO_OBSERVABLE.hash, n_hash)) @@ -247,7 +254,7 @@ def main() -> None: serialize_kwargs["context"] = context_dictionary node_iri = NS_BASE["file-" + case_utils.local_uuid.local_uuid()] - n_file = create_file_node( + create_file_node( graph, args.in_file, node_iri=node_iri, diff --git a/case_utils/case_sparql_construct/__init__.py b/case_utils/case_sparql_construct/__init__.py index 88c5877..d07ca8e 100644 --- a/case_utils/case_sparql_construct/__init__.py +++ b/case_utils/case_sparql_construct/__init__.py @@ -26,8 +26,10 @@ import rdflib.plugins.sparql # type: ignore import case_utils.ontology - -from case_utils.ontology.version_info import * +from case_utils.ontology.version_info import ( + CURRENT_CASE_VERSION, + built_version_choices_list, +) _logger = logging.getLogger(os.path.basename(__file__)) @@ -81,7 +83,7 @@ def main() -> None: construct_query_text = None with open(args.in_sparql, "r") as in_fh: construct_query_text = in_fh.read().strip() - assert not construct_query_text is None + assert construct_query_text is not None if "subClassOf" in construct_query_text: case_utils.ontology.load_subclass_hierarchy( diff --git a/case_utils/case_sparql_select/__init__.py b/case_utils/case_sparql_select/__init__.py index fcb58a0..d92b598 100644 --- a/case_utils/case_sparql_select/__init__.py +++ b/case_utils/case_sparql_select/__init__.py @@ -30,7 +30,6 @@ import argparse import binascii -import importlib.resources import logging import os import sys @@ -39,8 +38,10 @@ import rdflib.plugins.sparql # type: ignore import case_utils.ontology - -from case_utils.ontology.version_info import * +from case_utils.ontology.version_info import ( + CURRENT_CASE_VERSION, + built_version_choices_list, +) NS_XSD = rdflib.XSD diff --git a/case_utils/case_validate/__init__.py b/case_utils/case_validate/__init__.py index e59319b..3f47af4 100644 --- a/case_utils/case_validate/__init__.py +++ b/case_utils/case_validate/__init__.py @@ -35,16 +35,17 @@ import importlib.resources import logging import os -import pathlib import sys import typing -import rdflib.util # type: ignore import pyshacl # type: ignore +import rdflib.util # type: ignore import case_utils.ontology - -from case_utils.ontology.version_info import * +from case_utils.ontology.version_info import ( + CURRENT_CASE_VERSION, + built_version_choices_list, +) _logger = logging.getLogger(os.path.basename(__file__)) diff --git a/case_utils/local_uuid.py b/case_utils/local_uuid.py index dd1e6cb..f526191 100644 --- a/case_utils/local_uuid.py +++ b/case_utils/local_uuid.py @@ -22,8 +22,8 @@ import pathlib import sys import typing -import warnings import uuid +import warnings DEMO_UUID_BASE: typing.Optional[str] = None diff --git a/case_utils/ontology/__init__.py b/case_utils/ontology/__init__.py index 7773fda..6ae872e 100644 --- a/case_utils/ontology/__init__.py +++ b/case_utils/ontology/__init__.py @@ -22,7 +22,7 @@ # Yes, this next import is self-referential (/circular). But, it does work with importlib. import case_utils.ontology -from .version_info import * +from .version_info import CURRENT_CASE_VERSION _logger = logging.getLogger(os.path.basename(__file__)) diff --git a/setup.cfg b/setup.cfg index d8a7051..ecbe655 100644 --- a/setup.cfg +++ b/setup.cfg @@ -37,3 +37,14 @@ console_scripts = [options.package_data] case_utils = py.typed case_utils.ontology = *.ttl + +[flake8] +# https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#flake8 +extend-ignore = + E203 + E302 + E501 + +[isort] +# https://pycqa.github.io/isort/docs/configuration/black_compatibility.html +profile = black diff --git a/tests/case_utils/case_file/test_case_file.py b/tests/case_utils/case_file/test_case_file.py index 013f68d..4e53229 100644 --- a/tests/case_utils/case_file/test_case_file.py +++ b/tests/case_utils/case_file/test_case_file.py @@ -19,7 +19,7 @@ import rdflib.plugins.sparql # type: ignore import case_utils.ontology -from case_utils.namespace import * +from case_utils.namespace import NS_UCO_CORE, NS_UCO_OBSERVABLE, NS_UCO_TYPES _logger = logging.getLogger(os.path.basename(__file__)) @@ -117,12 +117,12 @@ def test_confirm_mtime( for result in graph_case_file_disable_hashes.query(query_object): (n_observable_object,) = result assert ( - not n_observable_object is None + n_observable_object is not None ), "File object with expected mtime not found in hashless graph." n_observable_object = None for result in graph_case_file.query(query_object): (n_observable_object,) = result assert ( - not n_observable_object is None + n_observable_object is not None ), "File object with expected mtime not found in fuller graph." diff --git a/tests/case_utils/case_sparql_construct/test_case_sparql_construct.py b/tests/case_utils/case_sparql_construct/test_case_sparql_construct.py index f217ff0..81d2f4e 100644 --- a/tests/case_utils/case_sparql_construct/test_case_sparql_construct.py +++ b/tests/case_utils/case_sparql_construct/test_case_sparql_construct.py @@ -15,8 +15,6 @@ import rdflib.plugins.sparql # type: ignore -import case_utils - def _test_subclass_templates_result(filename: str, expected: typing.Set[str]) -> None: computed: typing.Set[str] = set() @@ -38,8 +36,7 @@ def _test_subclass_templates_result(filename: str, expected: typing.Set[str]) -> def _test_w3_templates_with_blank_nodes_result(filename: str) -> None: - ground_truth_positive = {("Alice", "Hacker"), ("Bob", "Hacker")} - ground_truth_negative: typing.Set[str] = set() + expected = {("Alice", "Hacker"), ("Bob", "Hacker")} graph = rdflib.Graph() graph.parse(filename) @@ -59,7 +56,7 @@ def _test_w3_templates_with_blank_nodes_result(filename: str) -> None: for result in graph.query(query_string): (l_given_name, l_family_name) = result computed.add((l_given_name.toPython(), l_family_name.toPython())) - assert computed == ground_truth_positive + assert expected == computed def test_w3_templates_with_blank_nodes_result_json() -> None: diff --git a/tests/case_utils/case_validate/cli/test_format_output_flags.py b/tests/case_utils/case_validate/cli/test_format_output_flags.py index f64d300..a2fe8ce 100644 --- a/tests/case_utils/case_validate/cli/test_format_output_flags.py +++ b/tests/case_utils/case_validate/cli/test_format_output_flags.py @@ -81,7 +81,7 @@ def test_format_jsonld_output_jsonld() -> None: subject_file = "format_jsonld_output_jsonld.jsonld" asserted_format = _guess_format(subject_file) assert asserted_format == "json-ld" - graph = _parse_graph(subject_file, asserted_format) + _parse_graph(subject_file, asserted_format) @pytest.mark.xfail( @@ -107,7 +107,7 @@ def test_format_jsonld_output_unspecified() -> None: subject_file = "format_jsonld_output_unspecified.jsonld" asserted_format = _guess_format(subject_file) assert asserted_format == "json-ld" - graph = _parse_graph(subject_file, asserted_format) + _parse_graph(subject_file, asserted_format) @pytest.mark.xfail( @@ -124,7 +124,7 @@ def test_format_turtle_output_turtle() -> None: subject_file = "format_turtle_output_turtle.ttl" asserted_format = _guess_format(subject_file) assert asserted_format == "turtle" - graph = _parse_graph(subject_file, asserted_format) + _parse_graph(subject_file, asserted_format) def test_format_turtle_output_txt() -> None: @@ -138,7 +138,7 @@ def test_format_turtle_output_unspecified() -> None: subject_file = "format_turtle_output_unspecified.ttl" asserted_format = _guess_format(subject_file) assert asserted_format == "turtle" - graph = _parse_graph(subject_file, asserted_format) + _parse_graph(subject_file, asserted_format) @pytest.mark.xfail( diff --git a/tests/case_utils/ontology/test_version_info.py b/tests/case_utils/ontology/test_version_info.py index ae56470..949e170 100644 --- a/tests/case_utils/ontology/test_version_info.py +++ b/tests/case_utils/ontology/test_version_info.py @@ -18,8 +18,10 @@ import rdflib # type: ignore import case_utils.ontology - -from case_utils.ontology.version_info import * +from case_utils.ontology.version_info import ( + CURRENT_CASE_VERSION, + built_version_choices_list, +) NS_OWL = rdflib.OWL @@ -30,7 +32,7 @@ def test_built_version_choices_list() -> None: continue ttl_filename = built_version_choice + ".ttl" with importlib.resources.open_text(case_utils.ontology, ttl_filename) as tmp_fh: - pass + assert tmp_fh def test_case_ontology_version_info_versus_monolithic() -> None: @@ -49,7 +51,7 @@ def test_case_ontology_version_info_versus_monolithic() -> None: ) ): version_info = str(triple[2]) - assert not version_info is None, "Failed to retrieve owl:versionInfo" + assert version_info is not None, "Failed to retrieve owl:versionInfo" assert ( CURRENT_CASE_VERSION == version_info @@ -79,7 +81,7 @@ def test_case_ontology_version_info_versus_submodule() -> None: ) ): version_info = str(triple[2]) - assert not version_info is None, "Failed to retrieve owl:versionInfo" + assert version_info is not None, "Failed to retrieve owl:versionInfo" assert ( CURRENT_CASE_VERSION == version_info diff --git a/tests/case_utils/test_guess_format.py b/tests/case_utils/test_guess_format.py index d2c90d2..b84c07f 100644 --- a/tests/case_utils/test_guess_format.py +++ b/tests/case_utils/test_guess_format.py @@ -14,8 +14,6 @@ import pytest import rdflib # type: ignore -import case_utils - PATH_TO_TTL = "/nonexistent/foo.ttl" PATH_TO_JSON = "/nonexistent/foo.json" PATH_TO_JSONLD = "/nonexistent/foo.jsonld" diff --git a/tests/case_utils/test_local_uuid.py b/tests/case_utils/test_local_uuid.py index c60ed09..f283585 100644 --- a/tests/case_utils/test_local_uuid.py +++ b/tests/case_utils/test_local_uuid.py @@ -11,8 +11,6 @@ # # We would appreciate acknowledgement if the software is used. -import os - import pytest import case_utils.local_uuid diff --git a/tests/src/compact.py b/tests/src/compact.py index f20a3b8..edb56a3 100644 --- a/tests/src/compact.py +++ b/tests/src/compact.py @@ -20,9 +20,9 @@ __version__ = "0.2.0" +import json import logging import os -import json import typing import pyld # type: ignore @@ -35,7 +35,7 @@ def main() -> None: doc = None with open(args.in_json, "r") as in_fh: doc = json.load(in_fh) - assert not doc is None + assert doc is not None assert isinstance( doc, (dict, list) ), "JSON parsed top-level type assumption invalidated" @@ -46,7 +46,7 @@ def main() -> None: def _accrue_local_context(doc_object: typing.Dict[str, typing.Any]) -> None: local_context = doc_object.get("@context", dict()) for key in local_context.keys(): - if not key in total_context: + if key not in total_context: # Accrue new key. total_context[key] = local_context[key] diff --git a/tests/src/glom_graph.py b/tests/src/glom_graph.py index df0fa1e..5f3331d 100644 --- a/tests/src/glom_graph.py +++ b/tests/src/glom_graph.py @@ -19,8 +19,6 @@ import rdflib # type: ignore -import case_utils - def main() -> None: g = rdflib.Graph() diff --git a/tests/src/isomorphic_diff.py b/tests/src/isomorphic_diff.py index f4eab89..e369ce9 100644 --- a/tests/src/isomorphic_diff.py +++ b/tests/src/isomorphic_diff.py @@ -36,8 +36,6 @@ import rdflib.compare # type: ignore -import case_utils - _logger = logging.getLogger(os.path.basename(__file__))