Skip to content

Commit 168678d

Browse files
Apply IsType optimizations
1 parent 8920b7d commit 168678d

File tree

4 files changed

+21
-27
lines changed

4 files changed

+21
-27
lines changed

cwltool/command_line_tool.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
import urllib.parse
1515
from collections.abc import (
1616
Generator,
17-
Iterable,
1817
Mapping,
1918
MutableMapping,
2019
MutableSequence,
2120
)
2221
from enum import Enum
2322
from functools import cmp_to_key, partial
2423
from re import Pattern
25-
from typing import TYPE_CHECKING, Any, Optional, TextIO, Union, cast
24+
from typing import Any, Optional, TYPE_CHECKING, TextIO, Union, cast
2625

2726
from cwl_utils.types import (
2827
CWLDirectoryType,
@@ -42,12 +41,7 @@
4241
from schema_salad.utils import json_dumps
4342
from schema_salad.validate import validate_ex
4443

45-
from .builder import (
46-
INPUT_OBJ_VOCAB,
47-
Builder,
48-
content_limit_respected_read_bytes,
49-
substitute,
50-
)
44+
from .builder import Builder, INPUT_OBJ_VOCAB, content_limit_respected_read_bytes, substitute
5145
from .context import LoadingContext, RuntimeContext, getdefault
5246
from .docker import DockerCommandLineJob, PodmanCommandLineJob
5347
from .errors import UnsupportedRequirement, WorkflowException
@@ -1292,7 +1286,7 @@ def collect_output(
12921286
fs_access: StdFsAccess,
12931287
compute_checksum: bool = True,
12941288
) -> CWLOutputType | None:
1295-
r: list[CWLOutputType] = []
1289+
r: MutableSequence[CWLFileType | CWLDirectoryType] = []
12961290
empty_and_optional = False
12971291
debug = _logger.isEnabledFor(logging.DEBUG)
12981292
result: CWLOutputType | None = None
@@ -1340,9 +1334,9 @@ def collect_output(
13401334
key=cmp_to_key(locale.strcoll),
13411335
)
13421336
r.extend(
1343-
cast(
1344-
Iterable[CWLOutputType],
1345-
[
1337+
[
1338+
cast(
1339+
CWLFileType | CWLDirectoryType,
13461340
{
13471341
"location": g,
13481342
"path": fs_access.join(
@@ -1353,31 +1347,31 @@ def collect_output(
13531347
"nameroot": os.path.splitext(decoded_basename)[0],
13541348
"nameext": os.path.splitext(decoded_basename)[1],
13551349
"class": "File" if fs_access.isfile(g) else "Directory",
1356-
}
1357-
for g, decoded_basename in zip(
1350+
},
1351+
)
1352+
for g, decoded_basename in zip(
1353+
sorted_glob_result,
1354+
map(
1355+
lambda x: os.path.basename(urllib.parse.unquote(x)),
13581356
sorted_glob_result,
1359-
map(
1360-
lambda x: os.path.basename(urllib.parse.unquote(x)),
1361-
sorted_glob_result,
1362-
),
1363-
)
1364-
],
1365-
)
1357+
),
1358+
)
1359+
]
13661360
)
13671361
except OSError as e:
13681362
_logger.warning(str(e), exc_info=builder.debug)
13691363
except Exception:
13701364
_logger.error("Unexpected error from fs_access", exc_info=True)
13711365
raise
13721366

1373-
for files in cast(MutableSequence[CWLFileType | CWLDirectoryType], r):
1367+
for files in r:
13741368
rfile = files.copy()
13751369
revmap(rfile)
13761370
if is_directory(files):
13771371
ll = binding.get("loadListing") or builder.loadListing
13781372
if ll and ll != "no_listing":
13791373
get_listing(fs_access, files, (ll == "deep_listing"))
1380-
elif is_file(files):
1374+
else:
13811375
if binding.get("loadContents"):
13821376
with fs_access.open(rfile["location"], "rb") as f:
13831377
files["contents"] = str(

cwltool/cwlprov/provenance_profile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def declare_file(self, value: _CWLFileArtifact) -> tuple[ProvEntity, ProvEntity,
312312
self.document.specializationOf(file_entity, entity)
313313

314314
# Check for secondaries
315-
for sec in cast(MutableSequence[CWLObjectType], value.get("secondaryFiles", [])):
315+
for sec in value.get("secondaryFiles", []):
316316
# TODO: Record these in a specializationOf entity with UUID?
317317
if is_file(sec):
318318
(sec_entity, _, _) = self.declare_file(cast(_CWLFileArtifact, sec))

cwltool/pathmapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections.abc import ItemsView, Iterable, Iterator, KeysView, MutableSequence
77
from typing import NamedTuple, Optional, cast
88

9-
from cwl_utils.types import CWLDirectoryType, CWLFileType, is_directory, is_file
9+
from cwl_utils.types import CWLDirectoryType, CWLFileType, is_directory
1010
from mypy_extensions import mypyc_attr
1111
from schema_salad.exceptions import ValidationException
1212
from schema_salad.ref_resolver import uri_file_path
@@ -137,7 +137,7 @@ def visit(
137137
copy=copy,
138138
staged=staged,
139139
)
140-
elif is_file(obj):
140+
else:
141141
path = obj["location"]
142142
ab = abspath(path, basedir)
143143
if "contents" in obj and path.startswith("_:"):

cwltool/process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ def scandeps(
12591259
}
12601260
)
12611261
if "listing" in doc:
1262-
deps["listing"] = cast(CWLDirectoryType, doc)["listing"]
1262+
deps["listing"] = doc["listing"]
12631263
deps["location"] = urljoin(base, u)
12641264
if "basename" in doc:
12651265
deps["basename"] = doc["basename"]

0 commit comments

Comments
 (0)