Skip to content

Commit b8190cc

Browse files
committed
Undefined should not be an exception (#187)
1 parent 1e5be9c commit b8190cc

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ a query language for APIs created by Facebook.
1010
![Lint Status](https://github.com/graphql-python/graphql-core/actions/workflows/lint.yml/badge.svg)
1111
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
1212

13-
An extensive test suite with over 2300 unit tests and 100% coverage comprises a
13+
An extensive test suite with over 2400 unit tests and 100% coverage comprises a
1414
replication of the complete test suite of GraphQL.js, making sure this port is
1515
reliable and compatible with GraphQL.js.
1616

src/graphql/pyutils/undefined.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__all__ = ["Undefined", "UndefinedType"]
88

99

10-
class UndefinedType(ValueError):
10+
class UndefinedType:
1111
"""Auxiliary class for creating the Undefined singleton."""
1212

1313
_instance: Optional[UndefinedType] = None
@@ -34,7 +34,7 @@ def __bool__(self) -> bool:
3434
return False
3535

3636
def __eq__(self, other: Any) -> bool:
37-
return other is Undefined
37+
return other is Undefined or other is None
3838

3939
def __ne__(self, other: Any) -> bool:
4040
return not self == other

tests/pyutils/test_undefined.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@ def is_hashable():
2121
def as_bool_is_false():
2222
assert bool(Undefined) is False
2323

24-
def only_equal_to_itself():
24+
def only_equal_to_itself_and_none():
25+
# because we want it to behave similarly to JavaScript
2526
assert Undefined == Undefined
2627
assert not Undefined != Undefined
2728
none_object = None
28-
assert Undefined != none_object
29-
assert not Undefined == none_object
29+
assert Undefined == none_object
30+
assert not Undefined != none_object
3031
false_object = False
3132
assert Undefined != false_object
3233
assert not Undefined == false_object
3334

35+
def should_not_be_an_exception():
36+
# because we want to create similar code to JavaScript where
37+
# undefined return values are different from exceptions
38+
# (for instance, this is used in the completeValue function)
39+
assert not isinstance(Undefined, Exception)
40+
3441
def cannot_be_redefined():
3542
with warns(RuntimeWarning, match="Redefinition of 'Undefined'"):
3643
redefined_undefined = UndefinedType()

0 commit comments

Comments
 (0)