From e27b2afaf2660fbc40a455a1b2a8f0f927a7418c Mon Sep 17 00:00:00 2001 From: Dale Dai Date: Tue, 24 Oct 2023 20:12:07 -0700 Subject: [PATCH 1/3] Add error tests in doctest and fix error message --- maths/prime_check.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/maths/prime_check.py b/maths/prime_check.py index c17877a57705..7169e0d9ccca 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -29,12 +29,20 @@ def is_prime(number: int) -> bool: True >>> is_prime(67483) False + >>> is_prime(16.1) + Traceback (most recent call last): + ... + AssertionError: is_prime() only accepts positive integers + >>> is_prime(-4) + Traceback (most recent call last): + ... + AssertionError: is_prime() only accepts positive integers """ # precondition assert isinstance(number, int) and ( number >= 0 - ), "'number' must been an int and positive" + ), "is_prime() only accepts positive integers" if 1 < number < 4: # 2 and 3 are primes From 5b5df87820878bac7c469405c9927cc7308ed167 Mon Sep 17 00:00:00 2001 From: Dale Dai Date: Wed, 25 Oct 2023 18:09:56 -0700 Subject: [PATCH 2/3] Change AssertationError to ValueError --- maths/prime_check.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/maths/prime_check.py b/maths/prime_check.py index 7169e0d9ccca..bcdd1a36a9e9 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -32,17 +32,16 @@ def is_prime(number: int) -> bool: >>> is_prime(16.1) Traceback (most recent call last): ... - AssertionError: is_prime() only accepts positive integers + ValueError: is_prime() only accepts positive integers >>> is_prime(-4) Traceback (most recent call last): ... - AssertionError: is_prime() only accepts positive integers + ValueError: is_prime() only accepts positive integers """ # precondition - assert isinstance(number, int) and ( - number >= 0 - ), "is_prime() only accepts positive integers" + if(not isinstance(number, int) or not number >= 0): + raise ValueError("is_prime() only accepts positive integers") if 1 < number < 4: # 2 and 3 are primes @@ -72,7 +71,7 @@ def test_primes(self): assert is_prime(29) def test_not_primes(self): - with pytest.raises(AssertionError): + with pytest.raises(ValueError): is_prime(-19) assert not is_prime( 0 From 4dc7a4427ec2df42e9e06c47fd3833efdbf33ed5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Oct 2023 01:10:42 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/prime_check.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/prime_check.py b/maths/prime_check.py index bcdd1a36a9e9..f1bc4def2469 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -40,7 +40,7 @@ def is_prime(number: int) -> bool: """ # precondition - if(not isinstance(number, int) or not number >= 0): + if not isinstance(number, int) or not number >= 0: raise ValueError("is_prime() only accepts positive integers") if 1 < number < 4: