Skip to content

Commit 825a669

Browse files
Merge pull request #23 from casework/review_types_with_mypy
Add mypy to CI
2 parents 2fff550 + 92780a3 commit 825a669

File tree

7 files changed

+49
-37
lines changed

7 files changed

+49
-37
lines changed

case_gnu_time/__init__.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import datetime
2222
import logging
2323
import os
24+
import typing
2425

2526
import dateutil
2627
import dateutil.parser
27-
import rdflib
28+
import dateutil.relativedelta
29+
import rdflib.util
2830

2931
import case_utils
3032

@@ -36,7 +38,7 @@
3638
NS_XSD = rdflib.namespace.XSD
3739

3840
class ProcessUCOObject(object):
39-
def __init__(self, graph, ns_base, **kwargs):
41+
def __init__(self, graph, ns_base, **kwargs) -> None:
4042
"""
4143
Initializing a ProcessUCOObject will create one triple in the graph. To add data to the new node, call populate_from_gnu_time_log().
4244
"""
@@ -51,12 +53,12 @@ def __init__(self, graph, ns_base, **kwargs):
5153
self._node = rdflib.URIRef(ns_base[prefix_slug + case_utils.local_uuid.local_uuid()])
5254
self.graph.add((self.node, NS_RDF.type, NS_UCO_OBSERVABLE.Process))
5355

54-
self._bnode_process = None
55-
self._created_time = None
56-
self._exit_status = None
57-
self._exit_time = None
56+
self._bnode_process: typing.Optional[rdflib.BNode] = None
57+
self._created_time: typing.Optional[str] = None
58+
self._exit_status: typing.Optional[int] = None
59+
self._exit_time: typing.Optional[str] = None
5860

59-
def populate_from_gnu_time_log(self, gnu_time_log):
61+
def populate_from_gnu_time_log(self, gnu_time_log) -> None:
6062
"""
6163
This method populates Process data from a GNU Time log file. If self.exit_time is not set before this method is called, it will be set by reading the modification time of gnu_time_log.
6264
"""
@@ -106,7 +108,7 @@ def populate_from_gnu_time_log(self, gnu_time_log):
106108
self.created_time = created_time_datetime.isoformat()
107109

108110
@property
109-
def bnode_process(self):
111+
def bnode_process(self) -> rdflib.BNode:
110112
"""
111113
Created on first access.
112114
"""
@@ -117,11 +119,11 @@ def bnode_process(self):
117119
return self._bnode_process
118120

119121
@property
120-
def created_time(self):
122+
def created_time(self) -> typing.Optional[str]:
121123
return self._created_time
122124

123125
@created_time.setter
124-
def created_time(self, value):
126+
def created_time(self, value: str) -> None:
125127
"""
126128
Only set once.
127129
"""
@@ -132,24 +134,22 @@ def created_time(self, value):
132134
check_value = dateutil.parser.isoparse(str_value)
133135
self.graph.add((self.bnode_process, NS_UCO_OBSERVABLE.observableCreatedTime, rdflib.Literal(str_value, datatype=NS_XSD.dateTime)))
134136
self._created_time = value
135-
return self._created_time
136137

137138
@property
138-
def exit_status(self):
139+
def exit_status(self) -> typing.Optional[int]:
139140
return self._exit_status
140141

141142
@exit_status.setter
142-
def exit_status(self, value):
143+
def exit_status(self, value: int) -> None:
143144
assert isinstance(value, int)
144145
self.graph.add((self.bnode_process, NS_UCO_OBSERVABLE.exitStatus, rdflib.Literal(value)))
145-
return self._exit_status
146146

147147
@property
148-
def exit_time(self):
148+
def exit_time(self) -> typing.Optional[str]:
149149
return self._exit_time
150150

151151
@exit_time.setter
152-
def exit_time(self, value):
152+
def exit_time(self, value: str) -> None:
153153
"""
154154
Only set once.
155155
"""
@@ -161,16 +161,15 @@ def exit_time(self, value):
161161
literal_time = rdflib.Literal(str_value, datatype=NS_XSD.dateTime)
162162
self.graph.add((self.bnode_process, NS_UCO_OBSERVABLE.exitTime, literal_time))
163163
self._exit_time = value
164-
return self._exit_time
165164

166165
@property
167-
def node(self):
166+
def node(self) -> rdflib.URIRef:
168167
"""
169168
Read-only property.
170169
"""
171170
return self._node
172171

