Skip to content

fix: avoid mutating global csv dialect in run_csv_view fallback#52

Merged
AkshajSinghal merged 1 commit into
TruFoundation:mainfrom
zza-830:fix/csv-dialect-mutation
Jun 8, 2026
Merged

fix: avoid mutating global csv dialect in run_csv_view fallback#52
AkshajSinghal merged 1 commit into
TruFoundation:mainfrom
zza-830:fix/csv-dialect-mutation

Conversation

@zza-830

@zza-830 zza-830 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Problem

When csv.Sniffer().sniff() fails in run_csv_view(), the fallback code calls csv.get_dialect('excel') which returns the class itself (not an instance), then mutates .delimiter on it. This permanently corrupts the global excel dialect for the process lifetime, affecting all subsequent CSV operations.

Closes #47

Fix

Use the built-in csv.excel_tab and csv.excel dialects directly based on the detected delimiter, avoiding any mutation of global state.

# Before (mutates global dialect)
dialect = csv.get_dialect("excel")
dialect.delimiter = delimiter

# After (uses built-in dialects, no mutation)
dialect = csv.excel_tab if delimiter == "\t" else csv.excel

Testing

tests/test_data.py::test_run_csv_view_file_not_found PASSED
tests/test_data.py::test_run_csv_view_empty_file PASSED
tests/test_data.py::test_run_csv_view_shows_limited_rows PASSED

When csv.Sniffer().sniff() fails, the fallback code called
csv.get_dialect('excel') which returns the class itself, then mutated
.delimiter on it. This permanently corrupted the global excel dialect
for the process lifetime, affecting all subsequent CSV operations.

Fix: use the built-in csv.excel_tab and csv.excel dialects directly
based on the detected delimiter, avoiding any mutation.

Closes TruFoundation#47
@AkshajSinghal AkshajSinghal merged commit 8fdfbed into TruFoundation:main Jun 8, 2026
0 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

run_csv_view dialect mutation on immutable object

2 participants