Add error tests in doctest and fix error message (#10930)

* Add error tests in doctest and fix error message

* Change AssertationError to ValueError

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Dale Dai 2023-10-25 22:27:46 -07:00 committed by GitHub
parent 0ffe506ea7
commit 0e7f8284a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,12 +29,19 @@ def is_prime(number: int) -> bool:
True True
>>> is_prime(67483) >>> is_prime(67483)
False False
>>> is_prime(16.1)
Traceback (most recent call last):
...
ValueError: is_prime() only accepts positive integers
>>> is_prime(-4)
Traceback (most recent call last):
...
ValueError: is_prime() only accepts positive integers
""" """
# precondition # precondition
assert isinstance(number, int) and ( if not isinstance(number, int) or not number >= 0:
number >= 0 raise ValueError("is_prime() only accepts positive integers")
), "'number' must been an int and positive"
if 1 < number < 4: if 1 < number < 4:
# 2 and 3 are primes # 2 and 3 are primes
@ -64,7 +71,7 @@ class Test(unittest.TestCase):
assert is_prime(29) assert is_prime(29)
def test_not_primes(self): def test_not_primes(self):
with pytest.raises(AssertionError): with pytest.raises(ValueError):
is_prime(-19) is_prime(-19)
assert not is_prime( assert not is_prime(
0 0