csv: apply dialect quoting and lineterminator in writer#8254
Conversation
QuoteStyle::Minimal was mapped to csv_core's Always, which quoted every field even under QUOTE_MINIMAL. Map it to Necessary so only fields that require quoting are quoted. Unmarks Test_Csv.test_write_quoting. Assisted-by: Claude Code:claude-opus-4-8
csv.writer ignored the quoting and lineterminator from a dialect (whether passed by name or object), always using the defaults. Resolve both from the dialect via get_quoting()/get_lineterminator(), mirroring get_delimiter(), so an explicit keyword argument still overrides the dialect. Unmarks TestDialectUnix.test_simple_writer. Assisted-by: Claude Code:claude-opus-4-8
📝 WalkthroughWalkthroughCSV writer configuration now respects dialect-provided quoting and line terminators, while explicit ChangesCSV formatting behavior
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [x] lib: cpython/Lib/csv.py dependencies:
dependent tests: (4 tests)
Legend:
|
|
Hello! With this PR, RustPython writes \r,\n!, while CPython writes "\r","\n"!. csv.writer(sio, lineterminator="!").writerow(["\r", "\n"])It looks like embedded CR/LF may no longer be quoted when the line terminator is not CR or LF. |
Summary
csv.writerignored thequotingandlineterminatordefined by a dialect (passed by name likedialect='unix'or as a dialect object), so writing with a built-in dialect diverged from CPython. Only explicit keyword arguments took effect.to_writernow resolves both from the dialect (viaget_quoting()/get_lineterminator(), mirroring the existingget_delimiter()), while an explicit keyword argument still overrides it.While fixing this I also found the
QuoteStyle::Minimal→ csv_core mapping wasAlways(quoting every field underQUOTE_MINIMAL); it is nowNecessary. This was dormant because the quote style was only applied for an explicitquoting=argument, so it had to be fixed before applying the dialect's quoting.Test plan
TestDialectUnix.test_simple_writerandTest_Csv.test_write_quoting(both@unittest.expectedFailurebefore).cargo run --release -- -m test test_csv: 128 run, 7 skipped, SUCCESS.extra_tests/snippets/stdlib_csv.py: passes.cargo fmt --check/cargo clippy: clean (no new warnings).dialect='unix'andQUOTE_MINIMALcases match.Assisted-by: Claude Code:claude-opus-4-8