Skip to content

gh-160106: Fix csv.Dialect.__replace__() losing slot-backed attributes - #160641

Open
saiyam-jain-dev wants to merge 1 commit into
python:mainfrom
saiyam-jain-dev:gh-160106-csv-dialect-replace-slots
Open

saiyam-jain-dev wants to merge 1 commit into
python:mainfrom
saiyam-jain-dev:gh-160106-csv-dialect-replace-slots

Conversation

@saiyam-jain-dev

@saiyam-jain-dev saiyam-jain-dev commented Oct 25, 2026 •

Copy link
Copy Markdown

Summary

Fixes #160106

copy.replace() on a csv.Dialect subclass that stores dialect options in slots silently drops those slot-backed attributes. Dialect.replace() only copied self.dict, so any attribute stored in a slot (rather than dict) was missing from the new object, causing csv.reader/csv.writer to fall back to the default comma delimiter.

Reproducer (before fix)

`python
import copy, 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))) # [['alpha', 'beta']]
print(list(csv.reader(['alpha;beta'], replacement))) # [['alpha;beta']] ← BUG
`

Fix

In Dialect.replace(), after copying self.dict, iterate the MRO for slots entries and copy any dialect attribute not already set via dict before applying the caller's changes.

Testing

The existing csv test suite continues to pass. The reproducer from the issue now produces [['alpha', 'beta']] for both readers.

…ributes

copy.replace() on a csv.Dialect subclass that stores dialect options in
__slots__ would silently drop those attributes.  __replace__() only copied
self.__dict__, so any attribute stored in a slot was missing from the new
object, causing csv.reader/writer to fall back to the default value.

Fix by iterating the MRO for __slots__ entries and copying any dialect
attribute that was not already set via __dict__.
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bedevere-app

bedevere-app Bot commented Oct 25, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

copy.replace loses slot-backed CSV dialect options

2 participants