perf: Replace black with stdlib pprint for runtime formatting#235
perf: Replace black with stdlib pprint for runtime formatting#235gvanrossum merged 1 commit intomicrosoft:mainfrom
Conversation
black is only used at runtime in two cold formatting paths: - create_context_prompt() in answers.py (LLM debug context) - format_code()/pretty_print() in utils.py (developer terminal output) Both format Python data structures, which is exactly what pprint does. Replace black.format_str with pprint.pformat + ast.literal_eval, eliminating the runtime dependency entirely. Move black from dependencies to dev dependency-group — it remains available for make format/check but is no longer required by library consumers.
|
@KRRT7 I like what this does, so I'm approving it. (EDIT: And merging.) Nevertheless I'd like to know how much you thought about this and how much was done by a coding agent (and which one). Could you be upfront about that please? Or are you an agent? |
|
I'm human :) We actually met briefly at PyCon last year — I mentioned I was building an optimization agent (codeflash) and you gave some helpful feedback. At PyBay when you demo'd typeagent it caught my attention because I thought I had an application for it, but got too busy to keep implementing it — anyways. I've been building the agent for the past several months and I'm dogfooding it on open source projects to stress-test it on real codebases. For this PR specifically: I pointed the agent to your comment (GH MCP) about conditionally importing black, and it did the initial profiling and initially implemented a try/except import wrapper — catching From the session transcript, I told it: "we want to come up with a way where 1) we don't do try except 2) move black to optional dependencies and 3) get the correct result" and "can we somehow get equivalent results with just stdlib or hook into another library somehow or similar". The agent found So the split is roughly: agent did profiling + implementation, I reviewed everything and directed the design decisions. |
|
Cool. I wish you had mentioned that in the initial PR comment. Worse, this introduced a bug that broker eval runs. I have a fix in #237. Not sure if that's due to your optimization agent. |
|
Yes, it's on us, thanks for letting me know, I'll try to be more transparent about things in the future. |
Follows up on #229 (defer black import). Guido suggested making
blackfully optional at runtime.blackwas only used at runtime in two cold formatting paths:knowpro/answers.py—create_context_prompt()formats a dict for LLM debug contextaitools/utils.py—format_code()/pretty_print()formats Python literals for terminal outputBoth format Python data structures, which is exactly what
pprint.pformatdoes. Replacedblack.format_strwithpprint.pformat+ast.literal_eval— stdlib only, no conditional imports, no try/except.Moved
blackfromdependenciestodevdependency-group. It remains available formake format/make checkbut is no longer required by library consumers.Changes
answers.py:pprint.pformat(dictify(context), width=200)replacesblack.format_str(str(dictify(context)), ...)utils.py:pretty_printusespprint.pformatdirectly;format_codeusesast.literal_eval+pprint.pformatpyproject.toml:blackmoved fromdependenciestodevdependency-grouptest_utils.py: updated existing test, added 3 new tests forpretty_printandformat_codeBenchmark
Azure Standard_D2s_v5 — 2 vCPU, 8 GiB RAM, Python 3.13
Import Time (hyperfine, warmup 5, min-runs 30)
import typeagentImport time is unchanged because #229 already deferred the
import blackto first use. This PR removes the deferred import entirely and dropsblack+ transitive deps (pathspec,platformdirs,tomli) from the runtime dependency tree.Generated by codeflash optimization agent