Skip to content

Commit 61fa373

Browse files
Merge pull request #1285 from linsword13/more-typing
Expand type checks
2 parents 8d1db2e + 6a66213 commit 61fa373

File tree

7 files changed

+47
-33
lines changed

7 files changed

+47
-33
lines changed

lib/ramble/ramble/definitions/variables.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# except according to those terms.
88

99
import copy
10-
from typing import List, Optional
10+
from typing import Any, Dict, List, Optional, Set
1111

1212
import ramble.util.colors as rucolor
1313

@@ -35,7 +35,7 @@ def __init__(
3535
self,
3636
name: str,
3737
default=None,
38-
description: str = None,
38+
description: Optional[str] = None,
3939
values=None,
4040
expandable: bool = True,
4141
track_used: bool = True,
@@ -182,7 +182,7 @@ def __init__(
182182
self,
183183
name: str,
184184
value=None,
185-
description: str = None,
185+
description: Optional[str] = None,
186186
method: str = "set",
187187
append_separator: str = ",",
188188
when=None,
@@ -278,10 +278,10 @@ def __init__(
278278
"""
279279
self.name = name
280280
self.when = when.copy() if when else []
281-
self.set = {}
282-
self.unset = {}
283-
self.prepend = []
284-
self.append = []
281+
self.set: Dict[str, str] = {}
282+
self.unset: Set[str] = set()
283+
self.prepend: List[Dict[str, Dict[str, str]]] = []
284+
self.append: List[Dict[str, Any]] = []
285285

286286
self.add_modification(
287287
modification=modification,

lib/ramble/ramble/language/language_base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import functools
1515
import inspect
1616
from collections.abc import Sequence # novm
17-
from typing import Any, Dict, List
17+
from typing import Any, Callable, Dict, List, Set
1818

1919
import llnl.util.lang
2020

@@ -25,7 +25,7 @@
2525

2626
#: These are variant names used by ramble internally; applications can't use
2727
#: them
28-
reserved_names = []
28+
reserved_names: List[str] = []
2929

3030
namespaces = [
3131
"ramble.app",
@@ -61,12 +61,12 @@ class DirectiveMeta(type):
6161
"""
6262

6363
# Set of all known directives
64-
_directive_names = set()
65-
_directive_init_values = {}
66-
_directives_to_be_executed = []
67-
_directive_functions = {}
68-
_directive_classes = {}
69-
_when_constraints_from_context = []
64+
_directive_names: Set[str] = set()
65+
_directive_init_values: Dict[str, Any] = {}
66+
_directives_to_be_executed: List[Callable[..., Any]] = []
67+
_directive_functions: Dict[str, Callable[..., Any]] = {}
68+
_directive_classes: Dict[str, type] = {}
69+
_when_constraints_from_context: List[str] = []
7070
_default_args: List[dict] = []
7171

7272
push_to_context = _push_to_context

lib/ramble/ramble/language/language_helpers.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import fnmatch
1010
from collections import OrderedDict
11-
from typing import Any, List, Union
11+
from typing import Any, List, Optional, Union
1212

1313
from ramble.language.language_base import DirectiveError
1414

@@ -131,7 +131,11 @@ def require_definition(
131131

132132

133133
def require_condition(
134-
obj, directive_name: str, single_arg_name: str = None, multiple_arg_name: str = None, **kwargs
134+
obj,
135+
directive_name: str,
136+
single_arg_name: Optional[str] = None,
137+
multiple_arg_name: Optional[str] = None,
138+
**kwargs,
135139
):
136140
"""Require at least one condition for a type in a directive, and converts all conditions to
137141
when conditions
@@ -155,25 +159,26 @@ def require_condition(
155159
Returns:
156160
List of all when conditions
157161
"""
162+
single_arg_val = kwargs.get(single_arg_name) if single_arg_name else None
163+
multiple_arg_val = kwargs.get(multiple_arg_name) if multiple_arg_name else None
164+
when_arg_val = kwargs.get("when")
158165

159-
if not (kwargs[single_arg_name] or kwargs[multiple_arg_name] or kwargs["when"]):
166+
if not (single_arg_val or multiple_arg_val or when_arg_val):
160167
raise DirectiveError(
161168
f"Directive {directive_name} requires at least one of "
162169
f"{single_arg_name} or {multiple_arg_name} or when to be defined."
163170
)
164171

165-
when_list = []
166-
if "when" in kwargs:
172+
if when_arg_val is not None:
167173
when_list = build_when_list(kwargs["when"], obj, obj.name, directive_name)
168-
169-
single_arg_val = kwargs[single_arg_name] if single_arg_name in kwargs else ""
170-
multiple_arg_vals = kwargs[multiple_arg_name] if multiple_arg_name in kwargs else []
174+
else:
175+
when_list = []
171176

172177
# If args are modifier modes, convert to when conditions
173178
if single_arg_name == "mode" or multiple_arg_name == "modes":
174179
all_modes = merge_definitions(
175180
single_arg_val,
176-
multiple_arg_vals,
181+
multiple_arg_val,
177182
obj.modes,
178183
"mode",
179184
"modes",

lib/ramble/ramble/language/modifier_language.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ def _define_modifier_variable(mod):
325325
def package_manager_requirement(
326326
command: str,
327327
validation_type: str,
328-
mode: str = None,
329-
modes: list = None,
328+
mode: Optional[str] = None,
329+
modes: Optional[list] = None,
330330
regex=None,
331331
package_manager: str = "*",
332332
when=None,

lib/ramble/ramble/variants.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ def multi_value_variant(self, name: str, value: Any):
196196
def _define_variant(
197197
self,
198198
name: str,
199-
variant_type: int,
199+
variant_type: variant_types,
200200
default: Optional[Any] = None,
201-
description: str = "",
201+
description: Optional[str] = "",
202202
values: Optional[Union[Sequence, Callable[[Any], bool]]] = None,
203203
):
204204
"""Define a variant within this set.
@@ -299,7 +299,7 @@ def __init__(
299299
self,
300300
name: str,
301301
default: Optional[Any] = None,
302-
description: str = "",
302+
description: Optional[str] = "",
303303
values: Optional[Union[Sequence, Callable[[Any], bool]]] = None,
304304
):
305305
self.name = name

lib/ramble/ramble/workload.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# option. This file may not be copied, modified, or distributed
77
# except according to those terms.
88

9-
from typing import List, Optional
9+
from typing import Dict, FrozenSet, List, Optional
1010

1111
import ramble.util.colors as rucolor
1212
from ramble.definitions.variables import EnvironmentVariable, Variable
@@ -15,6 +15,10 @@
1515
class Workload:
1616
"""Class representing a single workload"""
1717

18+
executables: List[str]
19+
inputs: List[str]
20+
tags: List[str]
21+
1822
def __init__(
1923
self,
2024
name: str,
@@ -36,8 +40,8 @@ def __init__(
3640
tags = []
3741

3842
self.name = name
39-
self.variables = {}
40-
self.environment_variables = {}
43+
self.variables: Dict[FrozenSet[str], List[Variable]] = {}
44+
self.environment_variables: Dict[FrozenSet[str], List[EnvironmentVariable]] = {}
4145

4246
attr_names = ["executables", "inputs", "tags"]
4347
attr_vals = [executables, inputs, tags]

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ directory = "htmlcov"
108108
[tool.mypy]
109109
# TODO: Add in more files for type-checking
110110
# For now, only type-check the util directory
111-
files = ["lib/ramble/ramble/util/**/*.py"]
111+
files = [
112+
"lib/ramble/ramble/util/**/*.py",
113+
"lib/ramble/ramble/schema/**/*.py",
114+
"lib/ramble/ramble/language/**/*.py",
115+
"lib/ramble/ramble/definitions/**/*.py",
116+
]
112117
ignore_errors = true
113118
ignore_missing_imports = true
114119
allow_redefinition = true

0 commit comments

Comments
 (0)