Skip to content

copy.replace loses slot-backed CSV dialect options #160106

Description

@Boyeep

Bug report

copy.replace() loses slot-backed attributes of csv.Dialect subclasses. This can silently change how CSV data is parsed, even when the delimiter was not replaced.

Reproduced with CPython 3.16.0a1. The same implementation is present on main at 31a7d99.

import copy
import csv

class SemicolonDialect(csv.excel):
    __slots__ = ('delimiter',)

    def __init__(self):
        self.delimiter = ';'
        super().__init__()

dialect = SemicolonDialect()
replacement = copy.replace(dialect, quoting=csv.QUOTE_ALL)

print(list(csv.reader(['alpha;beta'], dialect)))
print(list(csv.reader(['alpha;beta'], replacement)))

Actual output:

[['alpha', 'beta']]
[['alpha;beta']]

Expected: both readers produce [['alpha', 'beta']], since only the quoting setting was replaced.

Dialect.__replace__() copies only __dict__, losing attributes stored in slots. The replacement therefore lacks its delimiter, and csv.reader falls back to the default comma delimiter.

I have prepared a local fix and regression tests.

CPython versions tested on:

3.16

Operating systems tested on:

No response

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 21, 2026
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 21, 2026
  3. added a commit that references this issue on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions