Skip to content

Commit 4ed9478

Browse files
Changes made
1 parent 4a3a3ff commit 4ed9478

File tree

7 files changed

+45
-27
lines changed

7 files changed

+45
-27
lines changed

dpytools/config/config.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99

1010

1111
class Config:
12-
1312
def __init__(self):
1413
self._properties_to_validate: List[BaseProperty] = []
1514

1615
@staticmethod
1716
def from_env(config_dict: Dict[str, Dict[str, Any]]) -> Config:
18-
1917
config = Config()
2018

2119
for env_var_name, value in config_dict.items():
22-
2320
value_for_property = os.environ.get(env_var_name, None)
2421
assert (
2522
value_for_property is not None
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .string import StringProperty
21
from .intproperty import IntegerProperty
2+
from .string import StringProperty

dpytools/config/properties/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ def name(self):
1414

1515
@name.setter
1616
def name(self, value):
17-
raise ValueError(f"Trying to change name property to value {value} but you cannot change a property name after instantiation.")
17+
raise ValueError(
18+
f"Trying to change name property to value {value} but you cannot change a property name after instantiation."
19+
)
1820

1921
@property
2022
def value(self):
2123
return self._value
2224

2325
@value.setter
2426
def value(self, value):
25-
raise ValueError(f"Trying to change value to {value} but you cannot change a property value after instantiation.")
27+
raise ValueError(
28+
f"Trying to change value to {value} but you cannot change a property value after instantiation."
29+
)
2630

2731
@abstractmethod
2832
def type_is_valid(self):
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import Optional
21
from dataclasses import dataclass
2+
from typing import Optional
3+
34
from .base import BaseProperty
45

6+
57
@dataclass
68
class IntegerProperty(BaseProperty):
79
min_val: Optional[int]
@@ -10,12 +12,14 @@ class IntegerProperty(BaseProperty):
1012
def type_is_valid(self):
1113
"""
1214
Validate that the property looks like
13-
its of the correct type
15+
its of the correct type
1416
"""
1517
try:
1618
int(self._value)
1719
except Exception as err:
18-
raise Exception(f"Cannot cast {self._name} value {self._value} to integer.") from err
20+
raise Exception(
21+
f"Cannot cast {self._name} value {self._value} to integer."
22+
) from err
1923

2024
def secondary_validation(self):
2125
"""
@@ -26,7 +30,11 @@ def secondary_validation(self):
2630
raise ValueError(f"Integer value for {self._name} does not exist.")
2731

2832
if self.min_val and self._value < self.min_val:
29-
raise ValueError(f"Integer value for {self._name} is lower than allowed minimum.")
33+
raise ValueError(
34+
f"Integer value for {self._name} is lower than allowed minimum."
35+
)
3036

3137
if self.max_val and self._value > self.max_val:
32-
raise ValueError(f"Integer value for {self._name} is higher than allowed maximum.")
38+
raise ValueError(
39+
f"Integer value for {self._name} is higher than allowed maximum."
40+
)

dpytools/config/properties/string.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Optional
1+
import re
22
from dataclasses import dataclass
3-
from .base import BaseProperty
3+
from typing import Optional
44

5-
import re
5+
from .base import BaseProperty
66

77

88
@dataclass

dpytools/validation/json/validation.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
def validate_json_schema(
1111
schema_path: Union[Path, str],
1212
data_dict: Optional[Dict] = None,
13-
data_path: Union[Path, str, None] = None,
14-
error_msg: Optional[str] = "",
15-
indent: Optional[int] = 2,
13+
data_path: Optional[Union[Path, str]] = None,
14+
error_msg: Optional[str] = None,
15+
indent: Optional[int] = None,
1616
):
1717
"""
18-
Validate metadata.json files against schema.
18+
Validate a JSON file against a schema.
1919
2020
Either `data_dict` or `data_path` must be provided.
2121
22-
`msg` and `indent` are used to format the error message if validation fails.
22+
`error_msg` and `indent` can be used to format the error message if validation fails.
2323
"""
2424
# Confirm that *either* `data_dict` *or* `data_path` has been provided, otherwise raise ValueError
2525
if data_dict and data_path:
@@ -46,14 +46,19 @@ def validate_json_schema(
4646
# Load data to be validated as dict
4747
if data_dict:
4848
if not isinstance(data_dict, Dict):
49-
raise ValueError("Invalid data format")
49+
raise ValueError(
50+
"Invalid data format, `data_dict` should be a Python dictionary"
51+
)
5052
data_to_validate = data_dict
5153

5254
if data_path:
5355
if isinstance(data_path, str):
5456
data_path = Path(data_path).absolute()
5557
if not isinstance(data_path, Path):
56-
raise ValueError("Invalid data format")
58+
raise ValueError(
59+
"Invalid data format, `data_path` should be a pathlib.Path or string of file location"
60+
)
61+
# Check `data_path` exists
5762
if not data_path.exists():
5863
raise ValueError(f"Data path '{data_path}' does not exist")
5964
with open(data_path, "r") as f:
@@ -78,5 +83,6 @@ def validate_json_schema(
7883
JSON data:
7984
{json.dumps(data_to_validate, indent=indent)}
8085
"""
86+
print(formatted_msg)
8187
raise ValidationError(formatted_msg) from err
8288
raise err

tests/test_json_validation.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ def test_validate_json_schema_data_path():
1515
validate_json_schema(
1616
schema_path=pipeline_config_schema,
1717
data_path=pipeline_config,
18-
error_msg="Validating pipeline_config.json",
1918
)
2019
is None
2120
)
@@ -38,7 +37,6 @@ def test_validate_json_schema_data_dict():
3837
validate_json_schema(
3938
schema_path=pipeline_config_schema,
4039
data_dict=pipeline_config,
41-
error_msg="Validating pipeline_config dict",
4240
)
4341
is None
4442
)
@@ -82,15 +80,18 @@ def test_validate_json_schema_no_data_dict_or_data_path():
8280

8381
def test_validate_json_schema_invalid_data_path_format():
8482
"""
85-
Raise ValueError if data_path is not a file path
83+
Raise ValueError if data_path is not a string or file path
8684
"""
8785
pipeline_config_schema = "tests/test_cases/pipeline_config_schema.json"
8886
pipeline_config = ["Invalid", "data", "format"]
8987
with pytest.raises(ValueError) as err:
9088
validate_json_schema(
9189
schema_path=pipeline_config_schema, data_path=pipeline_config
9290
)
93-
assert "Invalid data format" in str(err.value)
91+
assert (
92+
"Invalid data format, `data_path` should be a pathlib.Path or string of file location"
93+
in str(err.value)
94+
)
9495

9596

9697
def test_validate_json_schema_invalid_data_dict_format():
@@ -103,12 +104,14 @@ def test_validate_json_schema_invalid_data_dict_format():
103104
validate_json_schema(
104105
schema_path=pipeline_config_schema, data_dict=pipeline_config
105106
)
106-
assert "Invalid data format" in str(err.value)
107+
assert "Invalid data format, `data_dict` should be a Python dictionary" in str(
108+
err.value
109+
)
107110

108111

109112
def test_validate_json_schema_url():
110113
"""
111-
Raise NotImplementedError if schema path is a URL (i.e. not a local file)
114+
Raise NotImplementedError if `schema_path` is a URL (i.e. not a local file)
112115
"""
113116
pipeline_config_schema = "http://example.org"
114117
pipeline_config = "tests/test_cases/pipeline_config.json"

0 commit comments

Comments
 (0)