Skip to content

Commit 197eac9

Browse files
committed
pyupgrade refresh
1 parent d06ab55 commit 197eac9

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

cwl_utils/inputs_schema_gen.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
from copy import deepcopy
1212
from pathlib import Path
13-
from typing import Any, Dict, List, Optional, Union
13+
from typing import Any, Optional, Union
1414
from urllib.parse import urlparse
1515

1616
import requests
@@ -85,22 +85,22 @@ class JSONSchemaProperty:
8585
def __init__(
8686
self,
8787
name: str,
88-
type_: Union[InputType, List[InputType], str, Any],
88+
type_: Union[InputType, list[InputType], str, Any],
8989
description: Optional[str] = "",
9090
required: Optional[bool] = False,
9191
):
9292
"""Initialise the JSONSchemaProperty object."""
9393
# Initialise values
9494
self.name: str = name
95-
self.type_: Union[InputType, List[InputType], str, Any] = type_
95+
self.type_: Union[InputType, list[InputType], str, Any] = type_
9696
self.description = description
9797
self.required = required
9898
self.type_dict = self.generate_type_dict()
9999

100-
def generate_type_dict(self) -> Dict[str, Any]:
100+
def generate_type_dict(self) -> dict[str, Any]:
101101
"""Generate the type dict for a property from a CWL input parameter type."""
102102
# If the type is a list and contains null, then the property is not required
103-
if isinstance(self.type_, List) and "null" in self.type_:
103+
if isinstance(self.type_, list) and "null" in self.type_:
104104
self.required = False
105105
self.type_ = list(filter(lambda type_item: type_item != "null", self.type_))
106106

@@ -109,7 +109,7 @@ def generate_type_dict(self) -> Dict[str, Any]:
109109
self.type_ = self.type_[0]
110110

111111
# type_ is still a list therefore we offer multiple input types for this parameter
112-
if isinstance(self.type_, List):
112+
if isinstance(self.type_, list):
113113
# We use the oneOf keyword to specify multiple types
114114
type_dict = self.generate_type_dict_from_type_list(self.type_)
115115
# type_ is a single type
@@ -121,7 +121,7 @@ def generate_type_dict(self) -> Dict[str, Any]:
121121

122122
return type_dict
123123

