Skip to content

Commit a5a1b85

Browse files
committed
improve type hints
1 parent 3cb3d42 commit a5a1b85

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

cwltool/context.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
import shutil
55
import tempfile
66
import threading
7-
from typing import IO, Any, Callable, Dict, Iterable, List, Optional, TextIO, Union
7+
from typing import (
8+
IO,
9+
Any,
10+
Callable,
11+
Dict,
12+
Iterable,
13+
List,
14+
Optional,
15+
TextIO,
16+
Tuple,
17+
Union,
18+
)
819

920
# move to a regular typing import when Python 3.3-3.6 is no longer supported
1021
from ruamel.yaml.comments import CommentedMap
@@ -23,6 +34,8 @@
2334
from .utils import DEFAULT_TMP_PREFIX, CWLObjectType, HasReqsHints, ResolverType
2435

2536
if TYPE_CHECKING:
37+
from cwl_utils.parser.cwl_v1_2 import LoadingOptions
38+
2639
from .process import Process
2740
from .provenance import ResearchObject # pylint: disable=unused-import
2841
from .provenance_profile import ProvenanceProfile
@@ -102,8 +115,8 @@ def __init__(self, kwargs: Optional[Dict[str, Any]] = None) -> None:
102115
self.relax_path_checks = False # type: bool
103116
self.singularity = False # type: bool
104117
self.podman = False # type: bool
105-
self.eval_timeout = 60 # type: float
106-
self.codegen_idx = {}
118+
self.eval_timeout: float = 60
119+
self.codegen_idx: Dict[str, Tuple[Any, "LoadingOptions"]] = {}
107120
self.fast_validator = False
108121

109122
super().__init__(kwargs)

cwltool/load_tool.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
cast,
2020
)
2121

22+
from cwl_utils.parser import cwl_v1_2, cwl_v1_2_utils
2223
from ruamel.yaml.comments import CommentedMap, CommentedSeq
2324
from schema_salad.exceptions import ValidationException
2425
from schema_salad.ref_resolver import Loader, file_uri
@@ -40,9 +41,6 @@
4041
from .update import ALLUPDATES
4142
from .utils import CWLObjectType, ResolverType, visit_class
4243

43-
import cwl_utils.parser.cwl_v1_2
44-
import cwl_utils.parser.cwl_v1_2_utils
45-
4644
jobloaderctx = {
4745
"cwl": "https://w3id.org/cwl/cwl#",
4846
"cwltool": "http://commonwl.org/cwltool#",
@@ -266,30 +264,43 @@ def _add_blank_ids(
266264
)
267265
)
268266

269-
def _fast_validator_convert_stdstreams_to_files(processobj) -> None:
270-
if isinstance(processobj, cwl_utils.parser.cwl_v1_2.CommandLineTool):
271-
cwl_utils.parser.cwl_v1_2_utils.convert_stdstreams_to_files(processobj)
272-
elif isinstance(processobj, cwl_utils.parser.cwl_v1_2.Workflow):
267+
268+
def _fast_validator_convert_stdstreams_to_files(
269+
processobj: Union[cwl_v1_2.Process, MutableSequence[cwl_v1_2.Process]]
270+
) -> None:
271+
if isinstance(processobj, cwl_v1_2.CommandLineTool):
272+
cwl_v1_2_utils.convert_stdstreams_to_files(processobj)
273+
elif isinstance(processobj, cwl_v1_2.Workflow):
273274
for st in processobj.steps:
274275
_fast_validator_convert_stdstreams_to_files(st.run)
275276
elif isinstance(processobj, MutableSequence):
276277
for p in processobj:
277278
_fast_validator_convert_stdstreams_to_files(p)
278279

279280

280-
def fast_validator(workflowobj, fileuri, uri, loadingContext: LoadingContext):
281-
lopt = cwl_utils.parser.cwl_v1_2.LoadingOptions(idx=loadingContext.codegen_idx, fileuri=fileuri)
281+
def fast_validator(
282+
workflowobj: Union[CommentedMap, CommentedSeq, None],
283+
fileuri: Optional[str],
284+
uri: str,
285+
loadingContext: LoadingContext,
286+
) -> Tuple[Union[CommentedMap, CommentedSeq], CommentedMap]:
287+
lopt = cwl_v1_2.LoadingOptions(idx=loadingContext.codegen_idx, fileuri=fileuri)
282288

283289
if uri not in loadingContext.codegen_idx:
284-
cwl_utils.parser.cwl_v1_2.load_document_with_metadata(workflowobj, fileuri, loadingOptions=lopt, addl_metadata_fields=("id", "cwlVersion"))
290+
cwl_v1_2.load_document_with_metadata(
291+
workflowobj,
292+
fileuri,
293+
loadingOptions=lopt,
294+
addl_metadata_fields=["id", "cwlVersion"],
295+
)
285296

286297
objects, loadopt = loadingContext.codegen_idx[uri]
287298

288299
_fast_validator_convert_stdstreams_to_files(objects)
289300

290-
processobj = cwl_utils.parser.cwl_v1_2.save(objects, relative_uris=False)
301+
processobj = cwl_v1_2.save(objects, relative_uris=False)
291302

292-
metadata = {}
303+
metadata: Dict[str, Any] = {}
293304
metadata["id"] = loadopt.fileuri
294305

295306
if loadopt.namespaces:
@@ -298,15 +309,19 @@ def fast_validator(workflowobj, fileuri, uri, loadingContext: LoadingContext):
298309
metadata["$schemas"] = loadopt.schemas
299310
if loadopt.baseuri:
300311
metadata["$base"] = loadopt.baseuri
301-
for k,v in loadopt.addl_metadata.items():
312+
for k, v in loadopt.addl_metadata.items():
302313
if isinstance(processobj, MutableMapping) and k in processobj:
303314
metadata[k] = processobj[k]
304315
else:
305316
metadata[k] = v
306317

307-
loadingContext.loader.graph += loadopt.graph
318+
if loadingContext.loader:
319+
loadingContext.loader.graph += loadopt.graph
320+
321+
return cast(Union[CommentedMap, CommentedSeq], cmap(processobj)), cast(
322+
CommentedMap, cmap(metadata)
323+
)
308324

309-
return cmap(processobj), cmap(metadata)
310325

311326
def resolve_and_validate_document(
312327
loadingContext: LoadingContext,
@@ -500,7 +515,7 @@ def make_tool(
500515
if loadingContext.loader is None:
501516
raise ValueError("loadingContext must have a loader")
502517

503-
if loadingContext.fast_validator:
518+
if loadingContext.fast_validator and isinstance(uri, str):
504519
resolveduri, metadata = fast_validator(None, None, uri, loadingContext)
505520
else:
506521
resolveduri, metadata = loadingContext.loader.resolve_ref(uri)

0 commit comments

Comments
 (0)