Skip to content

Commit a9ae0d9

Browse files
committed
Change check for source argument
Replicates graphql/graphql-js@ef0f1ab Also removes check for name argument to be consistent here.
1 parent fa38fe1 commit a9ae0d9

File tree

7 files changed

+21
-78
lines changed

7 files changed

+21
-78
lines changed

src/graphql/language/parser.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@
5757
from .directive_locations import DirectiveLocation
5858
from .ast import Token
5959
from .lexer import Lexer, is_punctuator_token_kind
60-
from .source import Source
60+
from .source import Source, is_source
6161
from .token_kind import TokenKind
6262
from ..error import GraphQLError, GraphQLSyntaxError
63-
from ..pyutils import inspect
6463

6564
__all__ = ["parse", "parse_type", "parse_value"]
6665

@@ -178,10 +177,10 @@ def __init__(
178177
no_location: bool = False,
179178
experimental_fragment_variables: bool = False,
180179
):
181-
if isinstance(source, str):
182-
source = Source(source)
183-
elif not isinstance(source, Source):
184-
raise TypeError(f"Must provide Source. Received: {inspect(source)}.")
180+
source = (
181+
cast(Source, source) if is_source(source) else Source(cast(str, source))
182+
)
183+
185184
self._lexer = Lexer(source)
186185
self._no_location = no_location
187186
self._experimental_fragment_variables = experimental_fragment_variables

src/graphql/language/source.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .location import SourceLocation
44

5-
__all__ = ["Source"]
5+
__all__ = ["Source", "is_source"]
66

77

88
class Source:
@@ -26,11 +26,7 @@ def __init__(
2626
2727
The ``line`` and ``column`` attributes in ``location_offset`` are 1-indexed.
2828
"""
29-
if not isinstance(body, str):
30-
raise TypeError("body must be a string.")
3129
self.body = body
32-
if not isinstance(name, str):
33-
raise TypeError("name must be a string.")
3430
self.name = name
3531
if not isinstance(location_offset, SourceLocation):
3632
location_offset = SourceLocation._make(location_offset)
@@ -64,3 +60,11 @@ def __eq__(self, other: Any) -> bool:
6460

6561
def __ne__(self, other: Any) -> bool:
6662
return not self == other
63+
64+
65+
def is_source(source: Any) -> bool:
66+
"""Test if the given value is a Source object.
67+
68+
For internal use only.
69+
"""
70+
return isinstance(source, Source)

src/graphql/utilities/strip_ignored_characters.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from typing import Union
1+
from typing import Union, cast
22

3-
from ..language import Lexer, Source, TokenKind
3+
from ..language import Lexer, TokenKind
4+
from ..language.source import Source, is_source
45
from ..language.block_string import (
56
dedent_block_string_value,
67
get_block_string_indentation,
78
)
89
from ..language.lexer import is_punctuator_token_kind
9-
from ..pyutils import inspect
1010

1111

1212
def strip_ignored_characters(source: Union[str, Source]) -> str:
@@ -65,14 +65,10 @@ def strip_ignored_characters(source: Union[str, Source]) -> str:
6565
6666
"""Type description""" type Foo{"""Field description""" bar:String}
6767
'''
68-
source_obj = Source(source) if isinstance(source, str) else source
69-
if not isinstance(source_obj, Source):
70-
raise TypeError(
71-
f"Must provide string or Source. Received: {inspect(source_obj)}."
72-
)
73-
74-
body = source_obj.body
75-
lexer = Lexer(source_obj)
68+
source = cast(Source, source) if is_source(source) else Source(cast(str, source))
69+
70+
body = source.body
71+
lexer = Lexer(source)
7672
stripped_body = ""
7773
was_last_added_token_non_punctuator = False
7874
while lexer.advance().kind != TokenKind.EOF:

tests/execution/test_sync.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,6 @@ def does_not_return_an_awaitable_for_validation_errors():
138138
result = graphql_sync(schema, doc)
139139
assert result == (None, validation_errors)
140140

141-
def raises_a_type_error_when_no_query_is_passed():
142-
with raises(TypeError) as exc_info:
143-
# noinspection PyTypeChecker
144-
assert graphql_sync(schema, None) # type: ignore
145-
msg = str(exc_info.value)
146-
assert msg == "Must provide Source. Received: None."
147-
148141
def does_not_return_an_awaitable_for_sync_execution():
149142
doc = "query Example { syncField }"
150143
assert graphql_sync(schema, doc, "rootValue") == (

tests/language/test_parser.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,6 @@ def assert_syntax_error(text: str, message: str, location: Location) -> None:
4545

4646

4747
def describe_parser():
48-
def asserts_that_a_source_to_parse_was_provided():
49-
with raises(TypeError) as exc_info:
50-
# noinspection PyArgumentList
51-
assert parse() # type: ignore
52-
msg = str(exc_info.value)
53-
assert "missing" in msg
54-
assert "source" in msg
55-
with raises(TypeError) as exc_info:
56-
# noinspection PyTypeChecker
57-
assert parse(None) # type: ignore
58-
msg = str(exc_info.value)
59-
assert "Must provide Source. Received: None." in msg
60-
with raises(TypeError) as exc_info:
61-
# noinspection PyTypeChecker
62-
assert parse({}) # type: ignore
63-
msg = str(exc_info.value)
64-
assert "Must provide Source. Received: {}." in msg
65-
6648
def parse_provides_useful_errors():
6749
with raises(GraphQLSyntaxError) as exc_info:
6850
parse("{")

tests/language/test_source.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,6 @@ def can_create_custom_attribute():
7878
node.custom = "bar" # type: ignore
7979
assert node.custom == "bar" # type: ignore
8080

81-
def rejects_invalid_body_and_name():
82-
with raises(TypeError, match="body must be a string\\."):
83-
# noinspection PyTypeChecker
84-
Source(None) # type: ignore
85-
with raises(TypeError, match="body must be a string\\."):
86-
# noinspection PyTypeChecker
87-
Source(1) # type: ignore
88-
with raises(TypeError, match="name must be a string\\."):
89-
# noinspection PyTypeChecker
90-
Source("", None) # type: ignore
91-
with raises(TypeError, match="name must be a string\\."):
92-
# noinspection PyTypeChecker
93-
Source("", 1) # type: ignore
94-
9581
def rejects_invalid_location_offset():
9682
def create_source(location_offset: Tuple[int, int]) -> Source:
9783
return Source("", "", cast(SourceLocation, location_offset))

tests/utilities/test_strip_ignored_characters.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,6 @@ def to_stay_the_same(self):
7575

7676

7777
def describe_strip_ignored_characters():
78-
def asserts_that_a_source_was_provided():
79-
with raises(
80-
TypeError, match="missing 1 required positional argument: 'source'"
81-
):
82-
# noinspection PyArgumentList
83-
strip_ignored_characters() # type: ignore
84-
with raises(
85-
TypeError, match="Must provide string or Source. Received: None\\."
86-
):
87-
# noinspection PyTypeChecker
88-
strip_ignored_characters(None) # type: ignore
89-
90-
def asserts_that_a_valid_source_was_provided():
91-
with raises(TypeError, match="Must provide string or Source. Received: {}\\."):
92-
# noinspection PyTypeChecker
93-
strip_ignored_characters({}) # type: ignore
94-
9578
def strips_ignored_characters_from_graphql_query_document():
9679
query = dedent(
9780
"""

0 commit comments

Comments
 (0)