Return a string list that represents the error messages. Use a form suitable for displaying to the user. If self.pretty is True also append a relevant trimmed source code line (only for severity 'error').
(
self, error_tuples: list[ErrorTuple], source_lines: list[str] | None
)
| 1019 | ) |
| 1020 | |
| 1021 | def format_messages_default( |
| 1022 | self, error_tuples: list[ErrorTuple], source_lines: list[str] | None |
| 1023 | ) -> list[str]: |
| 1024 | """Return a string list that represents the error messages. |
| 1025 | |
| 1026 | Use a form suitable for displaying to the user. If self.pretty |
| 1027 | is True also append a relevant trimmed source code line (only for |
| 1028 | severity 'error'). |
| 1029 | """ |
| 1030 | a: list[str] = [] |
| 1031 | for file, line, column, end_line, end_column, severity, message, code in error_tuples: |
| 1032 | s = "" |
| 1033 | if file is not None: |
| 1034 | if self.options.show_column_numbers and line >= 0 and column >= 0: |
| 1035 | srcloc = f"{file}:{line}:{1 + column}" |
| 1036 | if self.options.show_error_end and end_line >= 0 and end_column >= 0: |
| 1037 | srcloc += f":{end_line}:{end_column}" |
| 1038 | elif line >= 0: |
| 1039 | srcloc = f"{file}:{line}" |
| 1040 | else: |
| 1041 | srcloc = file |
| 1042 | s = f"{srcloc}: {severity}: {message}" |
| 1043 | else: |
| 1044 | s = message |
| 1045 | if ( |
| 1046 | not self.hide_error_codes |
| 1047 | and code |
| 1048 | and (severity != "note" or code in SHOW_NOTE_CODES) |
| 1049 | ): |
| 1050 | # If note has an error code, it is related to a previous error. Avoid |
| 1051 | # displaying duplicate error codes. |
| 1052 | s = f"{s} [{code}]" |
| 1053 | a.append(s) |
| 1054 | if self.options.pretty: |
| 1055 | # Add source code fragment and a location marker. |
| 1056 | if severity == "error" and source_lines and line > 0: |
| 1057 | source_line = source_lines[line - 1] |
| 1058 | source_line_expanded = source_line.expandtabs() |
| 1059 | min_column = len(source_line) - len(source_line.lstrip()) |
| 1060 | if column < min_column: |
| 1061 | # Something went wrong, take first non-empty column. |
| 1062 | column = min_column |
| 1063 | |
| 1064 | # Shifts column after tab expansion |
| 1065 | column = len(source_line[:column].expandtabs()) |
| 1066 | end_column = len(source_line[:end_column].expandtabs()) |
| 1067 | |
| 1068 | # Note, currently coloring uses the offset to detect source snippets, |
| 1069 | # so these offsets should not be arbitrary. |
| 1070 | a.append(" " * DEFAULT_SOURCE_OFFSET + source_line_expanded) |
| 1071 | marker = "^" |
| 1072 | if end_line == line and end_column > column: |
| 1073 | marker = f'^{"~" * (end_column - column - 1)}' |
| 1074 | elif end_line != line: |
| 1075 | # just highlight the first line instead |
| 1076 | marker = f'^{"~" * (len(source_line_expanded) - column - 1)}' |
| 1077 | a.append(" " * (DEFAULT_SOURCE_OFFSET + column) + marker) |
| 1078 | return a |
no test coverage detected