Skip to content

Commit f99bcea

Browse files
committed
Add minimal type signatures to test scripts
An observed behavior is that mypy will not type signature analysis until one is added in the call path, e.g. designating `def main() -> None`. This patch is the minimal set of effects of adding a None return type to unit test functions. References: * [AC-211] Add static type checking to CASE-Utilities-Python Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
1 parent 9a582d7 commit f99bcea

File tree

7 files changed

+48
-42
lines changed

7 files changed

+48
-42
lines changed

tests/case_file/test_case_file.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,22 @@
3737

3838
SRCDIR = os.path.dirname(__file__)
3939

40-
def load_graph(filename):
40+
def load_graph(
41+
filename
42+
) -> rdflib.Graph:
4143
in_graph = rdflib.Graph()
4244
in_graph.parse(filename)
4345
return in_graph
4446

4547
@pytest.fixture
46-
def graph_case_file():
48+
def graph_case_file() -> rdflib.Graph:
4749
return load_graph(os.path.join(SRCDIR, "sample.txt.ttl"))
4850

4951
@pytest.fixture
50-
def graph_case_file_disable_hashes():
52+
def graph_case_file_disable_hashes() -> rdflib.Graph:
5153
return load_graph(os.path.join(SRCDIR, "sample.txt-disable_hashes.ttl"))
5254

53-
def test_confirm_hashes(graph_case_file):
55+
def test_confirm_hashes(graph_case_file) -> None:
5456
expected = {
5557
"MD5": "098F6BCD4621D373CADE4E832627B4F6",
5658
"SHA1": "A94A8FE5CCB19BA61C4C0873D391E987982FBBD3",
@@ -91,7 +93,7 @@ def test_confirm_hashes(graph_case_file):
9193

9294
assert expected == computed
9395

94-
def test_confirm_mtime(graph_case_file, graph_case_file_disable_hashes):
96+
def test_confirm_mtime(graph_case_file, graph_case_file_disable_hashes) -> None:
9597
query_confirm_mtime = """
9698
SELECT ?nFile
9799
WHERE {

tests/case_sparql_construct/test_case_sparql_construct.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
#
1212
# We would appreciate acknowledgement if the software is used.
1313

14+
import typing
15+
1416
import rdflib.plugins.sparql # type: ignore
1517

1618
import case_utils
1719

18-
def _test_templates_with_blank_nodes_result(filename):
20+
def _test_templates_with_blank_nodes_result(filename) -> None:
1921
ground_truth_positive = {
2022
("Alice", "Hacker"),
2123
("Bob", "Hacker")
2224
}
23-
ground_truth_negative = set()
25+
ground_truth_negative : typing.Set[str] = set()
2426

2527
graph = rdflib.Graph()
2628
graph.parse(filename)
@@ -48,7 +50,8 @@ def _test_templates_with_blank_nodes_result(filename):
4850
))
4951
assert computed == ground_truth_positive
5052

51-
def test_templates_with_blank_nodes_result_json():
53+
def test_templates_with_blank_nodes_result_json() -> None:
5254
_test_templates_with_blank_nodes_result("output.json")
53-
def test_templates_with_blank_nodes_result_turtle():
55+
56+
def test_templates_with_blank_nodes_result_turtle() -> None:
5457
_test_templates_with_blank_nodes_result("output.ttl")

tests/case_utils/test_guess_format.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,45 @@
2222
PATH_TO_XHTML = "/nonexistent/foo.xhtml"
2323
FMAP_XHTML_GRDDL = {"xhtml": "grddl"}
2424

25-
def test_rdflib_util_guess_format_xhtml_default():
25+
def test_rdflib_util_guess_format_xhtml_default() -> None:
2626
assert rdflib.util.guess_format(PATH_TO_XHTML) == "rdfa", "Failed to reproduce rdflib.util.guess_format test"
2727

28-
def test_rdflib_util_guess_format_xhtml_fmap():
28+
def test_rdflib_util_guess_format_xhtml_fmap() -> None:
2929
"""
3030
This test implements one of the documented demonstrations in rdflib.util.guess_format.
3131
"""
3232
assert rdflib.util.guess_format(PATH_TO_XHTML, FMAP_XHTML_GRDDL) == "grddl", "Failed to reproduce rdflib.util.guess_format test"
3333

34-
def test_rdflib_util_guess_format_ttl_default():
34+
def test_rdflib_util_guess_format_ttl_default() -> None:
3535
assert rdflib.util.guess_format(PATH_TO_TTL) == "turtle", "Failed to recognize .ttl RDF file extension"
3636

3737
@pytest.mark.xfail(reason="rdflib 5.0.0 guess_format fmap argument overwrites base module's extension map", strict=True)
38-
def test_rdflib_util_guess_format_ttl_fmap():
38+
def test_rdflib_util_guess_format_ttl_fmap() -> None:
3939
assert rdflib.util.guess_format(PATH_TO_TTL, FMAP_XHTML_GRDDL) == "turtle", "Failed to recognize .ttl RDF file extension when using fmap"
4040

41-
def test_rdflib_util_guess_format_json():
41+
def test_rdflib_util_guess_format_json() -> None:
4242
assert rdflib.util.guess_format(PATH_TO_JSON) == "json-ld", "Failed to recognize .json RDF file extension"
4343

44-
def test_rdflib_util_guess_format_jsonld():
44+
def test_rdflib_util_guess_format_jsonld() -> None:
4545
assert rdflib.util.guess_format(PATH_TO_JSONLD) == "json-ld", "Failed to recognize .jsonld RDF file extension"
4646

47-
def test_case_utils_guess_format_ttl_default():
47+
def test_case_utils_guess_format_ttl_default() -> None:
4848
assert case_utils.guess_format(PATH_TO_TTL) == "turtle", "Failed to recognize .ttl RDF file extension"
4949

5050
@pytest.mark.xfail(reason="Preserving behavior - rdflib 5.0.0 guess_format fmap argument overwrites base module's extension map", strict=True)
51-
def test_case_utils_guess_format_ttl_fmap():
51+
def test_case_utils_guess_format_ttl_fmap() -> None:
5252
assert case_utils.guess_format(PATH_TO_TTL, FMAP_XHTML_GRDDL) == "turtle", "Failed to recognize .ttl RDF file extension when using fmap"
5353

54-
def test_case_utils_guess_format_json_default():
54+
def test_case_utils_guess_format_json_default() -> None:
5555
assert case_utils.guess_format(PATH_TO_JSON) == "json-ld", "Failed to recognize .json RDF file extension"
5656

5757
@pytest.mark.xfail(reason="Preserving behavior - rdflib 5.0.0 guess_format fmap argument overwrites base module's extension map", strict=True)
58-
def test_case_utils_guess_format_json_fmap():
58+
def test_case_utils_guess_format_json_fmap() -> None:
5959
assert case_utils.guess_format(PATH_TO_JSON, FMAP_XHTML_GRDDL) == "json-ld", "Failed to recognize .json RDF file extension when using fmap"
6060

61-
def test_case_utils_guess_format_jsonld_default():
61+
def test_case_utils_guess_format_jsonld_default() -> None:
6262
assert case_utils.guess_format(PATH_TO_JSONLD) == "json-ld", "Failed to recognize .jsonld RDF file extension"
6363

6464
@pytest.mark.xfail(reason="Preserving behavior - rdflib 5.0.0 guess_format fmap argument overwrites base module's extension map", strict=True)
65-
def test_case_utils_guess_format_jsonld_fmap():
65+
def test_case_utils_guess_format_jsonld_fmap() -> None:
6666
assert case_utils.guess_format(PATH_TO_JSONLD, FMAP_XHTML_GRDDL) == "json-ld", "Failed to recognize .jsonld RDF file extension when using fmap"

tests/hexbinary/test_hexbinary.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
import logging
5050
import os
51+
import typing
5152

5253
import pytest
5354
import rdflib.plugins.sparql # type: ignore
@@ -64,7 +65,7 @@
6465
n_uppercase1 = rdflib.URIRef("urn:example:uppercase1")
6566
p_predicate = rdflib.URIRef("urn:example:predicate1")
6667

67-
def test_sparql_syntax_bind_boolean():
68+
def test_sparql_syntax_bind_boolean() -> None:
6869
"""
6970
This test serves as a syntax reminder for binding boolean values.
7071
"""
@@ -81,7 +82,7 @@ def test_sparql_syntax_bind_boolean():
8182
assert confirmed
8283

8384
@pytest.mark.xfail(reason="hard-coded failure")
84-
def test_pytest_syntax_xfail():
85+
def test_pytest_syntax_xfail() -> None:
8586
"""
8687
This test serves as a syntax reminder for the XFail decorator.
8788
"""
@@ -97,7 +98,7 @@ def test_pytest_syntax_xfail():
9798
confirmed = l_value.toPython()
9899
assert confirmed
99100

100-
def test_sparql_syntax_integer_coercion():
101+
def test_sparql_syntax_integer_coercion() -> None:
101102
"""
102103
This test serves as a syntax reminder for type coercions.
103104
"""
@@ -113,7 +114,7 @@ def test_sparql_syntax_integer_coercion():
113114
confirmed = l_value.toPython()
114115
assert confirmed
115116

116-
def test_sparql_syntax_integer_cast():
117+
def test_sparql_syntax_integer_cast() -> None:
117118
"""
118119
This test serves as a syntax reminder for the casting form of type coercions.
119120
"""
@@ -130,7 +131,7 @@ def test_sparql_syntax_integer_cast():
130131
assert confirmed
131132

132133
@pytest.mark.xfail
133-
def test_sparql_cast_custom_type():
134+
def test_sparql_cast_custom_type() -> None:
134135
"""
135136
This test checks for nonexistent literal-datatype assignments.
136137
"""
@@ -146,7 +147,7 @@ def test_sparql_cast_custom_type():
146147
confirmed = l_value.toPython()
147148
assert confirmed
148149

149-
def test_sparql_compare_hexbinary_mixcase():
150+
def test_sparql_compare_hexbinary_mixcase() -> None:
150151
confirmed = None
151152
graph = rdflib.Graph()
152153
for result in graph.query("""\
@@ -159,7 +160,7 @@ def test_sparql_compare_hexbinary_mixcase():
159160
confirmed = l_value.toPython()
160161
assert confirmed
161162

162-
def test_sparql_compare_hexbinary_matchcase():
163+
def test_sparql_compare_hexbinary_matchcase() -> None:
163164
confirmed = None
164165
graph = rdflib.Graph()
165166
for result in graph.query("""\
@@ -172,7 +173,7 @@ def test_sparql_compare_hexbinary_matchcase():
172173
confirmed = l_value.toPython()
173174
assert confirmed
174175

175-
def test_sparql_compare_hexbinarycanonical_matchcase():
176+
def test_sparql_compare_hexbinarycanonical_matchcase() -> None:
176177
confirmed = None
177178
graph = rdflib.Graph()
178179
for result in graph.query("""\
@@ -186,7 +187,7 @@ def test_sparql_compare_hexbinarycanonical_matchcase():
186187
assert confirmed
187188

188189
@pytest.mark.xfail
189-
def test_sparql_compare_hexbinarycanonical_mixcase():
190+
def test_sparql_compare_hexbinarycanonical_mixcase() -> None:
190191
"""
191192
This test shows hexBinaryCanonical does not induce a casing-insensitive comparison.
192193
"""
@@ -203,7 +204,7 @@ def test_sparql_compare_hexbinarycanonical_mixcase():
203204
assert confirmed
204205

205206
@pytest.mark.xfail
206-
def test_sparql_compare_hb_hbc_mixcase():
207+
def test_sparql_compare_hb_hbc_mixcase() -> None:
207208
"""
208209
This test confirms that literal-comparison takes into account datatype when one type is unknown.
209210
"""
@@ -220,7 +221,7 @@ def test_sparql_compare_hb_hbc_mixcase():
220221
assert confirmed
221222

222223
@pytest.mark.xfail
223-
def test_sparql_compare_hb_hbc_mixcase_cast():
224+
def test_sparql_compare_hb_hbc_mixcase_cast() -> None:
224225
"""
225226
This test is a bit redundant with test_sparql_cast_custom_type, but is here as an explicit demonstration of failure to cast a hexBinary value.
226227
"""
@@ -236,7 +237,7 @@ def test_sparql_compare_hb_hbc_mixcase_cast():
236237
confirmed = l_value.toPython()
237238
assert confirmed
238239

239-
def test_rdflib_literal_hexbinary():
240+
def test_rdflib_literal_hexbinary() -> None:
240241
_logger.debug("l_hb_lowercase = %r." % l_hb_lowercase)
241242
_logger.debug("l_hb_uppercase = %r." % l_hb_uppercase)
242243
_logger.debug("l_hb_lowercase.toPython() = %r." % l_hb_lowercase.toPython())
@@ -249,20 +250,20 @@ def test_rdflib_literal_hexbinary():
249250
assert l_hb_lowercase.toPython() == l_hb_uppercase.toPython()
250251

251252
@pytest.mark.xfail
252-
def test_rdflib_literal_hexbinarycanonical():
253+
def test_rdflib_literal_hexbinarycanonical() -> None:
253254
_logger.debug("l_hb_uppercase = %r." % l_hb_uppercase)
254255
_logger.debug("l_hbc_uppercase = %r." % l_hbc_uppercase)
255256

256257
assert l_hb_uppercase == l_hbc_uppercase
257258

258259
@pytest.mark.xfail
259-
def test_rdflib_literal_topython_hexbinarycanonical():
260+
def test_rdflib_literal_topython_hexbinarycanonical() -> None:
260261
_logger.debug("l_hb_lowercase.toPython() = %r." % l_hb_lowercase.toPython())
261262
_logger.debug("l_hb_uppercase.toPython() = %r." % l_hb_uppercase.toPython())
262263

263264
assert l_hb_uppercase.toPython() == l_hbc_uppercase.toPython()
264265

265-
def _query_all_value_matches(graph):
266+
def _query_all_value_matches(graph) -> typing.Set[str]:
266267
"""
267268
Return set of all node names (as strings) that have a matching value, where
268269
"matching" is determined by the SPARQL engine's type and data coercions.
@@ -280,7 +281,7 @@ def _query_all_value_matches(graph):
280281
computed.add(n_node2.toPython())
281282
return computed
282283

283-
def test_graph_repeat():
284+
def test_graph_repeat() -> None:
284285
"""
285286
Two nodes are given the same literal value, and are found to match on literal values.
286287
"""
@@ -302,7 +303,7 @@ def test_graph_repeat():
302303
computed = _query_all_value_matches(graph)
303304
assert computed == expected
304305

305-
def test_graph_all_hexbinary_literals():
306+
def test_graph_all_hexbinary_literals() -> None:
306307
"""
307308
Two nodes with the same literal value, and another node with the uppercase of the literal hexBinary value, are found to match on literal values.
308309
"""
@@ -333,7 +334,7 @@ def test_graph_all_hexbinary_literals():
333334
assert computed == expected
334335

335336
@pytest.mark.xfail
336-
def test_graph_hexbinarycanonical():
337+
def test_graph_hexbinarycanonical() -> None:
337338
graph = rdflib.Graph()
338339
graph.add((
339340
n_lowercase1,

tests/src/compact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
_logger = logging.getLogger(os.path.basename(__file__))
3030

31-
def main():
31+
def main() -> None:
3232
with open(args.out_json, "w") as out_fh:
3333
doc = None
3434
with open(args.in_json, "r") as in_fh:

tests/src/glom_graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import case_utils
2323

24-
def main():
24+
def main() -> None:
2525
g = rdflib.Graph()
2626
for in_graph in args.in_graph:
2727
g.parse(in_graph)

tests/src/isomorphic_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
_logger = logging.getLogger(os.path.basename(__file__))
4242

43-
def main():
43+
def main() -> None:
4444
parser = argparse.ArgumentParser()
4545
parser.add_argument("--debug", action="store_true")
4646
parser.add_argument("in_graph_1")

0 commit comments

Comments
 (0)