Skip to content

Commit 5b185f2

Browse files
committed
Use explicit default arguments
Replicates graphql/graphql-js@468dfb5
1 parent dceb5cd commit 5b185f2

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

src/graphql/language/source.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Optional
2-
31
from .location import SourceLocation
42

53
__all__ = ["Source"]
@@ -13,8 +11,8 @@ class Source:
1311
def __init__(
1412
self,
1513
body: str,
16-
name: Optional[str] = None,
17-
location_offset: Optional[SourceLocation] = None,
14+
name: str = "GraphQL request",
15+
location_offset: SourceLocation = SourceLocation(1, 1),
1816
) -> None:
1917
"""Initialize source input.
2018
@@ -26,13 +24,13 @@ def __init__(
2624
2725
line and column in location_offset are 1-indexed
2826
"""
29-
27+
if not isinstance(body, str):
28+
raise TypeError("body must be a string.")
3029
self.body = body
31-
self.name = "GraphQL request" if name is None else name
32-
if not location_offset:
33-
location_offset = SourceLocation(1, 1)
34-
elif not isinstance(location_offset, SourceLocation):
35-
# noinspection PyProtectedMember,PyTypeChecker
30+
if not isinstance(name, str):
31+
raise TypeError("name must be a string.")
32+
self.name = name
33+
if not isinstance(location_offset, SourceLocation):
3634
location_offset = SourceLocation._make(location_offset)
3735
if location_offset.line <= 0:
3836
raise ValueError(

tests/language/test_source.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,46 @@
11
from pytest import raises # type: ignore
22

3-
from graphql.language import Source
3+
from graphql.language import Source, SourceLocation
4+
from graphql.pyutils import dedent
45

56

67
def describe_source():
8+
def accepts_body_and_name():
9+
source = Source("foo", "bar")
10+
assert source.body == "foo"
11+
assert source.name == "bar"
12+
13+
def accepts_location_offset():
14+
location_offset = SourceLocation(2, 3)
15+
source = Source("", "", location_offset)
16+
assert source.location_offset is location_offset
17+
18+
def accepts_tuple_as_location_offset():
19+
# noinspection PyTypeChecker
20+
source = Source("", "", (2, 3)) # type: ignore
21+
assert isinstance(source.location_offset, SourceLocation)
22+
assert source.location_offset == (2, 3)
23+
24+
def uses_default_arguments():
25+
source = Source("")
26+
assert source.name == "GraphQL request"
27+
assert isinstance(source.location_offset, SourceLocation)
28+
assert source.location_offset == (1, 1)
29+
30+
def can_get_location():
31+
body = dedent(
32+
"""
33+
line 1
34+
line 2
35+
line 3
36+
"""
37+
)
38+
source = Source(body)
39+
assert source.body == body
40+
location = source.get_location(body.find("2"))
41+
assert isinstance(location, SourceLocation)
42+
assert location == (2, 6)
43+
744
def can_be_stringified():
845
source = Source("")
946
assert str(source) == "<Source name='GraphQL request'>"
@@ -26,10 +63,33 @@ def can_be_compared():
2663
assert not source == "bar"
2764
assert source != "bar"
2865

66+
def rejects_invalid_body_and_name():
67+
with raises(TypeError, match="body must be a string\\."):
68+
# noinspection PyTypeChecker
69+
Source(None) # type: ignore
70+
with raises(TypeError, match="body must be a string\\."):
71+
# noinspection PyTypeChecker
72+
Source(1) # type: ignore
73+
with raises(TypeError, match="name must be a string\\."):
74+
# noinspection PyTypeChecker
75+
Source("", None) # type: ignore
76+
with raises(TypeError, match="name must be a string\\."):
77+
# noinspection PyTypeChecker
78+
Source("", 1) # type: ignore
79+
2980
def rejects_invalid_location_offset():
3081
def create_source(location_offset):
3182
return Source("", "", location_offset)
3283

84+
with raises(TypeError):
85+
create_source(None)
86+
with raises(TypeError):
87+
create_source(1)
88+
with raises(TypeError):
89+
create_source((1,))
90+
with raises(TypeError):
91+
create_source((1, 2, 3))
92+
3393
with raises(
3494
ValueError,
3595
match="line in location_offset is 1-indexed and must be positive\\.",

0 commit comments

Comments
 (0)