>>> _validate_dict({"b": 0.5}, "mock_name", float) >>> _validate_dict("invalid", "mock_name", float) Traceback (most recent call last): ... ValueError: mock_name must be a dict >>> _validate_dict({"a": 8}, "mock_name", dict) Traceback (most recent call last):
(
_object: Any, var_name: str, value_type: type, nested: bool = False
)
| 338 | |
| 339 | |
| 340 | def _validate_dict( |
| 341 | _object: Any, var_name: str, value_type: type, nested: bool = False |
| 342 | ) -> None: |
| 343 | """ |
| 344 | >>> _validate_dict({"b": 0.5}, "mock_name", float) |
| 345 | >>> _validate_dict("invalid", "mock_name", float) |
| 346 | Traceback (most recent call last): |
| 347 | ... |
| 348 | ValueError: mock_name must be a dict |
| 349 | >>> _validate_dict({"a": 8}, "mock_name", dict) |
| 350 | Traceback (most recent call last): |
| 351 | ... |
| 352 | ValueError: mock_name all values must be dict |
| 353 | >>> _validate_dict({2: 0.5}, "mock_name",float, True) |
| 354 | Traceback (most recent call last): |
| 355 | ... |
| 356 | ValueError: mock_name all keys must be strings |
| 357 | >>> _validate_dict({"b": 4}, "mock_name", float,True) |
| 358 | Traceback (most recent call last): |
| 359 | ... |
| 360 | ValueError: mock_name nested dictionary all values must be float |
| 361 | """ |
| 362 | if not isinstance(_object, dict): |
| 363 | msg = f"{var_name} must be a dict" |
| 364 | raise ValueError(msg) |
| 365 | if not all(isinstance(x, str) for x in _object): |
| 366 | msg = f"{var_name} all keys must be strings" |
| 367 | raise ValueError(msg) |
| 368 | if not all(isinstance(x, value_type) for x in _object.values()): |
| 369 | nested_text = "nested dictionary " if nested else "" |
| 370 | msg = f"{var_name} {nested_text}all values must be {value_type.__name__}" |
| 371 | raise ValueError(msg) |
| 372 | |
| 373 | |
| 374 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected