raisefunction 1.0.4__tar.gz → 1.0.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {raisefunction-1.0.4/src/raisefunction.egg-info → raisefunction-1.0.5}/PKG-INFO +1 -1
- {raisefunction-1.0.4 → raisefunction-1.0.5}/pyproject.toml +1 -1
- raisefunction-1.0.5/src/raisefunction/__init__.py +2 -0
- {raisefunction-1.0.4 → raisefunction-1.0.5}/src/raisefunction/core/__init__.py +8 -3
- {raisefunction-1.0.4 → raisefunction-1.0.5}/src/raisefunction/tests/test_0.py +4 -4
- raisefunction-1.0.5/src/raisefunction/tests/test_1.py +62 -0
- raisefunction-1.0.5/src/raisefunction/tests/test_2.py +76 -0
- raisefunction-1.0.5/src/raisefunction/tests/test_3.py +42 -0
- {raisefunction-1.0.4 → raisefunction-1.0.5/src/raisefunction.egg-info}/PKG-INFO +1 -1
- {raisefunction-1.0.4 → raisefunction-1.0.5}/src/raisefunction.egg-info/SOURCES.txt +4 -1
- raisefunction-1.0.4/src/raisefunction/__init__.py +0 -5
- {raisefunction-1.0.4 → raisefunction-1.0.5}/LICENSE.txt +0 -0
- {raisefunction-1.0.4 → raisefunction-1.0.5}/MANIFEST.in +0 -0
- {raisefunction-1.0.4 → raisefunction-1.0.5}/README.rst +0 -0
- {raisefunction-1.0.4 → raisefunction-1.0.5}/setup.cfg +0 -0
- {raisefunction-1.0.4 → raisefunction-1.0.5}/src/raisefunction/tests/__init__.py +0 -0
- {raisefunction-1.0.4 → raisefunction-1.0.5}/src/raisefunction.egg-info/dependency_links.txt +0 -0
- {raisefunction-1.0.4 → raisefunction-1.0.5}/src/raisefunction.egg-info/top_level.txt +0 -0
|
@@ -6,13 +6,18 @@ DEFAULT = object()
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
@overload
|
|
9
|
-
def raisefunction(
|
|
9
|
+
def raisefunction(
|
|
10
|
+
exc: BaseException | type[BaseException],
|
|
11
|
+
) -> Never: ...
|
|
10
12
|
@overload
|
|
11
|
-
def raisefunction(
|
|
13
|
+
def raisefunction(
|
|
14
|
+
exc: BaseException | type[BaseException],
|
|
15
|
+
cause: Optional[BaseException],
|
|
16
|
+
) -> Never: ...
|
|
12
17
|
|
|
13
18
|
|
|
14
19
|
def raisefunction(
|
|
15
|
-
exc: BaseException,
|
|
20
|
+
exc: BaseException | type[BaseException],
|
|
16
21
|
cause: Optional[BaseException] | object = DEFAULT,
|
|
17
22
|
) -> Never:
|
|
18
23
|
"This function raises the given exception."
|
|
@@ -3,19 +3,19 @@ from typing import *
|
|
|
3
3
|
|
|
4
4
|
from raisefunction.core import raisefunction
|
|
5
5
|
|
|
6
|
-
__all__ = ["
|
|
6
|
+
__all__ = ["Test0"]
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
class
|
|
9
|
+
class Test0(unittest.TestCase):
|
|
10
10
|
def test_dispatcher_no_arguments(self: Self) -> None:
|
|
11
11
|
"Test the dispatcher raises a TypeError with no arguments."
|
|
12
|
-
with self.assertRaises(TypeError)
|
|
12
|
+
with self.assertRaises(TypeError):
|
|
13
13
|
raisefunction()
|
|
14
14
|
# self.assertIn("requires at least 1 positional argument", str(context.exception))
|
|
15
15
|
|
|
16
16
|
def test_dispatcher_too_many_arguments(self: Self) -> None:
|
|
17
17
|
"Test the dispatcher raises a TypeError with more than 2 arguments."
|
|
18
|
-
with self.assertRaises(TypeError)
|
|
18
|
+
with self.assertRaises(TypeError):
|
|
19
19
|
raisefunction(1, 2, 3)
|
|
20
20
|
# self.assertIn("takes at most 2 positional arguments", str(context.exception))
|
|
21
21
|
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from typing import *
|
|
3
|
+
|
|
4
|
+
from raisefunction import core
|
|
5
|
+
|
|
6
|
+
__all__ = ["Test1"]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Test1(unittest.TestCase):
|
|
10
|
+
def test_raises_given_exception_instance_without_cause(self: Self) -> None:
|
|
11
|
+
exc: ValueError
|
|
12
|
+
exc = ValueError("boom")
|
|
13
|
+
|
|
14
|
+
with self.assertRaises(ValueError) as cm:
|
|
15
|
+
core.raisefunction(exc)
|
|
16
|
+
|
|
17
|
+
self.assertIs(cm.exception, exc)
|
|
18
|
+
# When no explicit cause is given, __cause__ is normally None
|
|
19
|
+
# and __context__ may or may not be set depending on context.
|
|
20
|
+
self.assertIsNone(cm.exception.__cause__)
|
|
21
|
+
|
|
22
|
+
def test_raises_given_exception_class_without_cause(self: Self) -> None:
|
|
23
|
+
# Using a class mirrors: raise ValueError
|
|
24
|
+
with self.assertRaises(ValueError) as cm:
|
|
25
|
+
core.raisefunction(ValueError)
|
|
26
|
+
self.assertIsInstance(cm.exception, ValueError)
|
|
27
|
+
self.assertIsNone(cm.exception.__cause__)
|
|
28
|
+
|
|
29
|
+
def test_raises_with_explicit_cause(self: Self) -> None:
|
|
30
|
+
cause: RuntimeError
|
|
31
|
+
exc: ValueError
|
|
32
|
+
cause = RuntimeError("original")
|
|
33
|
+
exc = ValueError("wrapped")
|
|
34
|
+
|
|
35
|
+
with self.assertRaises(ValueError) as cm:
|
|
36
|
+
core.raisefunction(exc, cause)
|
|
37
|
+
|
|
38
|
+
self.assertIs(cm.exception.__cause__, cause)
|
|
39
|
+
|
|
40
|
+
def test_raises_with_suppressed_context_when_cause_is_none(self: Self) -> None:
|
|
41
|
+
exc: ValueError
|
|
42
|
+
exc = ValueError("no context")
|
|
43
|
+
|
|
44
|
+
# Explicit `from None` should result in __cause__ being None,
|
|
45
|
+
# and suppress linking to any active exception.
|
|
46
|
+
with self.assertRaises(ValueError) as cm:
|
|
47
|
+
core.raisefunction(exc, None)
|
|
48
|
+
|
|
49
|
+
self.assertIsNone(cm.exception.__cause__)
|
|
50
|
+
|
|
51
|
+
def test_raising_class_that_requires_args_produces_type_error(self: Self) -> None:
|
|
52
|
+
class NeedsArg(Exception):
|
|
53
|
+
def __init__(self: Self, msg: str) -> None:
|
|
54
|
+
super().__init__(msg)
|
|
55
|
+
|
|
56
|
+
# This mirrors normal `raise NeedsArg` behaviour
|
|
57
|
+
with self.assertRaises(TypeError):
|
|
58
|
+
core.raisefunction(NeedsArg)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
if __name__ == "__main__":
|
|
62
|
+
unittest.main()
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from typing import *
|
|
3
|
+
|
|
4
|
+
from raisefunction import core
|
|
5
|
+
|
|
6
|
+
__all__ = ["Test2"]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Test2(unittest.TestCase):
|
|
10
|
+
def test_raises_given_exception_instance_without_cause(self: Self) -> None:
|
|
11
|
+
cm: unittest._AssertRaisesContext[ValueError]
|
|
12
|
+
exc: ValueError
|
|
13
|
+
exc = ValueError("boom")
|
|
14
|
+
|
|
15
|
+
with self.assertRaises(ValueError) as cm:
|
|
16
|
+
core.raisefunction(exc)
|
|
17
|
+
|
|
18
|
+
self.assertIs(cm.exception, exc)
|
|
19
|
+
# When no explicit cause is given, __cause__ is normally None.
|
|
20
|
+
# __context__ may or may not be set depending on context.
|
|
21
|
+
self.assertIsNone(cm.exception.__cause__)
|
|
22
|
+
|
|
23
|
+
def test_raises_given_exception_class_without_cause(self: Self) -> None:
|
|
24
|
+
cm: unittest._AssertRaisesContext[ValueError]
|
|
25
|
+
with self.assertRaises(ValueError) as cm:
|
|
26
|
+
core.raisefunction(ValueError)
|
|
27
|
+
|
|
28
|
+
self.assertIsInstance(cm.exception, ValueError)
|
|
29
|
+
self.assertIsNone(cm.exception.__cause__)
|
|
30
|
+
|
|
31
|
+
def test_raises_with_explicit_cause(self: Self) -> None:
|
|
32
|
+
cause: RuntimeError
|
|
33
|
+
cm: unittest._AssertRaisesContext[ValueError]
|
|
34
|
+
exc: ValueError
|
|
35
|
+
cause = RuntimeError("original")
|
|
36
|
+
exc = ValueError("wrapped")
|
|
37
|
+
|
|
38
|
+
with self.assertRaises(ValueError) as cm:
|
|
39
|
+
core.raisefunction(exc, cause)
|
|
40
|
+
|
|
41
|
+
self.assertIs(cm.exception.__cause__, cause)
|
|
42
|
+
|
|
43
|
+
def test_raise_from_None(self: Self) -> None:
|
|
44
|
+
cm: unittest._AssertRaisesContext[ValueError]
|
|
45
|
+
exc: ValueError
|
|
46
|
+
one: int
|
|
47
|
+
zero: int
|
|
48
|
+
|
|
49
|
+
exc = ValueError("no context")
|
|
50
|
+
with self.assertRaises(ValueError) as cm:
|
|
51
|
+
try:
|
|
52
|
+
# Create a real context exception
|
|
53
|
+
one = 1
|
|
54
|
+
zero = 0
|
|
55
|
+
one / zero
|
|
56
|
+
except ZeroDivisionError:
|
|
57
|
+
raise exc from None
|
|
58
|
+
self.assertIsNone(cm.exception.__cause__)
|
|
59
|
+
self.assertIsInstance(cm.exception.__context__, ZeroDivisionError)
|
|
60
|
+
self.assertTrue(cm.exception.__suppress_context__)
|
|
61
|
+
|
|
62
|
+
def test_raising_class_that_requires_args_produces_type_error(self: Self) -> None:
|
|
63
|
+
class NeedsArg(Exception):
|
|
64
|
+
def __init__(self: Self, msg: str) -> None:
|
|
65
|
+
super().__init__(msg)
|
|
66
|
+
|
|
67
|
+
cm: unittest._AssertRaisesContext[BaseException]
|
|
68
|
+
with self.assertRaises(TypeError) as cm:
|
|
69
|
+
core.raisefunction(NeedsArg)
|
|
70
|
+
|
|
71
|
+
# Optional: sanity check on the error type/message
|
|
72
|
+
self.assertIsInstance(cm.exception, TypeError)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
if __name__ == "__main__":
|
|
76
|
+
unittest.main()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from typing import *
|
|
3
|
+
|
|
4
|
+
from raisefunction import core
|
|
5
|
+
|
|
6
|
+
__all__ = ["Test3"]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Test3(unittest.TestCase):
|
|
10
|
+
|
|
11
|
+
def go(self: Self, *, use_func: bool) -> None:
|
|
12
|
+
cm: unittest._AssertRaisesContext[ValueError]
|
|
13
|
+
exc: ValueError
|
|
14
|
+
exc = ValueError("no context")
|
|
15
|
+
with self.assertRaises(ValueError) as cm:
|
|
16
|
+
self.go_with(use_func=use_func, exc=exc)
|
|
17
|
+
self.assertIsNone(cm.exception.__cause__)
|
|
18
|
+
self.assertIsInstance(cm.exception.__context__, ZeroDivisionError)
|
|
19
|
+
self.assertTrue(cm.exception.__suppress_context__)
|
|
20
|
+
|
|
21
|
+
def go_with(self: Self, *, use_func: bool, exc: Exception) -> None:
|
|
22
|
+
one: int
|
|
23
|
+
zero: int
|
|
24
|
+
try:
|
|
25
|
+
# Create a real context exception
|
|
26
|
+
one = 1
|
|
27
|
+
zero = 0
|
|
28
|
+
one / zero
|
|
29
|
+
except ZeroDivisionError:
|
|
30
|
+
# This should behave like: raise exc from None
|
|
31
|
+
if use_func:
|
|
32
|
+
core.raisefunction(exc, None)
|
|
33
|
+
else:
|
|
34
|
+
raise exc from None
|
|
35
|
+
|
|
36
|
+
def test_raise_from_None(self: Self) -> None:
|
|
37
|
+
self.go(use_func=False)
|
|
38
|
+
self.go(use_func=True)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
unittest.main()
|
|
@@ -10,4 +10,7 @@ src/raisefunction.egg-info/dependency_links.txt
|
|
|
10
10
|
src/raisefunction.egg-info/top_level.txt
|
|
11
11
|
src/raisefunction/core/__init__.py
|
|
12
12
|
src/raisefunction/tests/__init__.py
|
|
13
|
-
src/raisefunction/tests/test_0.py
|
|
13
|
+
src/raisefunction/tests/test_0.py
|
|
14
|
+
src/raisefunction/tests/test_1.py
|
|
15
|
+
src/raisefunction/tests/test_2.py
|
|
16
|
+
src/raisefunction/tests/test_3.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|