Skip to content

Commit 2e104e0

Browse files
committed
fix: Clarify comments and extend tests for PR feedback
Signed-off-by: Mark Wiebe <[email protected]>
1 parent 526d660 commit 2e104e0

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/openjd/model/_parse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def decode_job_template(
167167
168168
Args:
169169
template (dict[str, Any]): A Job Template as a dictionary object.
170-
supported_extensions (list[str]): A list of extension names to support. This list is intersected
170+
supported_extensions (Optional[Iterable[str]]): A list of extension names to support. This list is intersected
171171
with the extensions names supported by the implementation before processing.
172172
173173
Returns:
@@ -241,7 +241,7 @@ def decode_environment_template(
241241
242242
Args:
243243
template (dict[str, Any]): An Environment Template as a dictionary object.
244-
supported_extensions (list[str]): A list of extension names to support. This list is intersected
244+
supported_extensions (Optional[Iterable[str]]): A list of extension names to support. This list is intersected
245245
with the extensions names supported by the implementation before processing.
246246
247247
Returns:

src/openjd/model/v2023_09/_model.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ class ModelParsingContext:
6262
"""
6363

6464
extensions: set[str]
65-
"""Initially, is the set of supported extension names. When the 'extensions'
66-
field of the template is processed, this becomes the set of extensions that
67-
the the template requested."""
65+
"""When parsing a top-level model instance, this is the set of supported extension names.
66+
The 'extensions' field is second in the list of model properties for both the job template
67+
and environment template, and when that field is processed it becomes the set of extensions
68+
that the template requested.
69+
70+
When fields of a model that depend on an extension are processed, its validators should
71+
check whether the needed extension is included in the context and adjust its parsing
72+
as written in the specification.
73+
"""
6874

6975
def __init__(self, *, supported_extensions: Optional[Iterable[str]] = None) -> None:
7076
self.extensions = set(supported_extensions or [])
@@ -2450,7 +2456,8 @@ def _permitted_extension_names(
24502456
) -> Optional[ExtensionNameList]:
24512457
context = cast(ModelParsingContext, info.context)
24522458
if value is not None:
2453-
# Before processing the extensions field, context.extensions is the list of supported extensions.
2459+
# Before processing the extensions field, context.extensions is the list of supported extensions
2460+
# that were requested in the call of the parse_job_template function.
24542461
# Take the intersection of the input supported extensions with what is implemented
24552462
# in this list, as the implementation needs to support an extension for it to be supported.
24562463
supported_extensions = context.extensions.intersection(cls.supported_extension_names())

test/openjd/model/test_parse.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ class MockExtensionName(str, Enum):
196196
SUPPORTED_NAME = "SUPPORTED_NAME"
197197

198198

199+
class MockExtensionNameWithTwoNames(str, Enum):
200+
"""A mock enum with only SUPPORTED_NAME for testing."""
201+
202+
SUPPORTED_NAME = "SUPPORTED_NAME"
203+
ANOTHER_SUPPORTED_NAME = "ANOTHER_SUPPORTED_NAME"
204+
205+
199206
@pytest.mark.parametrize(
200207
"template,template_type,decode_function",
201208
[
@@ -262,6 +269,12 @@ def test_template_extensions_list(template, template_type, decode_function) -> N
262269
in str(excinfo.value)
263270
)
264271

272+
# Extension names cannot be repeated
273+
template["extensions"] = ["SUPPORTED_NAME", "SUPPORTED_NAME"]
274+
with pytest.raises(DecodeValidationError) as excinfo:
275+
decode_function(template=template)
276+
assert "Duplicate values for extension name are not allowed." in str(excinfo.value)
277+
265278
# When the request list includes an unsupported extension name
266279
template["extensions"] = ["SUPPORTED_NAME"]
267280
with pytest.raises(DecodeValidationError) as excinfo:
@@ -279,3 +292,10 @@ def test_template_extensions_list(template, template_type, decode_function) -> N
279292
f"1 validation errors for {template_type}\nextensions:\n\tUnsupported extension names: UNSUPPORTED_NAME"
280293
in str(excinfo.value)
281294
)
295+
296+
# For this test, there are two different extension names supported
297+
with patch.object(openjd.model.v2023_09._model, "ExtensionName", MockExtensionNameWithTwoNames):
298+
# When the requested extension name is in the supported list
299+
template["extensions"] = ["ANOTHER_SUPPORTED_NAME"]
300+
model = decode_function(template=template, supported_extensions=["ANOTHER_SUPPORTED_NAME"])
301+
assert model.extensions == ["ANOTHER_SUPPORTED_NAME"]

0 commit comments

Comments
 (0)