Skip to content

Commit

Permalink
Merge pull request #13 from marcinzaremba/invalid-error-parsed-type
Browse files Browse the repository at this point in the history
Throw ValueError for parsed types too
  • Loading branch information
jmcs authored Aug 17, 2018
2 parents 0f7e4c3 + 44174ab commit 96ee1ca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage.xml
tox.ini
*.egg-info
**/__pycache__
.pytest_cache/
2 changes: 1 addition & 1 deletion ecological/autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def get(self, wanted_type: WantedType) -> Union[WantedType, Any]:

try:
value = self.transform(raw_value, wanted_type)
except ValueError as e:
except (ValueError, SyntaxError) as e:
raise ValueError(f"Invalid configuration for '{self.name}': {e}.")

return value
Expand Down
14 changes: 11 additions & 3 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,20 @@ class Configuration(ecological.AutoConfig, prefix="prefix"):
assert Configuration.not_default == "Not Default"


def test_invalid_value(monkeypatch):
monkeypatch.setenv("INVALID", "Invalid integer")
def test_invalid_value_regular_type(monkeypatch):
monkeypatch.setenv("PARAM_REGULAR_TYPE", "not an integer")

with pytest.raises(ValueError):
class Configuration(ecological.AutoConfig):
invalid: int
param_regular_type: int


def test_invalid_value_parsed_type(monkeypatch):
monkeypatch.setenv("PARAM_PARSED_TYPE", "not a list")

with pytest.raises(ValueError):
class Configuration(ecological.AutoConfig):
param_parsed_type: list = ['param_1', 'param_2']


def test_no_default():
Expand Down

0 comments on commit 96ee1ca

Please sign in to comment.