Skip to content

Bring back colored traceback for ConversionError #45

@NiklasRosenstein

Description

@NiklasRosenstein

Databind 1.x had colored tracebacks for location errors:

image

def format(
self,
indent: str = ' ',
list_item: str = '-',
filenames: bool = True,
sparse_filenames: bool = True,
collapse: bool = False,
colors: t.Optional[bool] = None,
) -> str:
"""
Converts the location to a string representation of the form
- $ (type) [filename:line:col]
- key (type) [filename:line:col]
...
"""
if colors is None and sys.stdout.isatty():
colors = True
c = t.cast(t.Callable, colored if colors else _no_colored)
parts: t.List[str] = []
prev_filename: t.Optional[str] = None
for loc in _get_location_chain(self):
source_changed = filenames and bool(not prev_filename or loc.filename and loc.filename != prev_filename)
if collapse and parts and not source_changed and not loc.key:
continue
name = '$' if loc.key is None else f'.{loc.key}'
parts.append(f'{indent}{list_item} {c(name, "cyan")} {c("(" + str(loc.type) + ")", "green")}')
if not sparse_filenames or source_changed:
parts.append(' ')
parts.append(c(_get_filename_and_pos_string(loc.filename, loc.pos), 'magenta'))
parts.append('\n')
if loc.filename:
prev_filename = loc.filename
parts.pop() # Remove the last newline
return ''.join(parts)

We should bring that feature back in Databind 4.x

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions