Skip to content

Commit cb3160c

Browse files
committed
flake8 cleanup
Bonus type modernizing and some docstrings
1 parent 8978ca2 commit cb3160c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+502
-460
lines changed

.flake8

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[flake8]
2+
ignore = E203,W503
3+
max-line-length = 100
4+
select = B,C,E,F,W,T4
5+
exclude = cwltool/schemas
6+
extend-ignore = E501,B905
7+
# when Python 3.10 is the minimum version, re-enable check B905 for zip + strict
8+
extend-select = B9

cwltool/argparser.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def arg_parser() -> argparse.ArgumentParser:
4242
type=str,
4343
default="",
4444
help="Log your tools stdout/stderr to this location outside of container "
45-
"This will only log stdout/stderr if you specify stdout/stderr in their respective fields or capture it as an output",
45+
"This will only log stdout/stderr if you specify stdout/stderr in their "
46+
"respective fields or capture it as an output",
4647
)
4748

4849
parser.add_argument(
@@ -741,7 +742,7 @@ def get_default_args() -> Dict[str, Any]:
741742

742743

743744
class FSAction(argparse.Action):
744-
objclass = None # type: str
745+
objclass: Optional[str] = None
745746

746747
def __init__(
747748
self,
@@ -777,7 +778,7 @@ def __call__(
777778

778779

779780
class FSAppendAction(argparse.Action):
780-
objclass = None # type: str
781+
objclass: Optional[str] = None
781782

782783
def __init__(
783784
self,
@@ -815,19 +816,19 @@ def __call__(
815816

816817

817818
class FileAction(FSAction):
818-
objclass = "File"
819+
objclass: Optional[str] = "File"
819820

820821

821822
class DirectoryAction(FSAction):
822-
objclass = "Directory"
823+
objclass: Optional[str] = "Directory"
823824

824825

825826
class FileAppendAction(FSAppendAction):
826-
objclass = "File"
827+
objclass: Optional[str] = "File"
827828

828829

829830
class DirectoryAppendAction(FSAppendAction):
830-
objclass = "Directory"
831+
objclass: Optional[str] = "Directory"
831832

832833

833834
class AppendAction(argparse.Action):
@@ -900,9 +901,9 @@ def add_argument(
900901
return None
901902

902903
ahelp = description.replace("%", "%%")
903-
action = None # type: Optional[Union[Type[argparse.Action], str]]
904-
atype = None # type: Any
905-
typekw = {} # type: Dict[str, Any]
904+
action: Optional[Union[Type[argparse.Action], str]] = None
905+
atype: Optional[Any] = None
906+
typekw: Dict[str, Any] = {}
906907

907908
if inptype == "File":
908909
action = FileAction

cwltool/builder.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22
import copy
33
import logging
44
import math
5-
from typing import (
5+
from typing import ( # pylint: disable=unused-import
66
IO,
7+
TYPE_CHECKING,
78
Any,
89
Callable,
910
Dict,
1011
List,
1112
MutableMapping,
1213
MutableSequence,
1314
Optional,
15+
Type,
1416
Union,
1517
cast,
1618
)
1719

1820
from cwl_utils import expression
1921
from cwl_utils.file_formats import check_format
2022
from rdflib import Graph
21-
from ruamel.yaml.comments import CommentedMap
2223
from schema_salad.avro.schema import Names, Schema, make_avsc_object
2324
from schema_salad.exceptions import ValidationException
2425
from schema_salad.sourceline import SourceLine
2526
from schema_salad.utils import convert_to_dict, json_dumps
2627
from schema_salad.validate import validate
27-
from typing_extensions import TYPE_CHECKING, Type # pylint: disable=unused-import
28+
29+
from ruamel.yaml.comments import CommentedMap
2830

2931
from .errors import WorkflowException
3032
from .loghandler import _logger
@@ -186,10 +188,8 @@ def bind_input(
186188
if lead_pos is None:
187189
lead_pos = []
188190

189-
bindings = [] # type: List[MutableMapping[str, Union[str, List[int]]]]
190-
binding = (
191-
{}
192-
) # type: Union[MutableMapping[str, Union[str, List[int]]], CommentedMap]
191+
bindings: List[MutableMapping[str, Union[str, List[int]]]] = []
192+
binding: Union[MutableMapping[str, Union[str, List[int]]], CommentedMap] = {}
193193
value_from_expression = False
194194
if "inputBinding" in schema and isinstance(
195195
schema["inputBinding"], MutableMapping
@@ -208,7 +208,7 @@ def bind_input(
208208
).makeError(
209209
"'position' expressions must evaluate to an int, "
210210
f"not a {type(result)}. Expression {position} "
211-
f"resulted in '{result}'."
211+
f"resulted in {result!r}."
212212
)
213213
binding["position"] = result
214214
bp.append(result)
@@ -227,7 +227,7 @@ def bind_input(
227227
if isinstance(schema["type"], MutableSequence):
228228
bound_input = False
229229
for t in schema["type"]:
230-
avsc = None # type: Optional[Schema]
230+
avsc: Optional[Schema] = None
231231
if isinstance(t, str) and self.names.has_name(t, None):
232232
avsc = self.names.get_name(t, None)
233233
elif (
@@ -336,10 +336,10 @@ def bind_input(
336336
if binding:
337337
b2 = cast(CWLObjectType, copy.deepcopy(binding))
338338
b2["datum"] = item
339-
itemschema = {
339+
itemschema: CWLObjectType = {
340340
"type": schema["items"],
341341
"inputBinding": b2,
342-
} # type: CWLObjectType
342+
}
343343
for k in ("secondaryFiles", "format", "streamable"):
344344
if k in schema:
345345
itemschema[k] = schema[k]
@@ -362,9 +362,9 @@ def _capture_files(f: CWLObjectType) -> CWLObjectType:
362362
datum = cast(CWLObjectType, datum)
363363
self.files.append(datum)
364364

365-
loadContents_sourceline = (
366-
None
367-
) # type: Union[None, MutableMapping[str, Union[str, List[int]]], CWLObjectType]
365+
loadContents_sourceline: Union[
366+
None, MutableMapping[str, Union[str, List[int]]], CWLObjectType
367+
] = None
368368
if binding and binding.get("loadContents"):
369369
loadContents_sourceline = binding
370370
elif schema.get("loadContents"):
@@ -385,7 +385,7 @@ def _capture_files(f: CWLObjectType) -> CWLObjectType:
385385
except Exception as e:
386386
raise Exception(
387387
"Reading {}\n{}".format(datum["location"], e)
388-
)
388+
) from e
389389

390390
if "secondaryFiles" in schema:
391391
if "secondaryFiles" not in datum:
@@ -415,8 +415,8 @@ def _capture_files(f: CWLObjectType) -> CWLObjectType:
415415
"The result of a expression in the field "
416416
"'required' must "
417417
f"be a bool or None, not a {type(required_result)}. "
418-
f"Expression '{sf_entry['required']}' resulted "
419-
f"in '{required_result}'."
418+
f"Expression {sf_entry['required']!r} resulted "
419+
f"in {required_result!r}."
420420
)
421421
sf_required = required_result
422422
else:
@@ -454,7 +454,7 @@ def _capture_files(f: CWLObjectType) -> CWLObjectType:
454454
"Expected secondaryFile expression to "
455455
"return type 'str', a 'File' or 'Directory' "
456456
"dictionary, or a list of the same. Received "
457-
f"'{type(sfname)} from '{sf_entry['pattern']}'."
457+
f"{type(sfname)!r} from {sf_entry['pattern']!r}."
458458
)
459459

460460
for d in cast(
@@ -529,9 +529,9 @@ def addsf(
529529
"An expression in the 'format' field must "
530530
"evaluate to a string, or list of strings. "
531531
"However a non-string item was received: "
532-
f"'{entry}' of type '{type(entry)}'. "
533-
f"The expression was '{schema['format']}' and "
534-
f"its fully evaluated result is '{eval_format}'."
532+
f"{entry!r} of type {type(entry)!r}. "
533+
f"The expression was {schema['format']!r} and "
534+
f"its fully evaluated result is {eval_format!r}."
535535
)
536536
if expression.needs_parsing(entry):
537537
message = (
@@ -542,7 +542,7 @@ def addsf(
542542
"Expressions or CWL Parameter References. List "
543543
f"entry number {index+1} contains the following "
544544
"unallowed CWL Parameter Reference or Expression: "
545-
f"'{entry}'."
545+
f"{entry!r}."
546546
)
547547
if message:
548548
raise SourceLine(
@@ -557,8 +557,8 @@ def addsf(
557557
"evaluate to a string, or list of strings. "
558558
"However the type of the expression result was "
559559
f"{type(eval_format)}. "
560-
f"The expression was '{schema['format']}' and "
561-
f"its fully evaluated result is 'eval_format'."
560+
f"The expression was {schema['format']!r} and "
561+
f"its fully evaluated result is {eval_format!r}."
562562
)
563563
try:
564564
check_format(
@@ -568,8 +568,8 @@ def addsf(
568568
)
569569
except ValidationException as ve:
570570
raise WorkflowException(
571-
"Expected value of '%s' to have format %s but\n "
572-
" %s" % (schema["name"], schema["format"], ve)
571+
f"Expected value of {schema['name']!r} to have "
572+
f"format {schema['format']!r} but\n {ve}"
573573
) from ve
574574

575575
visit_class(
@@ -646,7 +646,7 @@ def generate_arg(self, binding: CWLObjectType) -> List[str]:
646646
"'separate' option can not be specified without prefix"
647647
)
648648

649-
argl = [] # type: MutableSequence[CWLOutputType]
649+
argl: MutableSequence[CWLOutputType] = []
650650
if isinstance(value, MutableSequence):
651651
if binding.get("itemSeparator") and value:
652652
itemSeparator = cast(str, binding["itemSeparator"])

cwltool/checker.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def check_types(
6262
return check_types(
6363
merge_flatten_type(_get_type(srctype)), _get_type(sinktype), None, None
6464
)
65-
raise WorkflowException(f"Unrecognized linkMerge enum '{linkMerge}'")
65+
raise WorkflowException(f"Unrecognized linkMerge enum {linkMerge!r}")
6666

6767

6868
def merge_flatten_type(src: SinkType) -> CWLOutputType:
@@ -561,8 +561,8 @@ def loop_checker(steps: Iterator[MutableMapping[str, Any]]) -> None:
561561
"""
562562
Check http://commonwl.org/cwltool#Loop requirement compatibility with other directives.
563563
564-
:raises:
565-
ValidationException: If there is an incompatible combination between cwltool:loop and 'scatter' or 'when'.
564+
:raises ValidationException: If there is an incompatible combination between
565+
cwltool:loop and 'scatter' or 'when'.
566566
"""
567567
exceptions = []
568568
for step in steps:

0 commit comments

Comments
 (0)