173-
def build_process_object(graph, ns_base, gnu_time_log, mtime=None, prefix_slug=None):
172+
def build_process_object(graph, ns_base, gnu_time_log, mtime=None, prefix_slug=None) -> ProcessUCOObject:
174173
"""
175174
This function builds a Process UCO Object from a file that contains the output of GNU Time's --verbose flag.
176175
@@ -204,7 +203,7 @@ def build_process_object(graph, ns_base, gnu_time_log, mtime=None, prefix_slug=N
204203
argument_parser.add_argument("gnu_time_log", help="A file recording the output of the process wrapper GNU time, with the --verbose flag (recorded with the --output flag). Used to retrieve exit status, conclusion time (if --done-log not provided), and run length.")
205204
argument_parser.add_argument("out_graph", help="A self-contained RDF graph file, in the format either requested by --output-format or guessed based on extension.")
206205

207-
def main():
206+
def main() -> None:
208207
args = argument_parser.parse_args()
209208
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
210209

@@ -221,9 +220,9 @@ def main():
221220
process_object = build_process_object(graph, NS_BASE, args.gnu_time_log, mtime_str)
222221

223222
#_logger.debug("args.output_format = %r." % args.output_format)
224-
output_format = args.output_format or case_utils.guess_format(args.out_graph)
223+
output_format = args.output_format or rdflib.util.guess_format(args.out_graph)
225224

226-
graph.serialize(destination=args.out_graph, format=output_format)
225+
graph.serialize(destination=args.out_graph)
227226

228227
if __name__ == "__main__":
229228
main()

tests/Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ all: \
3535
check-from_pip \
3636
check-gtime_log \
3737
check-gtime_and_done_log \
38+
check-mypy \
3839
download
3940

4041
.venv.done.log: \
@@ -122,7 +123,8 @@ check: \
122123
check-gtime_log \
123124
check-gtime_and_done_log \
124125
check-from_pip \
125-
check-as_import
126+
check-as_import \
127+
check-mypy
126128

127129
check-as_import: \
128130
all-as_import
@@ -148,6 +150,16 @@ check-gtime_and_done_log: \
148150
--directory gtime_and_done_log \
149151
check
150152

153+
check-mypy: \
154+
.venv.done.log
155+
source venv/bin/activate \
156+
&& mypy \
157+
$(top_srcdir)/case_gnu_time \
158+
as_import \
159+
from_pip \
160+
gtime_and_done_log \
161+
gtime_log
162+
151163
clean:
152164
@$(MAKE) \
153165
--directory gtime_log \

tests/as_import/test_as_import.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222

2323
srcdir = os.path.dirname(__file__)
2424

25-
def _parse_graph(filename):
25+
def _parse_graph(filename) -> rdflib.Graph:
2626
graph_filename = os.path.join(srcdir, filename)
2727
graph = rdflib.Graph()
2828
graph.parse(graph_filename)
2929
case_utils.ontology.load_subclass_hierarchy(graph)
3030
return graph
3131

32-
def _test_as_import_load(graph_filename):
32+
def _test_as_import_load(graph_filename) -> None:
3333
graph = _parse_graph(graph_filename)
3434
assert len(graph) > 0
3535

36-
def _test_as_import_query(graph_filename):
36+
def _test_as_import_query(graph_filename) -> None:
3737
graph = _parse_graph(graph_filename)
3838

3939
nsdict = {k:v for (k,v) in graph.namespace_manager.namespaces()}
@@ -66,19 +66,19 @@ def _test_as_import_query(graph_filename):
6666
iris.add(n_process_object.toPython())
6767
assert len(iris) == 1
6868

69-
def test_as_import_load_json():
69+
def test_as_import_load_json() -> None:
7070
_test_as_import_load("process.json")
7171

72-
def test_as_import_load_turtle():
72+
def test_as_import_load_turtle() -> None:
7373
_test_as_import_load("process.ttl")
7474

75-
def test_as_import_query_json():
75+
def test_as_import_query_json() -> None:
7676
_test_as_import_query("process.json")
7777

78-
def test_as_import_query_turtle():
78+
def test_as_import_query_turtle() -> None:
7979
_test_as_import_query("process.ttl")
8080

81-
def test_as_import_validation():
81+
def test_as_import_validation() -> None:
8282
graph = _parse_graph("validation.ttl")
8383
result = None
8484
for triple in graph.triples((None, rdflib.SH.conforms, None)):

tests/from_pip/test_from_pip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
srcdir = os.path.dirname(__file__)
1919

20-
def _parse_graph(filename):
20+
def _parse_graph(filename) -> rdflib.Graph:
2121
graph_filename = os.path.join(srcdir, filename)
2222
graph = rdflib.Graph()
2323
graph.parse(graph_filename)
2424
return graph
2525

26-
def test_from_pip_validation():
26+
def test_from_pip_validation() -> None:
2727
graph = _parse_graph("validation.ttl")
2828
result = None
2929
for triple in graph.triples((None, rdflib.SH.conforms, None)):

tests/gtime_and_done_log/test_gtime_and_done_log.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
srcdir = os.path.dirname(__file__)
1919

20-
def _parse_graph(filename):
20+
def _parse_graph(filename) -> rdflib.Graph:
2121
graph_filename = os.path.join(srcdir, filename)
2222
graph = rdflib.Graph()
2323
graph.parse(graph_filename)
2424
return graph
2525

26-
def test_gtime_and_done_log_validation():
26+
def test_gtime_and_done_log_validation() -> None:
2727
graph = _parse_graph("validation.ttl")
2828
result = None
2929
for triple in graph.triples((None, rdflib.SH.conforms, None)):

tests/gtime_log/test_gtime_log.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
srcdir = os.path.dirname(__file__)
1919

20-
def _parse_graph(filename):
20+
def _parse_graph(filename) -> rdflib.Graph:
2121
graph_filename = os.path.join(srcdir, filename)
2222
graph = rdflib.Graph()
2323
graph.parse(graph_filename)
2424
return graph
2525

26-
def test_gtime_log_validation():
26+
def test_gtime_log_validation() -> None:
2727
graph = _parse_graph("validation.ttl")
2828
result = None
2929
for triple in graph.triples((None, rdflib.SH.conforms, None)):

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
case_utils
2+
mypy
23
python-dateutil

0 commit comments

Comments
 (0)