Skip to content

Commit

Permalink
Make NdArray.dataType required. Add test case for NdArray with all nu…
Browse files Browse the repository at this point in the history
…ll values
  • Loading branch information
PaulVanSchayck committed Nov 14, 2024
1 parent 685c767 commit 6df19b7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/covjson_pydantic/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ class DataType(str, Enum):

class NdArray(CovJsonBaseModel, Generic[NdArrayTypeT], extra="allow"):
type: Literal["NdArray"] = "NdArray"
dataType: DataType = DataType.float # noqa: N815
dataType: DataType # noqa: N815
axisNames: Optional[List[str]] = None # noqa: N815
shape: Optional[List[int]] = None
values: List[Optional[NdArrayTypeT]] = None

@model_validator(mode="after")
def check_data_type(self):
t = typing.get_args(self.model_fields["values"].annotation)[0]
if t == typing.Optional[NdArrayTypeT]:
given_type = self.dataType.name if isinstance(self.dataType, DataType) else ""
raise ValueError(f"No NdArray type given, please specify as NdArray[{given_type}]")
if self.dataType == DataType.float and not t == typing.Optional[float]:
raise ValueError("dataType and NdArray type must both be float.")
if self.dataType == DataType.str and not t == typing.Optional[str]:
Expand Down
1 change: 1 addition & 0 deletions tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def test_happy_cases(file_name, object_type):
("mixed-type-axes.json", Axes, r"Input should be a valid number"),
("mixed-type-axes-2.json", Axes, r"Input should be a valid string"),
("mixed-type-ndarray-1.json", NdArray[float], r"Input should be a valid number"),
("mixed-type-ndarray-1.json", NdArray, r"No NdArray type given, please specify as NdArray\[float\]"),
("mixed-type-ndarray-2.json", NdArray[str], r"dataType and NdArray type must both be float"),
("mixed-type-ndarray-3.json", NdArray[int], r"Input should be a valid integer"),
("mixed-type-ndarray-3.json", NdArray[float], r"dataType and NdArray type must both be integer"),
Expand Down
28 changes: 27 additions & 1 deletion tests/test_data/coverage-mixed-type-ndarray.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
"en": "integer"
}
}
},
"null-parameter": {
"type": "Parameter",
"observedProperty": {
"label": {
"en": "null"
}
}
}
},
"ranges": {
Expand Down Expand Up @@ -86,7 +94,7 @@
],
"values": [
null,
62,
62.0,
63.136801411019825,
63.56289242989419,
63.98448554858814,
Expand All @@ -110,6 +118,24 @@
1,
2
]
},
"null-parameter": {
"type": "NdArray",
"dataType": "integer",
"axisNames": [
"x",
"y",
"t"
],
"shape": [
1,
1,
2
],
"values": [
null,
null
]
}
},
"extra:extra": "extra fields allowed"
Expand Down

0 comments on commit 6df19b7

Please sign in to comment.