Debian Codegraph @

friendly_traceback/syntax_errors/indentation_error.py

raw symbols SCIP
1 # type: ignore
2 from ..ft_gettext import current_lang
3 from ..token_utils import remove_meaningless_tokens
4
5 _ = current_lang.translate
6
7
8 def set_cause_indentation_error(value, statement):
9 value = str(value)
10 if "unexpected indent" in value:
11 this_case = _(
12 "Line `{number}` identified above is more indented than expected.\n"
13 ).format(number=statement.linenumber)
14 elif "expected an indented block" in value:
15 this_case = _(
16 "Line `{number}` identified above "
17 "was expected to begin a new indented block.\n"
18 ).format(number=statement.linenumber)
19 else:
20 this_case = _(
21 "Line `{number}` identified above is less indented than expected.\n"
22 ).format(number=statement.linenumber)
23
24 if (
25 len(statement.all_statements) > 1
26 and len(statement.tokens) == 1
27 and statement.tokens[0].is_string()
28 ):
29 prev_statement = statement.all_statements[-2]
30 prev_tokens = remove_meaningless_tokens(prev_statement)
31 if len(prev_tokens) == 1 and prev_tokens[0].is_string():
32 additional = _(
33 "However, line {number}, which is identified as having a problem,\n"
34 "consists of a single string which is also the case\n"
35 "for the preceding line.\n"
36 "Perhaps you meant to include a continuation character, `\\`,\n"
37 "at the end of line {preceding}.\n"
38 ).format(number=statement.linenumber, preceding=statement.linenumber - 1)
39
40 return this_case + "\n" + additional
41
42 return this_case