Skip to content

Commit 22588db

Browse files
committed
mypyc prep
1 parent 7dec97b commit 22588db

23 files changed

+137
-155
lines changed

cwltool/builder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
from rdflib import Graph, URIRef
2020
from rdflib.namespace import OWL, RDFS
2121
from ruamel.yaml.comments import CommentedMap
22+
from typing_extensions import TYPE_CHECKING, Type # pylint: disable=unused-import
23+
2224
from schema_salad import validate
2325
from schema_salad.avro.schema import Schema, make_avsc_object
2426
from schema_salad.schema import Names, convert_to_dict
2527
from schema_salad.sourceline import SourceLine
2628
from schema_salad.utils import json_dumps
27-
from typing_extensions import TYPE_CHECKING, Type # pylint: disable=unused-import
2829

2930
from . import expression
3031
from .errors import WorkflowException

cwltool/command_line_tool.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
)
3030

3131
import shellescape
32+
from ruamel.yaml.comments import CommentedMap
33+
from typing_extensions import TYPE_CHECKING, Type
34+
3235
from schema_salad import validate
3336
from schema_salad.avro.schema import Schema
3437
from schema_salad.ref_resolver import file_uri, uri_file_path
3538
from schema_salad.sourceline import SourceLine
3639
from schema_salad.utils import json_dumps
37-
from typing_extensions import TYPE_CHECKING, Type
3840

3941
from .builder import Builder, content_limit_respected_read_bytes, substitute
4042
from .context import LoadingContext, RuntimeContext, getdefault
@@ -327,7 +329,7 @@ def check_valid_locations(fs_access: StdFsAccess, ob: Dict[str, Any]) -> None:
327329

328330
class CommandLineTool(Process):
329331
def __init__(
330-
self, toolpath_object: MutableMapping[str, Any], loadingContext: LoadingContext
332+
self, toolpath_object: CommentedMap, loadingContext: LoadingContext
331333
) -> None:
332334
"""Initialize this CommandLineTool."""
333335
super(CommandLineTool, self).__init__(toolpath_object, loadingContext)

cwltool/context.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
# move to a regular typing import when Python 3.3-3.6 is no longer supported
77
from ruamel.yaml.comments import CommentedMap
8-
from schema_salad import schema
9-
from schema_salad.ref_resolver import Loader
108
from typing_extensions import TYPE_CHECKING
119

10+
from schema_salad.ref_resolver import Loader
11+
from schema_salad.schema import Names
12+
1213
from .builder import Builder, HasReqsHints
1314
from .mutation import MutationManager
1415
from .pathmapper import PathMapper
@@ -35,9 +36,8 @@ def __init__(self, kwargs: Optional[Dict[str, Any]] = None) -> None:
3536

3637

3738
def make_tool_notimpl(
38-
toolpath_object, # type: MutableMapping[str, Any]
39-
loadingContext, # type: LoadingContext
40-
): # type: (...) -> Process
39+
toolpath_object: CommentedMap, loadingContext: "LoadingContext"
40+
) -> "Process":
4141
raise NotImplementedError()
4242

4343

@@ -53,7 +53,7 @@ def __init__(self, kwargs: Optional[Dict[str, Any]] = None) -> None:
5353
self.hints = None # type: Optional[List[Dict[str, Any]]]
5454
self.overrides_list = [] # type: List[Dict[str, Any]]
5555
self.loader = None # type: Optional[Loader]
56-
self.avsc_names = None # type: Optional[schema.Names]
56+
self.avsc_names = None # type: Optional[Names]
5757
self.disable_js_validation = False # type: bool
5858
self.js_hint_options_file = None
5959
self.do_validate = True # type: bool

cwltool/cwlrdf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from typing import IO, Any, Dict, MutableMapping, cast
33

44
from rdflib import Graph
5+
from ruamel.yaml.comments import CommentedMap
6+
57
from schema_salad.jsonld_context import makerdf
68
from schema_salad.ref_resolver import ContextType
79

@@ -11,7 +13,7 @@
1113
def gather(tool, ctx): # type: (Process, ContextType) -> Graph
1214
g = Graph()
1315

14-
def visitor(t): # type: (MutableMapping[str, Any]) -> None
16+
def visitor(t): # type: (CommentedMap) -> None
1517
makerdf(t["id"], t, ctx, graph=g)
1618

1719
tool.visit(visitor)

cwltool/executors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121

2222
import psutil
23+
2324
from schema_salad.sourceline import SourceLine
2425
from schema_salad.validate import ValidationException
2526

cwltool/job.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
import psutil
3535
import shellescape
3636
from prov.model import PROV
37+
from typing_extensions import TYPE_CHECKING
38+
3739
from schema_salad.sourceline import SourceLine
3840
from schema_salad.utils import json_dump, json_dumps
39-
from typing_extensions import TYPE_CHECKING
4041

