From da118c3c7e4b114ed3dbe546171145b52808c261 Mon Sep 17 00:00:00 2001 From: Sam Weaver Date: Fri, 25 Oct 2024 10:21:17 -0400 Subject: [PATCH 1/3] Fix Symbol.__eq__ (fixes #1476) --- lark/grammar.py | 3 ++- tests/test_grammar.py | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lark/grammar.py b/lark/grammar.py index 1d226d9..cf4f40b 100644 --- a/lark/grammar.py +++ b/lark/grammar.py @@ -16,7 +16,8 @@ class Symbol(Serialize): self.name = name def __eq__(self, other): - assert isinstance(other, Symbol), other + if not isinstance(other, Symbol): + return False return self.is_term == other.is_term and self.name == other.name def __ne__(self, other): diff --git a/tests/test_grammar.py b/tests/test_grammar.py index 624b079..52425db 100644 --- a/tests/test_grammar.py +++ b/tests/test_grammar.py @@ -6,7 +6,7 @@ from unittest import TestCase, main from lark import Lark, Token, Tree, ParseError, UnexpectedInput from lark.load_grammar import GrammarError, GRAMMAR_ERRORS, find_grammar_errors, list_grammar_imports from lark.load_grammar import FromPackageLoader - +from lark.grammar import Symbol class TestGrammar(TestCase): def setUp(self): @@ -296,8 +296,11 @@ class TestGrammar(TestCase): p.parse('ab') + def test_symbol_eq(self): + a = None + b = Symbol("abc") - + self.assertNotEqual(a, b) if __name__ == '__main__': From 3ac48c1cd547a1971c83db15f1c285145a6fc3de Mon Sep 17 00:00:00 2001 From: Sam Weaver Date: Fri, 25 Oct 2024 13:30:46 -0400 Subject: [PATCH 2/3] Adjust behaviour in #1481 to raise a NotImplementedError --- lark/grammar.py | 6 +++++- tests/test_grammar.py | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lark/grammar.py b/lark/grammar.py index cf4f40b..fdf1c23 100644 --- a/lark/grammar.py +++ b/lark/grammar.py @@ -16,8 +16,12 @@ class Symbol(Serialize): self.name = name def __eq__(self, other): - if not isinstance(other, Symbol): + if other is None: return False + if not isinstance(other, Symbol): + raise NotImplementedError( + f"Comparing a Symbol with type {type(other).__name__} is not implemented" + ) return self.is_term == other.is_term and self.name == other.name def __ne__(self, other): diff --git a/tests/test_grammar.py b/tests/test_grammar.py index 52425db..5455b6b 100644 --- a/tests/test_grammar.py +++ b/tests/test_grammar.py @@ -302,6 +302,14 @@ class TestGrammar(TestCase): self.assertNotEqual(a, b) + a = dict() + + self.assertRaisesRegex( + NotImplementedError, + r"Comparing a Symbol with type dict is not implemented", + lambda: a == b, + ) + if __name__ == '__main__': main() From 9d35d1b5e15102f9b2462c30c05449d9eb8bad8c Mon Sep 17 00:00:00 2001 From: Sam Weaver Date: Fri, 25 Oct 2024 15:51:05 -0400 Subject: [PATCH 3/3] Adjust behaviour in #1481 to return NotImplemented --- lark/grammar.py | 6 +----- tests/test_grammar.py | 8 -------- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/lark/grammar.py b/lark/grammar.py index fdf1c23..016496b 100644 --- a/lark/grammar.py +++ b/lark/grammar.py @@ -16,12 +16,8 @@ class Symbol(Serialize): self.name = name def __eq__(self, other): - if other is None: - return False if not isinstance(other, Symbol): - raise NotImplementedError( - f"Comparing a Symbol with type {type(other).__name__} is not implemented" - ) + return NotImplemented return self.is_term == other.is_term and self.name == other.name def __ne__(self, other): diff --git a/tests/test_grammar.py b/tests/test_grammar.py index 5455b6b..52425db 100644 --- a/tests/test_grammar.py +++ b/tests/test_grammar.py @@ -302,14 +302,6 @@ class TestGrammar(TestCase): self.assertNotEqual(a, b) - a = dict() - - self.assertRaisesRegex( - NotImplementedError, - r"Comparing a Symbol with type dict is not implemented", - lambda: a == b, - ) - if __name__ == '__main__': main()