Skip to content

Commit 3a8e5be

Browse files
committed
major type rewrites
1 parent 90a8cf5 commit 3a8e5be

30 files changed

+429
-359
lines changed

cwltool/builder.py

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,33 @@
1414
Set,
1515
Tuple,
1616
Union,
17+
cast,
1718
)
1819

1920
from rdflib import Graph, URIRef
2021
from rdflib.namespace import OWL, RDFS
2122
from ruamel.yaml.comments import CommentedMap
22-
from typing_extensions import TYPE_CHECKING, Type # pylint: disable=unused-import
23-
2423
from schema_salad.avro.schema import Names, Schema, make_avsc_object
2524
from schema_salad.exceptions import ValidationException
2625
from schema_salad.sourceline import SourceLine
2726
from schema_salad.utils import convert_to_dict, json_dumps
2827
from schema_salad.validate import validate
28+
from typing_extensions import TYPE_CHECKING, Type # pylint: disable=unused-import
2929

3030
from . import expression
3131
from .errors import WorkflowException
3232
from .loghandler import _logger
3333
from .mutation import MutationManager
3434
from .pathmapper import CONTENT_LIMIT, get_listing, normalizeFilesDirs
3535
from .stdfsaccess import StdFsAccess
36-
from .utils import aslist, docker_windows_path_adjust, onWindows, visit_class
37-
38-
# move to a regular typing import when Python 3.3-3.6 is no longer supported
39-
36+
from .utils import (
37+
CWLObjectType,
38+
CWLOutputType,
39+
aslist,
40+
docker_windows_path_adjust,
41+
onWindows,
42+
visit_class,
43+
)
4044

4145
if TYPE_CHECKING:
4246
from .provenance import ProvenanceProfile # pylint: disable=unused-import
@@ -126,14 +130,14 @@ def check_format(
126130

127131

128132
class HasReqsHints(object):
129-
def __init__(self): # type: () -> None
133+
def __init__(self) -> None:
130134
"""Initialize this reqs decorator."""
131-
self.requirements = [] # type: List[Dict[str, Any]]
132-
self.hints = [] # type: List[Dict[str, Any]]
135+
self.requirements = [] # type: List[CWLObjectType]
136+
self.hints = [] # type: List[CWLObjectType]
133137

134138
def get_requirement(
135-
self, feature # type: str
136-
): # type: (...) -> Tuple[Optional[Any], Optional[bool]]
139+
self, feature: str
140+
) -> Tuple[Optional[CWLObjectType], Optional[bool]]:
137141
for item in reversed(self.requirements):
138142
if item["class"] == feature:
139143
return (item, True)
@@ -146,28 +150,28 @@ def get_requirement(
146150
class Builder(HasReqsHints):
147151
def __init__(
148152
self,
149-
job, # type: Dict[str, expression.JSON]
150-
files, # type: List[Dict[str, str]]
151-
bindings, # type: List[Dict[str, Any]]
152-
schemaDefs, # type: Dict[str, Dict[str, Any]]
153-
names, # type: Names
154-
requirements, # type: List[Dict[str, Any]]
155-
hints, # type: List[Dict[str, Any]]
156-
resources, # type: Dict[str, int]
153+
job: CWLObjectType,
154+
files: List[CWLObjectType],
155+
bindings: List[CWLObjectType],
156+
schemaDefs: Dict[str, CWLObjectType],
157+
names: Names,
158+
requirements: List[CWLObjectType],
159+
hints: List[CWLObjectType],
160+
resources: Dict[str, int],
157161
mutation_manager: Optional[MutationManager],
158-
formatgraph, # type: Optional[Graph]
162+
formatgraph: Optional[Graph],
159163
make_fs_access: Type[StdFsAccess],
160-
fs_access, # type: StdFsAccess
161-
job_script_provider, # type: Optional[Any]
162-
timeout, # type: float
163-
debug, # type: bool
164-
js_console, # type: bool
165-
force_docker_pull, # type: bool
166-
loadListing, # type: str
167-
outdir, # type: str
168-
tmpdir, # type: str
169-
stagedir, # type: str
170-
): # type: (...) -> None
164+
fs_access: StdFsAccess,
165+
job_script_provider: Optional[Any],
166+
timeout: float,
167+
debug: bool,
168+
js_console: bool,
169+
force_docker_pull: bool,
170+
loadListing: str,
171+
outdir: str,
172+
tmpdir: str,
173+
stagedir: str,
174+
) -> None:
171175
"""Initialize this Builder."""
172176
self.job = job
173177
self.files = files
@@ -360,7 +364,7 @@ def bind_input(
360364
)
361365
binding = {}
362366

363-
def _capture_files(f): # type: (Dict[str, str]) -> Dict[str, str]
367+
def _capture_files(f): # type: (CWLObjectType) -> CWLObjectType
364368
self.files.append(f)
365369
return f
366370

@@ -504,7 +508,7 @@ def tostr(self, value: Union[MutableMapping[str, str], Any]) -> str:
504508
else:
505509
return str(value)
506510

507-
def generate_arg(self, binding): # type: (Dict[str, Any]) -> List[str]
511+
def generate_arg(self, binding: CWLObjectType) -> List[str]:
508512
value = binding.get("datum")
509513
if "valueFrom" in binding:
510514
with SourceLine(
@@ -513,9 +517,9 @@ def generate_arg(self, binding): # type: (Dict[str, Any]) -> List[str]
513517
WorkflowException,
514518
_logger.isEnabledFor(logging.DEBUG),
515519
):
516-
value = self.do_eval(binding["valueFrom"], context=value)
520+
value = self.do_eval(cast(str, binding["valueFrom"]), context=value)
517521

518-
prefix = binding.get("prefix") # type: Optional[str]
522+
prefix = cast(Optional[str], binding.get("prefix"))
519523
sep = binding.get("separate", True)
520524
if prefix is None and not sep:
521525
with SourceLine(
@@ -528,13 +532,16 @@ def generate_arg(self, binding): # type: (Dict[str, Any]) -> List[str]
528532
"'separate' option can not be specified without prefix"
529533
)
530534

531-
argl = [] # type: MutableSequence[MutableMapping[str, str]]
535+
argl = [] # type: MutableSequence[CWLOutputType]
532536
if isinstance(value, MutableSequence):
533537
if binding.get("itemSeparator") and value:
534-
argl = [binding["itemSeparator"].join([self.tostr(v) for v in value])]
538+
itemSeparator = cast(str, binding["itemSeparator"])
539+
argl = [itemSeparator.join([self.tostr(v) for v in value])]
535540
elif binding.get("valueFrom"):
536541
value = [self.tostr(v) for v in value]
537-
return ([prefix] if prefix else []) + value
542+
return cast(List[str], ([prefix] if prefix else [])) + cast(
543+
List[str], value
544+
)
538545
elif prefix and value:
539546
return [prefix]
540547
else:
@@ -543,7 +550,7 @@ def generate_arg(self, binding): # type: (Dict[str, Any]) -> List[str]
543550
"File",
544551
"Directory",
545552
):
546-
argl = [value]
553+
argl = cast(MutableSequence[CWLOutputType], [value])
547554
elif isinstance(value, MutableMapping):
548555
return [prefix] if prefix else []
549556
elif value is True and prefix:
@@ -564,7 +571,9 @@ def generate_arg(self, binding): # type: (Dict[str, Any]) -> List[str]
564571

565572
def do_eval(
566573
self,
567-
ex: Union[MutableMapping[str, Any], MutableSequence[str], str],
574+
ex: Union[
575+
str, float, bool, None, MutableMapping[str, str], MutableSequence[str]
576+
],
568577
context: Optional[Any] = None,
569578
recursive: bool = False,
570579
strip_whitespace: bool = True,

0 commit comments

Comments
 (0)