Browse Source

Working on mypy --strict, some exception handling improvements

typer
Teran McKinney 3 weeks ago
parent
commit
3e6e29ed4c
  1. 1
      Makefile
  2. 20
      src/sporestack/validate.py
  3. 12
      tests/validate_test.py

1
Makefile

@ -5,6 +5,7 @@ test:
black --check .
flake8 .
mypy .
mypy --strict . || true
pytest
install:

20
src/sporestack/validate.py

@ -155,14 +155,22 @@ def region(region: Optional[str]) -> None:
def affiliate_amount(amount: Optional[int]) -> None:
msg = "affiliate_amount must be null or non-zero unsigned int."
if amount is None:
return
if is_unsigned_int(amount) is True:
if amount != 0:
return
raise TypeError("affiliate_amount must be null or non-zero unsigned int.")
if isinstance(amount, int):
if is_unsigned_int(amount) is True:
if amount != 0:
return
raise ValueError(msg)
else:
raise TypeError(msg)
def cents(cents: int) -> None:
if not is_unsigned_int(cents):
raise TypeError("cents must be unsigned integer.")
msg = "cents must be unsigned integer."
if isinstance(cents, int):
if not is_unsigned_int(cents):
raise ValueError(msg)
else:
raise TypeError(msg)

12
tests/validate_test.py

@ -66,8 +66,8 @@ def test_cents() -> None:
validate.cents(0)
validate.cents(10)
validate.cents(1000000)
with pytest.raises(TypeError):
validate.cents(-1) # type: ignore
with pytest.raises(ValueError):
validate.cents(-1)
with pytest.raises(TypeError):
validate.cents("a") # type: ignore
with pytest.raises(TypeError):
@ -193,9 +193,9 @@ def test_affiliate_amount() -> None:
validate.affiliate_amount(None)
validate.affiliate_amount(1)
validate.affiliate_amount(1000)
with pytest.raises(TypeError):
validate.affiliate_amount(0) # type: ignore
with pytest.raises(TypeError):
validate.affiliate_amount(-1) # type: ignore
with pytest.raises(ValueError):
validate.affiliate_amount(0)
with pytest.raises(ValueError):
validate.affiliate_amount(-1)
with pytest.raises(TypeError):
validate.affiliate_amount("string") # type: ignore
Loading…
Cancel
Save