124-
def generate_type_dict_from_type(self, type_item: Any) -> Dict[str, Any]:
124+
def generate_type_dict_from_type(self, type_item: Any) -> dict[str, Any]:
125125
"""
126126
Generate the type dict for a property from a CWL input parameter type.
127127
@@ -162,7 +162,7 @@ def generate_type_dict_from_type(self, type_item: Any) -> Dict[str, Any]:
162162
elif isinstance(type_item, InputRecordSchemaTypes):
163163
if type_item.fields is None:
164164
return {"type": "object"}
165-
if not isinstance(type_item.fields, List):
165+
if not isinstance(type_item.fields, list):
166166
_cwlutilslogger.error(
167167
"Expected fields of InputRecordSchemaType to be a list"
168168
)
@@ -176,7 +176,7 @@ def generate_type_dict_from_type(self, type_item: Any) -> Dict[str, Any]:
176176
for prop in type_item.fields
177177
},
178178
}
179-
elif isinstance(type_item, Dict):
179+
elif isinstance(type_item, dict):
180180
# Nested import
181181
# {'$import': '../relative/path/to/schema'}
182182
if "$import" in type_item.keys():
@@ -186,7 +186,7 @@ def generate_type_dict_from_type(self, type_item: Any) -> Dict[str, Any]:
186186
}
187187
else:
188188
raise ValueError(f"Unknown type: {type_item}")
189-
elif isinstance(type_item, List):
189+
elif isinstance(type_item, list):
190190
# Nested schema
191191
return {
192192
"oneOf": list(
@@ -200,8 +200,8 @@ def generate_type_dict_from_type(self, type_item: Any) -> Dict[str, Any]:
200200
raise ValueError(f"Unknown type: {type_item}")
201201

202202
def generate_type_dict_from_type_list(
203-
self, type_: List[InputType]
204-
) -> Dict[str, Any]:
203+
self, type_: list[InputType]
204+
) -> dict[str, Any]:
205205
"""Given a list of types, generate a JSON schema property dict wrapped in oneOf list."""
206206
return {
207207
"oneOf": list(
@@ -212,7 +212,7 @@ def generate_type_dict_from_type_list(
212212
)
213213
}
214214

215-
def to_dict(self) -> Dict[str, Any]:
215+
def to_dict(self) -> dict[str, Any]:
216216
"""Return as a dictionary."""
217217
return {self.name: self.type_dict}
218218

@@ -225,7 +225,7 @@ def get_is_required_from_input_parameter(
225225
return False
226226
if input_parameter.default is not None:
227227
return False
228-
if isinstance(input_parameter.type_, List) and "null" in input_parameter.type_:
228+
if isinstance(input_parameter.type_, list) and "null" in input_parameter.type_:
229229
return False
230230
if isinstance(input_parameter.type_, InputRecordSchemaTypes):
231231
if input_parameter.type_ is not None:
@@ -258,7 +258,7 @@ def generate_json_schema_property_from_input_parameter(
258258
)
259259

260260

261-
def generate_definition_from_schema(schema: InputRecordSchema) -> Dict[str, Any]:
261+
def generate_definition_from_schema(schema: InputRecordSchema) -> dict[str, Any]:
262262
"""
263263
Given a schema, generate a JSON schema definition.
264264
@@ -291,7 +291,7 @@ def generate_definition_from_schema(schema: InputRecordSchema) -> Dict[str, Any]
291291
if isinstance(prop_obj, str):
292292
raise TypeError("Property Object should be a dictionary")
293293

