Merge pull request #1481 from weaversam8/bugfix/1476-symbol-eq

Fix `Symbol.__eq__` to return false when comparing with None
This commit is contained in:
Erez Shinan 2024-10-26 17:23:05 +03:00 committed by GitHub
commit 24f19a35f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 3 deletions

View File

@ -16,7 +16,8 @@ class Symbol(Serialize):
self.name = name self.name = name
def __eq__(self, other): def __eq__(self, other):
assert isinstance(other, Symbol), other if not isinstance(other, Symbol):
return NotImplemented
return self.is_term == other.is_term and self.name == other.name return self.is_term == other.is_term and self.name == other.name
def __ne__(self, other): def __ne__(self, other):

View File

@ -6,7 +6,7 @@ from unittest import TestCase, main
from lark import Lark, Token, Tree, ParseError, UnexpectedInput 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 GrammarError, GRAMMAR_ERRORS, find_grammar_errors, list_grammar_imports
from lark.load_grammar import FromPackageLoader from lark.load_grammar import FromPackageLoader
from lark.grammar import Symbol
class TestGrammar(TestCase): class TestGrammar(TestCase):
def setUp(self): def setUp(self):
@ -296,8 +296,11 @@ class TestGrammar(TestCase):
p.parse('ab') p.parse('ab')
def test_symbol_eq(self):
a = None
b = Symbol("abc")
self.assertNotEqual(a, b)
if __name__ == '__main__': if __name__ == '__main__':