Implement the `csv` module #1018
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1018"
general
weight: 0.35
samuelcolvin
Implements the
csvmodule:reader,writer,DictReader,DictWriter,Error, theQUOTE_constants, the three built-in dialects,get_dialect,list_dialectsand a read-onlyfield_size_limit. Stacked on #1001, which it needs foropen()files to be iterable.The parser and writer are ports of CPython's
_csv.cstate machine andjoin_append_data, so quoting, escaping,skipinitialspace,strict, theQUOTE_conversions and the error messages match CPython 3.14.DictReaderandDictWriterfollowcsv.py.How a file gets read. A reader cannot load an unread
open()file from__next__, socsv.reader(f)andcsv.DictReader(f)yield the host read with a newLoadThenCalleffect and the construction is replayed once the buffer is in place. Every consumer then works:list(csv.reader(f)),next(reader),for row in csv.DictReader(f). The effect is general (it stores a callable plus itsArgValues), so it is also the mechanism the TODO onunloaded_iteration_errordescribes forlist(f)and friends; this PR wires it only forcsv.Writers.
writerowreturns whatever the target'swrite()returns, yielding the host write for a file.writerowson a file sends all rows in one host write (WritePositiongained aresult_noneflag so it still answersNone); on any other targetwrite()runs synchronously per row.Not implemented, documented in
limitations/csv.md:register_dialect/unregister_dialect(no module state),Sniffer, changingfield_size_limit. The built-in dialects are_csv.Dialectobjects rather than classes.Each commit is one layer: the exception type, the VM effect, the module, tests, type stubs, docs. Note the
heap/mod.rschange is two one-line match arms for the newHeapData::Csvpayload (GC trace and dec-ref), the same shape asItertools.Also touched:
_csv.Errorcrosses the Python and JS bindings ascsv.Error; theTypefingerprint indump_formatmoved because five variants were appended (additive, noDUMP_VERSIONbump).The four CPython-side case failures in
str__format.py,str__percent_format.py,fstring__all.pyandbytes__percent_format.py('{0:.2147482623g}'precision edge cases) also fail when the unchanged test files are run directly under this machine's CPython 3.14.7, so they are unrelated to this branch.🤖 Generated with Claude Code
https://claude.ai/code/session_01VMK9rG1LtuF1pugGYcHdHC
<!-- This is an auto-generated description by cubic. -->
---
Summary by cubic
Implements the
csvmodule:reader,writer,DictReader,DictWriter, theQUOTE_constants, the three built-in dialects,get_dialect,list_dialects,Errorand a read-onlyfield_size_limit. The parser and writer port CPython's_csv.cstate machine, so quoting, escaping,QUOTE_conversions, underscore digit separators and error messages match CPython 3.14;DictReader/DictWriterfollowcsv.py. Addscsv.Error(_csv.Error), a directExceptionsubclass, as a new exception type across the Python and JS bindings.Reading files and matching CPython edges
reader(f)andDictReader(f)on an unreadopen()file yield the host read through a newLoadThenCalleffect and replay construction once the buffer is in place, solist(...),next(...)andforloops all work; replays ending in an uncaught error get the per-turn resource-limit re-check.writerowson a file sends all rows in one host write; other targets stream rows, polling the clock through a retained iterator view.get_dialect(name=...)andfield_size_limit(new_limit=...)go throughFromArgs, so keyword forms and arity errors match CPython.float()accepts underscore digit separators under CPython's rule, shared with theQUOTE_NONNUMERICreader.register_dialect,unregister_dialectandSnifferare absent,field_size_limitis read-only, and the built-in dialects are_csv.Dialectobjects rather than classes; the gaps are documented indocs/limitations/csv.md, and hand-written type stubs reject the absent surface up front.Performance work
DictReadertakes the field vector straight from the reader and sizes rows by the previous row's key count;DictWriterreadsfieldnames,restvalandextrasactionper row and shares the dialect through anArc.crates/monty-bench.<sup>Written for commit a43b620944cb6a980c31df286b4c88b9756eb842. Summary will update on new commits.</sup>
<a href="https://cubic.dev/pr/pydantic/monty/pull/1018?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <a href="https://www.cubic.dev/action/auto-fix/pr/pydantic/monty/1018?returnTo=https%3A%2F%2Fgithub.com%2Fpydantic%2Fmonty%2Fpull%2F1018&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->