Skip to content

Commit bf98188

Browse files
committed
add fix for quotation marks in docstrings (issue 97)
1 parent 48a4ac2 commit bf98188

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/tap/utils.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def source_line_to_tokens(obj: object) -> Dict[int, List[Dict[str, Union[str, in
205205
for token_type, token, (start_line, start_column), (end_line, end_column), line in tokenize_source(obj):
206206
line_to_tokens.setdefault(start_line, []).append({
207207
'token_type': token_type,
208-
'token': bytes(token, encoding='ascii').decode('unicode-escape'),
208+
'token': token,
209209
'start_line': start_line,
210210
'start_column': start_column,
211211
'end_line': end_line,
@@ -241,8 +241,21 @@ def get_class_variables(cls: type) -> Dict[str, Dict[str, str]]:
241241
and token["token"][:1] in {'"', "'"}
242242
):
243243
sep = " " if variable_to_comment[class_variable]["comment"] else ""
244+
245+
# Identify the quote character (single or double)
244246
quote_char = token["token"][:1]
245-
variable_to_comment[class_variable]["comment"] += sep + token["token"].strip(quote_char).strip()
247+
248+
# Identify the number of quote characters at the start of the string
249+
num_quote_chars = len(token["token"]) - len(token["token"].lstrip(quote_char))
250+
251+
# Remove the number of quote characters at the start of the string and the end of the string
252+
token["token"] = token["token"][num_quote_chars:-num_quote_chars]
253+
254+
# Remove the unicode escape sequences (e.g. "\"")
255+
token["token"] = bytes(token["token"], encoding='ascii').decode('unicode-escape')
256+
257+
# Add the token to the comment, stripping whitespace
258+
variable_to_comment[class_variable]["comment"] += sep + token["token"].strip()
246259

247260
# Match class variable
248261
class_variable = None

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class MultiquoteMultiline:
308308
hi: str
309309
"\"Hello there\"\""
310310

311-
class_variables = OrderedDict()
311+
class_variables = {}
312312
class_variables['bar'] = {'comment': "''biz baz'"}
313313
class_variables['hi'] = {'comment': '"Hello there""'}
314314
self.assertEqual(get_class_variables(MultiquoteMultiline), class_variables)

0 commit comments

Comments
 (0)