GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-35540 dataclasses.asdict support defaultdict fields by kwsp · Pull Request #32056 · python/cpython

Conversation

@kwsp

@kwsp kwsp commented Mar 22, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

https://bugs.python.org/issue35540

dataclasses.asdict breaks when there are defaultdicts in the dataclass attributes because it assumes defaultdict and dict are the same thing, but in reality defaultdict takes a default_factory as the first argument in its constructor.

Since dataclasses.asdict already supports NamedTuples, I think its fair that defaultdict, which is also in the standard library, does not break dataclasses.

Test:

from dataclasses import dataclass, asdict
from typing import DefaultDict, List
from collections import defaultdict


@dataclass
class C:
    mp: DefaultDict[str, List]


dd = defaultdict(list)
dd["x"].append(12)
c = C(mp=dd)
d = asdict(c)  # throws "TypeError: first argument must be callable or None"
assert d == {"mp": {"x": [12]}}

https://bugs.python.org/issue35540

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tscheepers reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@kwsp
kwsp requested a review from ericvsmith as a code owner March 22, 2022 18:23
@bedevere-bot bedevere-bot added the awaiting review label Mar 22, 2022
@the-knights-who-say-ni

the-knights-who-say-ni commented Mar 22, 2022

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@kwsp

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@the-knights-who-say-ni the-knights-who-say-ni added the CLA not signed label Mar 22, 2022
@kwsp
bpo-35540: Fix dataclasses.asdict support for defaultdict fields
7567680
dataclasses.asdict breaks when there are `defaultdict`s in the dataclass
attributes because it assumes `defaultdict` and `dict` are the same thing,
but in reality `defaultdict` takes a default_factory as the first argument
in its constructor. This change adds support for defaultdict fields.
@kwsp
kwsp force-pushed the fix-issue-35540 branch from 82a5a4e to 7567680 Compare March 22, 2022 19:15
@the-knights-who-say-ni the-knights-who-say-ni added CLA signed and removed CLA not signed labels Mar 23, 2022
@kwsp
fix blurb
82b41dc
@MaxwellDupre

MaxwellDupre commented Apr 25, 2022

Copy link
Copy Markdown
Contributor

Could you add a test please?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kwsp added 2 commits May 4, 2022 11:37
@kwsp
added tests for asdict() with defaultdict
3fe8873
@kwsp
Merge branch 'fix-issue-35540' of github.com:kwsp/cpython into fix-is…
1efb31d
…sue-35540
@kwsp

kwsp commented May 4, 2022

Copy link
Copy Markdown
Contributor Author

Could you add a test please?

I just added a test for this

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@wrmsr wrmsr mannequin mentioned this pull request Apr 10, 2022
dataclasses.asdict breaks with defaultdict fields #79721
Closed
MaxwellDupre
MaxwellDupre approved these changes Jul 4, 2022
View reviewed changes

@MaxwellDupre MaxwellDupre left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests ok on Fedora.
Ref to bugs.python.org/issue35540 is not future proof, assume will be turned off at some time.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting core review and removed awaiting review labels Jul 4, 2022
@ezio-melotti ezio-melotti removed the CLA signed label Jul 13, 2022
@ericvsmith
Merge branch 'main' into fix-issue-35540
95c957f
ericvsmith
ericvsmith reviewed Oct 4, 2022
View reviewed changes
Comment thread Lib/dataclasses.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ericvsmith ericvsmith self-assigned this Oct 4, 2022
@kwsp
check 'default_factory' attr on the type
4fcb8d0
@ericvsmith
ericvsmith merged commit c46a423 into python:main Oct 7, 2022
@bedevere-bot bedevere-bot removed the awaiting core review label Oct 7, 2022
carljm added a commit to carljm/cpython that referenced this pull request Oct 8, 2022
@carljm
Merge branch 'main' into typewatch
29e38df
* main:
  bpo-35540 dataclasses.asdict now supports defaultdict fields (pythongh-32056)
  pythonGH-91052: Add C API for watching dictionaries (pythonGH-31787)
  bpo-38693: Use f-strings instead of str.format() within importlib (python#17058)
This was referenced Oct 10, 2022
bpo-35540: Add collections.defaultdict support to dataclasses.{asdict,astuple} #11361
Closed
gh-98410: move getbufferproc and releasebufferproc to buffer.h #31158
Merged
This was referenced Oct 11, 2022
dataclasses.astuple breaks on DefaultDict #98169
Closed
gh-98169 dataclasses.astuple support DefaultDict #98170
Merged
mpage pushed a commit to mpage/cpython that referenced this pull request Oct 11, 2022
@kwsp @mpage
bpo-35540 dataclasses.asdict now supports defaultdict fields (pythong…
395bc9a
…h-32056)
@skshetry skshetry mentioned this pull request Jun 16, 2023
dvc exp show --json produces error that disables VS Code Extension from tracking experiments treeverse/dvc#9565
Closed
@kwsp kwsp mentioned this pull request Jun 26, 2023
asdict() on dataclass with defaultdict raises TypeError: "first argument must be callable or None " #106113
Closed
@Liam-DeVoe Liam-DeVoe mentioned this pull request Dec 14, 2023
TypeError when drawing from a strategy that produces dataclasses with a defaultdict attribute HypothesisWorks/hypothesis#3812
Closed
@akitathai94

akitathai94 commented Jun 29, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Does this merge fix the issue yet cause it still throws error in Python 3.11

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kwsp

kwsp commented Jun 29, 2024

Copy link
Copy Markdown
Contributor Author

Does this merge fix the issue yet cause it still throws error in Python 3.11

This was merged in 3.12 and not backported to 3.11

https://docs.python.org/3.12/whatsnew/changelog.html

akitathai94 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ravin-m ravin-m mentioned this pull request Feb 4, 2025
converting defaultdict to dict in RunResponse.to_dict() agno-agi/agno#2005
Closed
2 of 12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ericvsmith ericvsmith ericvsmith left review comments
+1 more reviewer
@MaxwellDupre MaxwellDupre MaxwellDupre approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@ericvsmith ericvsmith

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@kwsp @the-knights-who-say-ni @MaxwellDupre @akitathai94 @ericvsmith @ezio-melotti @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.