Skip to content

Commit 1f312ef

Browse files
committed
correct formal objects equality
1 parent 510e04b commit 1f312ef

File tree

8 files changed

+27
-60
lines changed

8 files changed

+27
-60
lines changed

pyformlang/objects/base_epsilon.py

+3
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ def __hash__(self) -> int:
3030

3131
def __repr__(self) -> str:
3232
return "epsilon"
33+
34+
def _is_equal_to(self, other: FormalObject) -> bool:
35+
return isinstance(other, BaseEpsilon)

pyformlang/objects/base_terminal.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
""" General terminal representation """
22

3-
from typing import Any
43
from abc import abstractmethod
54

65
from .formal_object import FormalObject
@@ -9,16 +8,9 @@
98
class BaseTerminal(FormalObject):
109
""" General terminal representation """
1110

12-
def __eq__(self, other: Any) -> bool:
13-
if isinstance(other, BaseTerminal):
14-
return self.value == other.value
15-
if isinstance(other, FormalObject):
16-
return False
17-
return self.value == other
18-
19-
def __hash__(self) -> int:
20-
return super().__hash__()
21-
2211
@abstractmethod
2312
def __repr__(self):
2413
raise NotImplementedError
14+
15+
def _is_equal_to(self, other: FormalObject) -> bool:
16+
return isinstance(other, BaseTerminal) and self.value == other.value
+3-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
""" A variable in a CFG """
22

3-
from typing import Any
43
from string import ascii_uppercase
54

65
from .cfg_object import CFGObject
@@ -16,16 +15,6 @@ class Variable(CFGObject):
1615
The value of the variable
1716
"""
1817

19-
def __eq__(self, other: Any) -> bool:
20-
if isinstance(other, Variable):
21-
return self.value == other.value
22-
if isinstance(other, FormalObject):
23-
return False
24-
return self.value == other
25-
26-
def __hash__(self) -> int:
27-
return super().__hash__()
28-
2918
def __repr__(self) -> str:
3019
return f"Variable({self})"
3120

@@ -34,3 +23,6 @@ def to_text(self) -> str:
3423
if text and text[0] not in ascii_uppercase:
3524
return '"VAR:' + text + '"'
3625
return text
26+
27+
def _is_equal_to(self, other: FormalObject) -> bool:
28+
return isinstance(other, Variable) and self.value == other.value

pyformlang/objects/finite_automaton_objects/state.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Representation of a state in a finite state automaton
33
"""
44

5-
from typing import Any
6-
75
from .finite_automaton_object import FiniteAutomatonObject
86
from ..formal_object import FormalObject
97

@@ -24,15 +22,8 @@ class State(FiniteAutomatonObject):
2422
2523
"""
2624

27-
def __eq__(self, other: Any) -> bool:
28-
if isinstance(other, State):
29-
return self.value == other.value
30-
if isinstance(other, FormalObject):
31-
return False
32-
return self.value == other
33-
34-
def __hash__(self) -> int:
35-
return super().__hash__()
36-
3725
def __repr__(self) -> str:
3826
return f"State({self})"
27+
28+
def _is_equal_to(self, other: FormalObject) -> bool:
29+
return isinstance(other, State) and self.value == other.value

pyformlang/objects/formal_object.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ def value(self) -> Hashable:
2323
"""
2424
return self._value
2525

26-
@abstractmethod
2726
def __eq__(self, other: Any) -> bool:
28-
raise NotImplementedError
27+
if not isinstance(other, FormalObject):
28+
return self.value == other
29+
return self._is_equal_to(other) and other._is_equal_to(self)
2930

3031
def __hash__(self) -> int:
3132
if self._hash is None:
@@ -38,3 +39,7 @@ def __str__(self) -> str:
3839
@abstractmethod
3940
def __repr__(self) -> str:
4041
raise NotImplementedError
42+
43+
@abstractmethod
44+
def _is_equal_to(self, other: "FormalObject") -> bool:
45+
raise NotImplementedError
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
""" A StackSymbol in a pushdown automaton """
22

3-
from typing import Any
4-
53
from .symbol import Symbol
64
from ..formal_object import FormalObject
75

@@ -16,15 +14,8 @@ class StackSymbol(Symbol):
1614
1715
"""
1816

19-
def __eq__(self, other: Any) -> bool:
20-
if isinstance(other, StackSymbol):
21-
return self.value == other.value
22-
if isinstance(other, FormalObject):
23-
return False
24-
return self.value == other
25-
26-
def __hash__(self) -> int:
27-
return super().__hash__()
28-
2917
def __repr__(self) -> str:
3018
return f"StackSymbol({self})"
19+
20+
def _is_equal_to(self, other: FormalObject) -> bool:
21+
return isinstance(other, StackSymbol) and self.value == other.value
+3-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
""" A State in a pushdown automaton """
22

3-
from typing import Any
4-
53
from .pda_object import PDAObject
64
from ..formal_object import FormalObject
75

@@ -16,15 +14,8 @@ class State(PDAObject):
1614
1715
"""
1816

19-
def __eq__(self, other: Any) -> bool:
20-
if isinstance(other, State):
21-
return self.value == other.value
22-
if isinstance(other, FormalObject):
23-
return False
24-
return self.value == other
25-
26-
def __hash__(self) -> int:
27-
return super().__hash__()
28-
2917
def __repr__(self) -> str:
3018
return f"State({self})"
19+
20+
def _is_equal_to(self, other: FormalObject) -> bool:
21+
return isinstance(other, State) and self.value == other.value

pyformlang/pda/tests/test_pda.py

+2
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ def test_object_eq(self):
389389
assert StackSymbol("ABC") != Symbol("ABC")
390390
assert State("ABC") != FAState("ABC")
391391
assert Symbol("s") == Terminal("s")
392+
assert Terminal(1) != StackSymbol(1)
393+
assert StackSymbol(42) != FAState(42)
392394

393395
def test_contains(self, pda_example: PDA):
394396
""" Tests the transition containment checks """

0 commit comments

Comments
 (0)