4142
from .builder import Builder, HasReqsHints
4243
from .context import RuntimeContext, getdefault

cwltool/load_tool.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@
2121

2222
import requests.sessions
2323
from ruamel.yaml.comments import CommentedMap, CommentedSeq
24-
from schema_salad import schema
25-
from schema_salad.ref_resolver import ContextType, Fetcher, Loader, SubLoader, file_uri
24+
25+
from schema_salad.ref_resolver import (
26+
ContextType,
27+
Fetcher,
28+
FetcherCallableType,
29+
IdxResultType,
30+
Loader,
31+
ResolveType,
32+
file_uri,
33+
)
34+
from schema_salad.schema import validate_doc
2635
from schema_salad.sourceline import SourceLine, cmap
2736
from schema_salad.utils import json_dumps
2837
from schema_salad.validate import ValidationException
@@ -58,14 +67,11 @@
5867
} # type: ContextType
5968

6069

61-
FetcherConstructorType = Callable[
62-
[Dict[str, Union[str, bool]], requests.sessions.Session], Fetcher
63-
]
6470
ResolverType = Callable[[Loader, Union[str, Dict[str, Any]]], str]
6571

6672

6773
def default_loader(fetcher_constructor=None, enable_dev=False, doc_cache=True):
68-
# type: (Optional[FetcherConstructorType], bool, bool) -> Loader
74+
# type: (Optional[FetcherCallableType], bool, bool) -> Loader
6975
return Loader(
7076
jobloaderctx,
7177
fetcher_constructor=fetcher_constructor,
@@ -77,7 +83,7 @@ def default_loader(fetcher_constructor=None, enable_dev=False, doc_cache=True):
7783
def resolve_tool_uri(
7884
argsworkflow: str,
7985
resolver: Optional[ResolverType] = None,
80-
fetcher_constructor: Optional[FetcherConstructorType] = None,
86+
fetcher_constructor: Optional[FetcherCallableType] = None,
8187
document_loader: Optional[Loader] = None,
8288
) -> Tuple[str, str]:
8389

@@ -126,7 +132,7 @@ def fetch_document(
126132
resolver=loadingContext.resolver,
127133
document_loader=loadingContext.loader,
128134
)
129-
workflowobj = loadingContext.loader.fetch(fileuri)
135+
workflowobj = cast(CommentedMap, loadingContext.loader.fetch(fileuri))
130136
return loadingContext, workflowobj, uri
131137
if isinstance(argsworkflow, dict):
132138
uri = argsworkflow["id"] if argsworkflow.get("id") else "_:" + str(uuid.uuid4())
@@ -318,7 +324,7 @@ def resolve_and_validate_document(
318324
if isinstance(avsc_names, Exception):
319325
raise avsc_names
320326

321-
processobj = None # type: Union[CommentedMap, CommentedSeq, str, None]
327+
processobj = None # type: Optional[ResolveType]
322328
document_loader = Loader(
323329
sch_document_loader.ctx,
324330
schemagraph=sch_document_loader.graph,
@@ -359,9 +365,7 @@ def resolve_and_validate_document(
359365
return loadingContext, uri
360366

361367
if loadingContext.do_validate:
362-
schema.validate_doc(
363-
avsc_names, processobj, document_loader, loadingContext.strict
364-
)
368+
validate_doc(avsc_names, processobj, document_loader, loadingContext.strict)
365369

366370
# None means default behavior (do update)
367371
if loadingContext.do_update in (True, None):
@@ -436,7 +440,7 @@ def load_tool(
436440

437441

438442
def resolve_overrides(
439-
ov, # type: CommentedMap
443+
ov, # type: IdxResultType
440444
ov_uri, # type: str
441445
baseurl, # type: str
442446
): # type: (...) -> List[Dict[str, Any]]

cwltool/main.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import time
1313
import urllib
1414
from codecs import StreamWriter, getwriter
15-
from collections.abc import Iterable, MutableSequence, Sequence
15+
from collections.abc import Iterable, MutableMapping, MutableSequence, Sequence
1616
from typing import (
1717
IO,
1818
Any,
@@ -34,10 +34,11 @@
3434
import pkg_resources # part of setuptools
3535
from ruamel import yaml
3636
from ruamel.yaml.comments import CommentedMap, CommentedSeq
37-
from schema_salad import validate
37+
3838
from schema_salad.ref_resolver import Fetcher, Loader, file_uri, uri_file_path
39-
from schema_salad.sourceline import cmap, strip_dup_lineno
39+
from schema_salad.sourceline import strip_dup_lineno
4040
from schema_salad.utils import json_dumps
41+
from schema_salad.validate import ValidationException
4142

4243
from . import command_line_tool, workflow
4344
from .argparser import arg_parser, generate_parser, get_default_args
@@ -737,7 +738,7 @@ def choose_target(
737738
else:
738739
_logger.error("Can only use --target on Workflows")
739740
return None
740-
if isinstance(loadingContext.loader.idx, CommentedMap):
741+
if isinstance(loadingContext.loader.idx, MutableMapping):
741742
loadingContext.loader.idx[extracted["id"]] = extracted
742743
tool = make_tool(extracted["id"], loadingContext)
743744
else:
@@ -989,7 +990,7 @@ def main(
989990
)
990991
return 0
991992

992-
except (validate.ValidationException) as exc:
993+
except (ValidationException) as exc:
993994
_logger.error(
994995
"Tool definition failed validation:\n%s", str(exc), exc_info=args.debug
995996
)
@@ -1158,7 +1159,7 @@ def loc_to_path(obj): # type: (Dict[str, Any]) -> None
11581159
_logger.info("Final process status is %s", status)
11591160
return 0
11601161

1161-
except (validate.ValidationException) as exc:
1162+
except (ValidationException) as exc:
11621163
_logger.error(
11631164
"Input object failed validation:\n%s", str(exc), exc_info=args.debug
11641165
)

cwltool/pack.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
)
1717

1818
from ruamel.yaml.comments import CommentedMap, CommentedSeq
19-
from schema_salad.ref_resolver import Loader, SubLoader
20-
from schema_salad.sourceline import cmap
19+
20+
from schema_salad.ref_resolver import Loader, ResolveType, SubLoader
2121

2222
from .context import LoadingContext
2323
from .load_tool import fetch_document, resolve_and_validate_document
@@ -37,9 +37,7 @@ def flatten_deps(d, files): # type: (Any, Set[str]) -> None
3737
flatten_deps(d["listing"], files)
3838

3939

40-
LoadRefType = Callable[
41-
[Optional[str], str], Union[Dict[str, Any], List[Dict[str, Any]], str, None]
42-
]
40+
LoadRefType = Callable[[Optional[str], str], ResolveType]
4341

4442

4543
def find_run(
@@ -165,7 +163,7 @@ def pack(
165163
document_loader.idx[metadata["id"]] = CommentedMap(metadata.items())
166164

167165
def loadref(base, uri):
168-
# type: (Optional[str], str) -> Union[Dict[str, Any], List[Dict[str, Any]], str, None]
166+
# type: (Optional[str], str) -> ResolveType
169167
return document_loader.resolve_ref(uri, base_url=base)[0]
170168

171169
ids = set() # type: Set[str]

cwltool/pathmapper.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import requests
2424
from cachecontrol import CacheControl
2525
from cachecontrol.caches import FileCache
26-
from schema_salad import validate
26+
2727
from schema_salad.ref_resolver import uri_file_path
2828
from schema_salad.sourceline import SourceLine
29+
from schema_salad.validate import ValidationException
2930

3031
from .loghandler import _logger
3132
from .stdfsaccess import StdFsAccess, abspath
@@ -71,13 +72,13 @@ def normalizeFilesDirs(
7172
def addLocation(d): # type: (Dict[str, Any]) -> None
7273
if "location" not in d:
7374
if d["class"] == "File" and ("contents" not in d):
74-
raise validate.ValidationException(
75+
raise ValidationException(
7576
"Anonymous file object must have 'contents' and 'basename' fields."
7677
)
7778
if d["class"] == "Directory" and (
7879
"listing" not in d or "basename" not in d
7980
):
80-
raise validate.ValidationException(
81+
raise ValidationException(
8182
"Anonymous directory object must have 'listing' and 'basename' fields."
8283
)
8384
d["location"] = "_:" + str(uuid.uuid4())
@@ -89,7 +90,7 @@ def addLocation(d): # type: (Dict[str, Any]) -> None
8990
# strip trailing slash
9091
if path.endswith("/"):
9192
if d["class"] != "Directory":
92-
raise validate.ValidationException(
93+
raise ValidationException(
9394
"location '%s' ends with '/' but is not a Directory" % d["location"]
9495
)
9596
path = path.rstrip("/")
@@ -120,7 +121,7 @@ def addLocation(d): # type: (Dict[str, Any]) -> None
120121
contents = d.get("contents")
121122
if contents and len(contents) > CONTENT_LIMIT:
122123
if len(contents) > CONTENT_LIMIT:
123-
raise validate.ValidationException(
124+
raise ValidationException(
124125
"File object contains contents with number of bytes that exceeds CONTENT_LIMIT length (%d)"
125126
% CONTENT_LIMIT
126127
)
@@ -336,7 +337,7 @@ def visit(self, obj, stagedir, basedir, copy=False, staged=False):
336337
with SourceLine(
337338
obj,
338339
"location",
339-
validate.ValidationException,
340+
ValidationException,
340341
_logger.isEnabledFor(logging.DEBUG),
341342
):
342343
deref = ab

0 commit comments

Comments
 (0)