294-
if isinstance(prop_obj.get("type", []), List):
294+
if isinstance(prop_obj.get("type", []), list):
295295
if "null" in prop_obj.get("type", []):
296296
required = False
297297
prop_obj["type"] = list(
@@ -333,7 +333,7 @@ def cwl_to_jsonschema(cwl_obj: Union[Workflow, CommandLineTool]) -> Any:
333333
334334
"""
335335
# Initialise the schema from the workflow input json schema template
336-
with open(JSON_TEMPLATE_PATH, "r") as template_h:
336+
with open(JSON_TEMPLATE_PATH) as template_h:
337337
input_json_schema = json.load(template_h)
338338

339339
# Get the complex schema keys
@@ -354,7 +354,7 @@ def is_complex_record_schema_key(idx_iter: str) -> TypeGuard[bool]:
354354
return True
355355
return False
356356

357-
complex_schema_keys: List[str] = list(
357+
complex_schema_keys: list[str] = list(
358358
filter(
359359
lambda idx_iter: is_complex_record_schema_key(idx_iter),
360360
cwl_obj.loadingOptions.idx.keys(),
@@ -376,7 +376,7 @@ def get_complex_schema_values(idx_iter: str) -> InputRecordSchema:
376376

377377
return input_record_schema
378378

379-
complex_schema_values: List[InputRecordSchema] = list(
379+
complex_schema_values: list[InputRecordSchema] = list(
380380
map(
381381
lambda idx_iter: get_complex_schema_values(idx_iter),
382382
complex_schema_keys,
@@ -462,9 +462,9 @@ def get_complex_schema_values(idx_iter: str) -> InputRecordSchema:
462462

463463
# Traverse the properties and return all definitions that are used
464464
def _recursive_search(
465-
json_data: Dict[str, Any],
465+
json_data: dict[str, Any],
466466
target_key: str,
467-
) -> List[Any]:
467+
) -> list[Any]:
468468
"""Given a target key return all instances of a key in a json object."""
469469
result = []
470470

@@ -482,16 +482,16 @@ def _recursive_search(
482482

483483

484484
# Get all the property dependencies
485-
def _get_all_ref_attributes(json_object: Dict[str, Any]) -> List[Any]:
485+
def _get_all_ref_attributes(json_object: dict[str, Any]) -> list[Any]:
486486
"""Given a json object, return all the reference attributes."""
487487
return _recursive_search(json_object, "$ref")
488488

489489

490490
def get_property_dependencies(
491-
property_dict: Dict[str, Any],
492-
input_json_schema: Dict[str, Any],
493-
existing_property_dependencies: Optional[List[Any]] = None,
494-
) -> List[str]:
491+
property_dict: dict[str, Any],
492+
input_json_schema: dict[str, Any],
493+
existing_property_dependencies: Optional[list[Any]] = None,
494+
) -> list[str]:
495495
"""Recursively collect all dependencies for a property."""
496496
# Initialise return list
497497
if existing_property_dependencies is None:
@@ -516,7 +516,7 @@ def get_property_dependencies(
516516
return existing_property_dependencies
517517

518518

519-
def slim_definitions(input_json_schema: Dict[str, Any]) -> Dict[str, Any]:
519+
def slim_definitions(input_json_schema: dict[str, Any]) -> dict[str, Any]:
520520
"""
521521
Slim down the schema to only the definitions that are used by the properties.
522522
@@ -552,7 +552,7 @@ def arg_parser() -> argparse.ArgumentParser:
552552
return parser
553553

554554

555-
def parse_args(args: List[str]) -> argparse.Namespace:
555+
def parse_args(args: list[str]) -> argparse.Namespace:
556556
"""Parse the command line arguments."""
557557
return arg_parser().parse_args(args)
558558

cwl_utils/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from collections.abc import MutableMapping, MutableSequence
1111
from copy import deepcopy
1212
from io import StringIO
13-
from typing import Any, Dict, List, Optional, Union
13+
from typing import Any, Optional, Union
1414
from urllib.parse import urlparse
1515

1616
from ruamel.yaml.main import YAML
@@ -260,8 +260,8 @@ def to_pascal_case(name: str) -> str:
260260

261261

262262
def sanitise_schema_field(
263-
schema_field_item: Union[Dict[str, Any], str],
264-
) -> Union[Dict[str, Any], str]:
263+
schema_field_item: Union[dict[str, Any], str],
264+
) -> Union[dict[str, Any], str]:
265265
"""
266266
Schemas need to be resolved before converted to JSON properties.
267267
@@ -344,7 +344,7 @@ def sanitise_schema_field(
344344
if isinstance(schema_field_item, InputRecordSchemaTypes):
345345
return schema_field_item
346346

347-
if isinstance(schema_field_item.get("type"), List):
347+
if isinstance(schema_field_item.get("type"), list):
348348
if "null" in schema_field_item.get("type", []):
349349
required = False
350350
schema_field_item["type"] = list(
@@ -380,7 +380,7 @@ def sanitise_schema_field(
380380
type_="array", items=schema_field_item.get("type", "")
381381
)
382382

383-
if isinstance(schema_field_item.get("type"), Dict):
383+
if isinstance(schema_field_item.get("type"), dict):
384384
# Likely an enum
385385
if schema_field_item.get("type", {}).get("type", "") == "enum":
386386
schema_field_item["type"] = InputEnumSchemaV1_2(
@@ -398,7 +398,7 @@ def sanitise_schema_field(
398398
raise ValueError(f"Unknown type: {schema_field_item.get('type')}")
399399

400400
if not required:
401-
if isinstance(schema_field_item.get("type"), List):
401+
if isinstance(schema_field_item.get("type"), list):
402402
schema_field_item["type"] = ["null"] + schema_field_item.get("type", [])
403403
else:
404404
schema_field_item["type"] = ["null", schema_field_item.get("type", "")]

0 commit comments

Comments
 (0)