Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02f349b

Browse files
authored
Make print_block_string work with string proxy objects (#153)
1 parent 7c0d73a commit 02f349b

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/graphql/language/block_string.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def print_block_string(value: str, prefer_multiple_lines: bool = False) -> str:
8383
8484
For internal use only.
8585
"""
86+
if not isinstance(value, str):
87+
value = str(value) # resolve lazy string proxy object
88+
8689
is_single_line = "\n" not in value
8790
has_leading_space = value.startswith(" ") or value.startswith("\t")
8891
has_trailing_quote = value.endswith('"')
@@ -95,12 +98,12 @@ def print_block_string(value: str, prefer_multiple_lines: bool = False) -> str:
9598
)
9699

97100
# Format a multi-line block quote to account for leading space.
98-
result = (
101+
before = (
99102
"\n"
100103
if print_as_multiple_lines and not (is_single_line and has_leading_space)
101104
else ""
102-
) + value
103-
if print_as_multiple_lines:
104-
result += "\n"
105+
)
106+
after = "\n" if print_as_multiple_lines else ""
107+
value = value.replace('"""', '\\"""')
105108

106-
return '"""' + result.replace('"""', '\\"""') + '"""'
109+
return f'"""{before}{value}{after}"""'

tests/language/test_block_string.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import cast
2+
13
from graphql.language.block_string import (
24
dedent_block_string_value,
35
print_block_string,
@@ -144,3 +146,10 @@ def correctly_prints_string_with_a_first_line_indentation():
144146
assert print_block_string(s) == join_lines(
145147
'"""', " first ", " line ", "indentation", " string", '"""'
146148
)
149+
150+
def correctly_prints_lazy_stings():
151+
class LazyString:
152+
def __str__(self):
153+
return "lazy"
154+
155+
assert print_block_string(cast(str, LazyString())) == '"""lazy"""'

0 commit comments

Comments
 (0)