Skip to content

Commit f23d559

Browse files
stylistic fixes
1 parent 5fec4dd commit f23d559

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# assertions.py
1+
# pyassertions
22

33
A small assertions library for Python that I coded for fun.

assertions/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from .assertions import *
22

3-
__version__ = "1.0"
4-
53
__all__ = [
6-
"__version__",
74
"equals",
85
"not_equals",
96
"expect",

assertions/assertions.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def equals(
3737
print("Test passed")
3838

3939
elif a != b:
40-
raise AssertionError(message_on_fail + f": expected {a} to equal {b}")
40+
raise AssertionError(f"{message_on_fail}: expected {a} to equal {b}")
4141

4242
def not_equals(
4343
a: Any,
@@ -58,7 +58,7 @@ def not_equals(
5858
print("Test passed")
5959

6060
elif a == b:
61-
raise AssertionError(message_on_fail + f": expected {a} to not equal {b}")
61+
raise AssertionError(f"{message_on_fail}: expected {a} to not equal {b}")
6262

6363
def expect(
6464
value: Any,
@@ -84,8 +84,8 @@ def expect(
8484
def raises(
8585
function: Callable[..., Any],
8686
exceptions: Tuple[Type[BaseException], ...] | Type[BaseException] = (Exception,),
87-
message_on_fail = "Test failed",
88-
verbose = False,
87+
message_on_fail: str = "Test failed",
88+
verbose: bool = False,
8989
) -> None:
9090
"""
9191
Assertion of a function raising an exception.
@@ -100,11 +100,13 @@ def raises(
100100
exceptions = (exceptions,)
101101

102102
def handle():
103+
expected_exceptions = list(map(lambda e: e.__name__, exceptions))
104+
103105
raise AssertionError(
104106
message_on_fail
105107
+ ": expected given function to raise "
106108
+ ("" if len(exceptions) == 1 else "any of the following: ")
107-
+ ", ".join(map(lambda e: e.__name__, exceptions))
109+
+ ", ".join(expected_exceptions)
108110
)
109111

110112
try:
@@ -113,8 +115,6 @@ def handle():
113115
except exceptions:
114116
if verbose:
115117
print("Test passed")
116-
117-
return None
118118

119119
except:
120120
# handle cases when the exception is not of the expected type
@@ -144,11 +144,13 @@ def does_not_raise(
144144
exceptions = (exceptions,)
145145

146146
def handle():
147+
avoided_exceptions = list(map(lambda e: e.__name__, exceptions))
148+
147149
raise AssertionError(
148150
message_on_fail
149151
+ ": expected given function to not raise "
150152
+ ("" if len(exceptions) == 1 else "any of the following: ")
151-
+ ", ".join(map(lambda e: e.__name__, exceptions))
153+
+ ", ".join(avoided_exceptions)
152154
)
153155

154156
try:
@@ -158,7 +160,7 @@ def handle():
158160
handle()
159161

160162
except:
161-
# handle cases when the exception is not of the expected type
163+
# handle other types of exceptions
162164
if verbose:
163165
print("Test passed")
164166

@@ -187,7 +189,7 @@ def approximately_equals(
187189
print("Test passed")
188190

189191
elif abs(a-b) > margin:
190-
raise AssertionError(message_on_fail + f": expected {a} to be within {margin} of {b}")
192+
raise AssertionError(f"{message_on_fail}: expected {a} to be within {margin} of {b}")
191193

192194
def not_approximately_equals(
193195
a: int | float,
@@ -210,7 +212,7 @@ def not_approximately_equals(
210212
print("Test passed")
211213

212214
elif abs(a-b) <= margin:
213-
raise AssertionError(message_on_fail + f": expected {a} to not be within {margin} of {b}")
215+
raise AssertionError(f"{message_on_fail}: expected {a} to not be within {margin} of {b}")
214216

215217
def contains(
216218
it: Iterable[Any],
@@ -231,7 +233,7 @@ def contains(
231233
print("Test passed")
232234

233235
elif value not in it:
234-
raise AssertionError(message_on_fail + f": expected {value} to be in {it}")
236+
raise AssertionError(f"{message_on_fail}: expected {value} to be in {it}")
235237

236238
def does_not_contain(
237239
it: Iterable[Any],
@@ -252,7 +254,7 @@ def does_not_contain(
252254
print("Test passed")
253255

254256
elif value in it:
255-
raise AssertionError(message_on_fail + f": expected {value} to not be in {it}")
257+
raise AssertionError(f"{message_on_fail}: expected {value} to not be in {it}")
256258

257259
def is_instance(
258260
value: Any,
@@ -275,7 +277,7 @@ def is_instance(
275277
print("Test passed")
276278

277279
elif not isinstance(value, types):
278-
raise AssertionError(message_on_fail + f": expected {value} to be an instance of {types}")
280+
raise AssertionError(f"{message_on_fail}: expected {value} to be an instance of {types}")
279281

280282
def not_is_instance(
281283
value: Any,
@@ -298,7 +300,7 @@ def not_is_instance(
298300
print("Test passed")
299301

300302
elif isinstance(value, types):
301-
raise AssertionError(message_on_fail + f": expected {value} to not be an instance of {types}")
303+
raise AssertionError(f"{message_on_fail}: expected {value} to not be an instance of {types}")
302304

303305
def greater(
304306
value: int | float,
@@ -319,7 +321,7 @@ def greater(
319321
print("Test passed")
320322

321323
elif value <= comparison:
322-
raise AssertionError(message_on_fail + f": expected {value} to be greater than {comparison}")
324+
raise AssertionError(f"{message_on_fail}: expected {value} to be greater than {comparison}")
323325

324326
def less(
325327
value: int | float,
@@ -340,4 +342,4 @@ def less(
340342
print("Test passed")
341343

342344
elif value >= comparison:
343-
raise AssertionError(message_on_fail + f": expected {value} to be less than {comparison}")
345+
raise AssertionError(f"{message_on_fail}: expected {value} to be less than {comparison}")

setup.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
from setuptools import setup, find_packages
2-
from pathlib import Path
3-
4-
script_dir = Path(__file__).parent
5-
6-
def get_version():
7-
with (script_dir / "assertions" / "__init__.py").open("r") as f:
8-
for line in f:
9-
if line.startswith("__version__"):
10-
return line.split("=")[1].strip().strip('"')
112

123
setup(
13-
name="assertions",
14-
version=get_version(),
4+
name="pyassertions",
5+
version="1.1",
156
packages=find_packages(),
167
install_requires=[],
178
)

0 commit comments

Comments
 (0)