FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-35540 dataclasses.asdict support defaultdict fields by kwsp · Pull Request #32056 · python/cpython · GitHub

/ cpython Public

bpo-35540 dataclasses.asdict support defaultdict fields - #32056

Merged
ericvsmith merged 6 commits into
python:mainfrom
kwsp:fix-issue-35540
Oct 7, 2022
Merged

bpo-35540 dataclasses.asdict support defaultdict fields#32056
ericvsmith merged 6 commits into
python:mainfrom
kwsp:fix-issue-35540

Conversation

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

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

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!

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.

Copy link
Copy Markdown
Contributor

Could you add a test please?

kwsp commented May 4, 2022

Copy link
Copy Markdown
Contributor Author

Could you add a test please?

I just added a test for this

MaxwellDupre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Lib/dataclasses.py Outdated
ericvsmith self-assigned this Oct 4, 2022
ericvsmith merged commit c46a423 into python:main Oct 7, 2022
carljm added a commit to carljm/cpython that referenced this pull request Oct 8, 2022
* 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)

akitathai94 commented Jun 29, 2024
edited
Loading

Copy link
Copy Markdown

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

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

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

7 participants


Back | FazBrowse Home | New Git URL