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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: raisefunction
3
- Version: 1.0.4
3
+ Version: 1.0.5
4
4
  Summary: This project provides a function that raises errors passed to it.
5
5
  Author-email: Johannes <johannes.programming@gmail.com>
6
6
  License: The MIT License (MIT)
@@ -25,7 +25,7 @@ keywords = []
25
25
  name = "raisefunction"
26
26
  readme = "README.rst"
27
27
  requires-python = ">=3.11"
28
- version = "1.0.4"
28
+ version = "1.0.5"
29
29
 
30
30
  [project.license]
31
31
  file = "LICENSE.txt"
@@ -0,0 +1,2 @@
1
+ from raisefunction.core import raisefunction
2
+ from raisefunction.tests import test
@@ -6,13 +6,18 @@ DEFAULT = object()
6
6
 
7
7
 
8
8
  @overload
9
- def raisefunction(exc: BaseException) -> None: ...
9
+ def raisefunction(
10
+ exc: BaseException | type[BaseException],
11
+ ) -> Never: ...
10
12
  @overload
11
- def raisefunction(exc: BaseException, cause: Optional[BaseException]) -> None: ...
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__ = ["TestRaiseFunction"]
6
+ __all__ = ["Test0"]
7
7
 
8
8
 
9
- class TestRaiseFunction(unittest.TestCase):
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) as context:
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) as context:
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()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: raisefunction
3
- Version: 1.0.4
3
+ Version: 1.0.5
4
4
  Summary: This project provides a function that raises errors passed to it.
5
5
  Author-email: Johannes <johannes.programming@gmail.com>
6
6
  License: The MIT License (MIT)
@@ -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
@@ -1,5 +0,0 @@
1
- from raisefunction.core import *
2
- from raisefunction.tests import *
3
-
4
- if __name__ == "__main__":
5
- main()
File without changes
File without changes
File without changes
File without changes