Skip to content

Commit de0b14f

Browse files
committed
Add SourceLocation.__eq__ to make it comparable to the formatted form
1 parent 837ee66 commit de0b14f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/graphql/language/location.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ class SourceLocation(NamedTuple):
1616
def formatted(self):
1717
return dict(line=self.line, column=self.column)
1818

19+
def __eq__(self, other):
20+
if isinstance(other, dict):
21+
return other == self.formatted
22+
return super().__eq__(other)
23+
24+
def __ne__(self, other):
25+
return not self == other
26+
1927

2028
def get_location(source: "Source", position: int) -> SourceLocation:
2129
"""Get the line and column for a character position in the source.

tests/language/test_location.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from graphql import SourceLocation
2+
3+
4+
def describe_SourceLocation():
5+
def equals_with_itself():
6+
assert SourceLocation(1, 2) == SourceLocation(1, 2)
7+
assert (SourceLocation(1, 2) != SourceLocation(1, 2)) is False
8+
9+
def equals_with_formatted_form():
10+
sl = SourceLocation(1, 2)
11+
assert SourceLocation(1, 2) == sl.formatted
12+
assert (SourceLocation(1, 2) != sl.formatted) is False

0 commit comments

Comments
 (0)