diff --git a/.travis.yml b/.travis.yml index 9b05705..a060130 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,7 @@ script: - pycodestyle --max-line-length=119 $(find ipython2cwl -name '*.py') - coverage run --source ipython2cwl -m unittest discover tests - coveralls + - make mypy matrix: include: - name: "Python 3.7 on macOS 10.13" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b920674 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +mypy: + mypy $$(find ipython2cwl -name '*.py') diff --git a/ipython2cwl/cwltoolextractor.py b/ipython2cwl/cwltoolextractor.py index 61ac743..573ac94 100644 --- a/ipython2cwl/cwltoolextractor.py +++ b/ipython2cwl/cwltoolextractor.py @@ -7,12 +7,12 @@ from collections import namedtuple from copy import deepcopy from pathlib import Path -from typing import Dict, Any, List +from typing import Dict, Any, List, Tuple -import astor -import nbconvert +import astor # type: ignore +import nbconvert # type: ignore import yaml -from nbformat.notebooknode import NotebookNode +from nbformat.notebooknode import NotebookNode # type: ignore from .iotypes import CWLFilePathInput, CWLBooleanInput, CWLIntInput, CWLStringInput, CWLFilePathOutput, \ CWLDumpableFile, CWLDumpableBinaryFile, CWLDumpable @@ -30,7 +30,7 @@ class AnnotatedVariablesExtractor(ast.NodeTransformer): - input_type_mapper = { + input_type_mapper: Dict[Tuple[str, ...], Tuple[str, str]] = { (CWLFilePathInput.__name__,): ( 'File', 'pathlib.Path', @@ -177,7 +177,7 @@ def visit_Import(self, node: ast.Import): return None def visit_ImportFrom(self, node: ast.ImportFrom) -> Any: - if node.module == 'ipython2cwl' or node.module.startswith('ipython2cwl.'): + if node.module == 'ipython2cwl' or (node.module is not None and node.module.startswith('ipython2cwl.')): return None return node @@ -299,14 +299,18 @@ def compile(self, filename: Path = Path('notebookAsCWLTool.tar')) -> str: """ workdir = tempfile.mkdtemp() script_path = os.path.join(workdir, 'notebookTool') - cwl_path = os.path.join(workdir, 'tool.cwl') + cwl_path: str = os.path.join(workdir, 'tool.cwl') dockerfile_path = os.path.join(workdir, 'Dockerfile') setup_path = os.path.join(workdir, 'setup.py') requirements_path = os.path.join(workdir, 'requirements.txt') - with open(script_path, 'wb') as f: - f.write(self._wrap_script_to_method(self._tree, self._variables).encode()) - with open(cwl_path, 'w') as f: - yaml.safe_dump(self.cwl_command_line_tool(), f, encoding='utf-8') + with open(script_path, 'wb') as script_fd: + script_fd.write(self._wrap_script_to_method(self._tree, self._variables).encode()) + with open(cwl_path, 'w') as cwl_fd: + yaml.safe_dump( + self.cwl_command_line_tool(), + cwl_fd, + encoding='utf-8' + ) dockerfile = DOCKERFILE_TEMPLATE.format( python_version=f'python:{".".join(platform.python_version_tuple())}' ) diff --git a/ipython2cwl/repo2cwl.py b/ipython2cwl/repo2cwl.py index 3a9ab37..417a082 100644 --- a/ipython2cwl/repo2cwl.py +++ b/ipython2cwl/repo2cwl.py @@ -9,11 +9,11 @@ from typing import List, Optional, Tuple, Dict from urllib.parse import urlparse, ParseResult -import git -import nbformat +import git # type: ignore +import nbformat # type: ignore import yaml from git import Repo -from repo2docker import Repo2Docker +from repo2docker import Repo2Docker # type: ignore from .cwltoolextractor import AnnotatedIPython2CWLToolConverter @@ -68,8 +68,8 @@ def _store_jn_as_script(notebook_path: str, git_directory_absolute_path: str, bi return tool, script_relative_path -def existing_path(path: str): - path = Path(path) +def existing_path(path_str: str): + path: Path = Path(path_str) if not path.is_dir(): raise argparse.ArgumentTypeError(f'Directory: {str(path)} does not exists') return path @@ -159,7 +159,7 @@ def _repo2cwl(git_directory_path: Repo) -> Tuple[str, List[Dict]]: bin_path, r2d.output_image_spec ) - if cwl_command_line_tool is None: + if cwl_command_line_tool is None or script_name is None: continue cwl_command_line_tool['baseCommand'] = os.path.join('/app', 'cwl', 'bin', script_name) tools.append(cwl_command_line_tool) diff --git a/ipython2cwl/requirements_manager.py b/ipython2cwl/requirements_manager.py index 1e828c5..1dee5a7 100644 --- a/ipython2cwl/requirements_manager.py +++ b/ipython2cwl/requirements_manager.py @@ -1,6 +1,6 @@ from typing import List -from pip._internal.operations import freeze +from pip._internal.operations import freeze # type: ignore class RequirementsManager: diff --git a/test-requirements.txt b/test-requirements.txt index 5896fc7..34e0730 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,3 +6,4 @@ gitpython>=3.1.3 docker>=4.2.1 git+https://github.com/giannisdoukas/cwltool.git#egg=cwltool pandas==1.0.5 +mypy diff --git a/tests/repo-like/requirements.txt b/tests/repo-like/requirements.txt index 3101697..7a997b5 100644 --- a/tests/repo-like/requirements.txt +++ b/tests/repo-like/requirements.txt @@ -1 +1 @@ -PyYAML==5.3.1 \ No newline at end of file +PyYAML==5.3.1