Add 'make eval'. Fix crash in pprint(). Reformat some files.#237
Add 'make eval'. Fix crash in pprint(). Reformat some files.#237gvanrossum wants to merge 8 commits intomicrosoft:mainfrom
Conversation
KRRT7
left a comment
There was a problem hiding this comment.
Thanks for catching the format_code crash — that's on us from #235. Two notes:
-
format_codefallback wraps text in quotes —pprint.pformat(text)on a string returns itsrepr(), so a non-literal like<CustomClass object at 0x...>becomes'<CustomClass object at 0x...>'with surrounding quotes in the output. Sincetextis already a repr string meant for display, returning it directly would be cleaner (inline comment below). -
pretty_printsilently dropsprefix/suffix— This is a separate regression from #235 that isn't addressed here. Our PR changed the body toprint(pprint.pformat(obj, width=line_width)), dropping theprefix/suffixargs. Callers likeutils.pretty_print(actual1, Fore.GREEN, Fore.RESET)lost their color output. Should beprint(prefix + pprint.pformat(obj, width=line_width) + suffix). Happy to send a follow-up PR for this if you'd like.
| return pprint.pformat(text, width=line_width) | ||
|
|
||
|
|
||
| def reindent(text: str) -> str: |
There was a problem hiding this comment.
pprint.pformat(text) on a plain string returns its repr() — e.g. '<CustomClass object at 0x...>' with surrounding quotes. Since text is already a repr string intended for terminal/diff output, returning it directly avoids the extra quoting:
| def reindent(text: str) -> str: | |
| except (ValueError, SyntaxError): | |
| # Fall back to the raw text if it's not a valid literal | |
| return text |
|
Ah, thanks. If you add the fixes to this PR using the GitHub suggestion feature I will apply them. |
|
The In def pretty_print(obj: object, prefix: str = "", suffix: str = "") -> None:
"""Pretty-print an object using pprint."""
import pprint
line_width = min(200, shutil.get_terminal_size().columns)
print(pprint.pformat(obj, width=line_width))Should be: def pretty_print(obj: object, prefix: str = "", suffix: str = "") -> None:
"""Pretty-print an object using pprint."""
import pprint
line_width = min(200, shutil.get_terminal_size().columns)
print(prefix + pprint.pformat(obj, width=line_width) + suffix)Otherwise happy to send a separate PR for it. |
| return pprint.pformat(ast.literal_eval(text), width=line_width) | ||
| except (ValueError, SyntaxError): | ||
| # Fall back to simple pprint of the string itself if it's not a valid literal | ||
| return pprint.pformat(text, width=line_width) |
There was a problem hiding this comment.
| return pprint.pformat(text, width=line_width) | |
| return text |
pprint.pformat on a plain string wraps it in quotes — returning text directly avoids that.
|
Opened gvanrossum#1 against your |
Apparently at least one of isort and black was updated and now produces more compact code in some cases.
Also change the black call in Makefile to use only -tpy312 since black on 3.12 cannot parse back the py314 code it generated (maybe only the last -t flag was effective?).
And finally make Makefile more robust by using
uv run Xinstead of.venv/bin/X.