Skip to content

Commit cb64a53

Browse files
Merge pull request #13 from giannisdoukas/mypy
add mypy code test
2 parents a336476 + b367e66 commit cb64a53

File tree

7 files changed

+27
-19
lines changed

7 files changed

+27
-19
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ script:
1919
- pycodestyle --max-line-length=119 $(find ipython2cwl -name '*.py')
2020
- coverage run --source ipython2cwl -m unittest discover tests
2121
- coveralls
22+
- make mypy
2223
matrix:
2324
include:
2425
- name: "Python 3.7 on macOS 10.13"

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mypy:
2+
mypy $$(find ipython2cwl -name '*.py')

ipython2cwl/cwltoolextractor.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
from collections import namedtuple
88
from copy import deepcopy
99
from pathlib import Path
10-
from typing import Dict, Any, List
10+
from typing import Dict, Any, List, Tuple
1111

12-
import astor
13-
import nbconvert
12+
import astor # type: ignore
13+
import nbconvert # type: ignore
1414
import yaml
15-
from nbformat.notebooknode import NotebookNode
15+
from nbformat.notebooknode import NotebookNode # type: ignore
1616

1717
from .iotypes import CWLFilePathInput, CWLBooleanInput, CWLIntInput, CWLStringInput, CWLFilePathOutput, \
1818
CWLDumpableFile, CWLDumpableBinaryFile, CWLDumpable
@@ -30,7 +30,7 @@
3030

3131

3232
class AnnotatedVariablesExtractor(ast.NodeTransformer):
33-
input_type_mapper = {
33+
input_type_mapper: Dict[Tuple[str, ...], Tuple[str, str]] = {
3434
(CWLFilePathInput.__name__,): (
3535
'File',
3636
'pathlib.Path',
@@ -177,7 +177,7 @@ def visit_Import(self, node: ast.Import):
177177
return None
178178

179179
def visit_ImportFrom(self, node: ast.ImportFrom) -> Any:
180-
if node.module == 'ipython2cwl' or node.module.startswith('ipython2cwl.'):
180+
if node.module == 'ipython2cwl' or (node.module is not None and node.module.startswith('ipython2cwl.')):
181181
return None
182182
return node
183183

@@ -299,14 +299,18 @@ def compile(self, filename: Path = Path('notebookAsCWLTool.tar')) -> str:
299299
"""
300300
workdir = tempfile.mkdtemp()
301301
script_path = os.path.join(workdir, 'notebookTool')
302-
cwl_path = os.path.join(workdir, 'tool.cwl')
302+
cwl_path: str = os.path.join(workdir, 'tool.cwl')
303303
dockerfile_path = os.path.join(workdir, 'Dockerfile')
304304
setup_path = os.path.join(workdir, 'setup.py')
305305
requirements_path = os.path.join(workdir, 'requirements.txt')
306-
with open(script_path, 'wb') as f:
307-
f.write(self._wrap_script_to_method(self._tree, self._variables).encode())
308-
with open(cwl_path, 'w') as f:
309-
yaml.safe_dump(self.cwl_command_line_tool(), f, encoding='utf-8')
306+
with open(script_path, 'wb') as script_fd:
307+
script_fd.write(self._wrap_script_to_method(self._tree, self._variables).encode())
308+
with open(cwl_path, 'w') as cwl_fd:
309+
yaml.safe_dump(
310+
self.cwl_command_line_tool(),
311+
cwl_fd,
312+
encoding='utf-8'
313+
)
310314
dockerfile = DOCKERFILE_TEMPLATE.format(
311315
python_version=f'python:{".".join(platform.python_version_tuple())}'
312316
)

ipython2cwl/repo2cwl.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
from typing import List, Optional, Tuple, Dict
1010
from urllib.parse import urlparse, ParseResult
1111

12-
import git
13-
import nbformat
12+
import git # type: ignore
13+
import nbformat # type: ignore
1414
import yaml
1515
from git import Repo
16-
from repo2docker import Repo2Docker
16+
from repo2docker import Repo2Docker # type: ignore
1717

1818
from .cwltoolextractor import AnnotatedIPython2CWLToolConverter
1919

@@ -68,8 +68,8 @@ def _store_jn_as_script(notebook_path: str, git_directory_absolute_path: str, bi
6868
return tool, script_relative_path
6969

7070

71-
def existing_path(path: str):
72-
path = Path(path)
71+
def existing_path(path_str: str):
72+
path: Path = Path(path_str)
7373
if not path.is_dir():
7474
raise argparse.ArgumentTypeError(f'Directory: {str(path)} does not exists')
7575
return path
@@ -159,7 +159,7 @@ def _repo2cwl(git_directory_path: Repo) -> Tuple[str, List[Dict]]:
159159
bin_path,
160160
r2d.output_image_spec
161161
)
162-
if cwl_command_line_tool is None:
162+
if cwl_command_line_tool is None or script_name is None:
163163
continue
164164
cwl_command_line_tool['baseCommand'] = os.path.join('/app', 'cwl', 'bin', script_name)
165165
tools.append(cwl_command_line_tool)

ipython2cwl/requirements_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List
22

3-
from pip._internal.operations import freeze
3+
from pip._internal.operations import freeze # type: ignore
44

55

66
class RequirementsManager:

test-requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ gitpython>=3.1.3
66
docker>=4.2.1
77
git+https://github.com/giannisdoukas/cwltool.git#egg=cwltool
88
pandas==1.0.5
9+
mypy

tests/repo-like/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
PyYAML==5.3.1
1+
PyYAML==5.3.1

0 commit comments

Comments
 (0)