Skip to content

Commit e4b80ee

Browse files
committed
Support PEP 585 dict syntax in dagster-k8s models
1 parent bf00518 commit e4b80ee

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

python_modules/libraries/dagster-k8s/dagster_k8s/models.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ def _get_openapi_list_element_type(attr_type: str) -> str:
3939

4040

4141
def _is_openapi_dict_type(attr_type: str) -> bool:
42-
return attr_type.startswith("dict(")
42+
return attr_type.startswith("dict(") or attr_type.startswith("dict[")
4343

4444

4545
def _get_openapi_dict_value_type(attr_type: str) -> str:
4646
# group(2) because value, not key
47-
match = check.not_none(re.match(r"dict\(([^,]*), (.*)\)", attr_type))
47+
match = check.not_none(
48+
re.match(r"dict\(([^,]*), (.*)\)", attr_type)
49+
or re.match(r"dict\[([^,]*),\s*(.*)\]", attr_type)
50+
)
51+
4852
return match.group(2)
4953

5054

python_modules/libraries/dagster-k8s/dagster_k8s_tests/unit_tests/test_models.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,36 @@ def test_logger_calls_bounded():
150150

151151
# Creating a model should not use the logger lock
152152
assert mock_set_level.call_count == 0
153+
154+
155+
def test_snake_case_dict_type_pep585_format():
156+
with mock.patch.object(
157+
kubernetes.client.V1CSIVolumeSource,
158+
"openapi_types",
159+
{
160+
**kubernetes.client.V1CSIVolumeSource.openapi_types,
161+
"volume_attributes": "dict[str, str]",
162+
},
163+
):
164+
volume_dict = {
165+
"name": "my_volume",
166+
"csi": {
167+
"driver": "my_driver",
168+
"volumeAttributes": {
169+
"fooKey": "fooVal"
170+
},
171+
},
172+
}
173+
174+
assert k8s_snake_case_dict(
175+
kubernetes.client.V1Volume,
176+
volume_dict,
177+
) == {
178+
"name": "my_volume",
179+
"csi": {
180+
"driver": "my_driver",
181+
"volume_attributes": {
182+
"fooKey": "fooVal",
183+
},
184+
},
185+
}

0 commit comments

Comments
 (0)