cmp3 0.6.2__tar.gz → 0.6.3__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: cmp3
3
- Version: 0.6.2
3
+ Version: 0.6.3
4
4
  Summary: This project updates the concept of __cmp__ for python3.
5
5
  Author-email: Johannes <johannes.programming@gmail.com>
6
6
  License: The MIT License (MIT)
@@ -26,7 +26,7 @@ keywords = []
26
26
  name = "cmp3"
27
27
  readme = "README.rst"
28
28
  requires-python = ">=3.11"
29
- version = "0.6.2"
29
+ version = "0.6.3"
30
30
 
31
31
  [project.license]
32
32
  file = "LICENSE.txt"
@@ -11,14 +11,22 @@ def cmp(x: Any, y: Any, /, *, mode: str = "portingguide") -> float | int:
11
11
  if mode == "portingguide":
12
12
  return (x > y) - (x < y)
13
13
  if mode == "poset":
14
- if (x <= y) and (y <= x):
14
+ return cmp_poset(x, y)
15
+ raise ValueError("%r is not an appropriate value for mode." % mode)
16
+
17
+
18
+ def cmp_poset(x: Any, y: Any, /) -> float | int:
19
+ "This function returns a value that compares to 0 as x compares to y under the assumption that both arguments belong to a poset."
20
+ if x <= y:
21
+ if y <= x:
15
22
  return 0
16
- if (x <= y) and (not y <= x):
23
+ else:
17
24
  return -1
18
- if (not x <= y) and (y <= x):
25
+ else:
26
+ if y <= x:
19
27
  return 1
20
- return float("nan")
21
- raise ValueError
28
+ else:
29
+ return float("nan")
22
30
 
23
31
 
24
32
  def cmpDeco(cls: type, /) -> type:
@@ -0,0 +1,118 @@
1
+ import math
2
+ import unittest
3
+ from typing import *
4
+
5
+ from cmp3 import core
6
+
7
+
8
+ class TestCmpPortingGuide(unittest.TestCase):
9
+ def test_equal(self: Self) -> None:
10
+ self.assertEqual(core.cmp(1, 1), 0)
11
+ self.assertEqual(core.cmp("a", "a"), 0)
12
+
13
+ def test_less(self: Self) -> None:
14
+ self.assertEqual(core.cmp(1, 2), -1)
15
+ self.assertEqual(core.cmp("a", "b"), -1)
16
+
17
+ def test_greater(self: Self) -> None:
18
+ self.assertEqual(core.cmp(2, 1), 1)
19
+ self.assertEqual(core.cmp("b", "a"), 1)
20
+
21
+ def test_invalid_mode_raises(self: Self) -> None:
22
+ with self.assertRaises(ValueError):
23
+ core.cmp(1, 2, mode="invalid-mode")
24
+
25
+
26
+ class TestCmpPoset(unittest.TestCase):
27
+ def test_poset_equal(self: Self) -> None:
28
+ # subset ordering: same set
29
+ self.assertEqual(core.cmp_poset({1, 2}, {1, 2}), 0)
30
+
31
+ def test_poset_less(self: Self) -> None:
32
+ # subset ordering: strict subset
33
+ self.assertEqual(core.cmp_poset({1}, {1, 2}), -1)
34
+
35
+ def test_poset_greater(self: Self) -> None:
36
+ # subset ordering: strict superset
37
+ self.assertEqual(core.cmp_poset({1, 2}, {1}), 1)
38
+
39
+ def test_poset_incomparable(self: Self) -> None:
40
+ # incomparable in subset ordering
41
+ res = core.cmp_poset({1}, {2})
42
+ self.assertTrue(math.isnan(res))
43
+
44
+
45
+ class TestCmpModePoset(unittest.TestCase):
46
+ def test_cmp_uses_poset_mode(self: Self) -> None:
47
+ self.assertEqual(core.cmp({1}, {1, 2}, mode="poset"), -1)
48
+ self.assertEqual(core.cmp({1, 2}, {1}, mode="poset"), 1)
49
+ self.assertTrue(math.isnan(core.cmp({1}, {2}, mode="poset")))
50
+
51
+
52
+ class Number(core.CmpABC):
53
+ def __init__(self: Self, value: int) -> None:
54
+ self.value = value
55
+
56
+ def __cmp__(self: Self, other: Any) -> Any:
57
+ if not isinstance(other, Number):
58
+ return NotImplemented
59
+ # classic 3-way comparison
60
+ return (self.value > other.value) - (self.value < other.value)
61
+
62
+
63
+ class TestCmpDecoAndCmpABC(unittest.TestCase):
64
+ def setUp(self: Self) -> None:
65
+ self.a = Number(1)
66
+ self.b = Number(2)
67
+ self.c = Number(1)
68
+
69
+ def test_eq_ne(self: Self) -> None:
70
+ self.assertTrue(self.a == self.c)
71
+ self.assertFalse(self.a == self.b)
72
+ self.assertTrue(self.a != self.b)
73
+ self.assertFalse(self.a != self.c)
74
+
75
+ def test_lt_le(self: Self) -> None:
76
+ self.assertTrue(self.a < self.b)
77
+ self.assertTrue(self.a <= self.b)
78
+ self.assertTrue(self.a <= self.c)
79
+ self.assertFalse(self.b < self.a)
80
+
81
+ def test_gt_ge(self: Self) -> None:
82
+ self.assertTrue(self.b > self.a)
83
+ self.assertTrue(self.b >= self.a)
84
+ self.assertTrue(self.b >= self.c)
85
+ self.assertFalse(self.a > self.b)
86
+
87
+ def test_cmpabc_is_abstract(self: Self) -> None:
88
+ with self.assertRaises(TypeError):
89
+ core.CmpABC() # abstract, cannot be instantiated
90
+
91
+
92
+ class TestCmpDecoOnPlainClass(unittest.TestCase):
93
+ def test_decorator_on_non_abc_class(self: Self) -> None:
94
+ @core.cmpDeco
95
+ class Plain:
96
+ def __init__(self: Self, x: int) -> None:
97
+ self.x = x
98
+
99
+ def __cmp__(self: Self, other: Any) -> Any:
100
+ if not isinstance(other, Plain):
101
+ return NotImplemented
102
+ return (self.x > other.x) - (self.x < other.x)
103
+
104
+ p1: Plain
105
+ p2: Plain
106
+ p3: Plain
107
+ p1 = Plain(1)
108
+ p2 = Plain(2)
109
+ p3 = Plain(1)
110
+
111
+ self.assertTrue(p1 < p2)
112
+ self.assertTrue(p2 > p1)
113
+ self.assertTrue(p1 == p3)
114
+ self.assertTrue(p1 != p2)
115
+
116
+
117
+ if __name__ == "__main__":
118
+ unittest.main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmp3
3
- Version: 0.6.2
3
+ Version: 0.6.3
4
4
  Summary: This project updates the concept of __cmp__ for python3.
5
5
  Author-email: Johannes <johannes.programming@gmail.com>
6
6
  License: The MIT License (MIT)
@@ -11,4 +11,4 @@ src/cmp3.egg-info/requires.txt
11
11
  src/cmp3.egg-info/top_level.txt
12
12
  src/cmp3/core/__init__.py
13
13
  src/cmp3/tests/__init__.py
14
- src/cmp3/tests/test_1984.py
14
+ src/cmp3/tests/test_cmp3_core.py
@@ -1,11 +0,0 @@
1
- import unittest
2
- from typing import *
3
-
4
-
5
- class Test1984(unittest.TestCase):
6
- def test_1984(self: Self) -> None:
7
- self.assertEqual(2 + 2, 4, "Ignorance is Strength")
8
-
9
-
10
- if __name__ == "__main__":
11
- unittest.main()
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes