Skip to content

Vendor dataclasses.asdict#3813

Merged
Zac-HD merged 16 commits intoHypothesisWorks:masterfrom
Liam-DeVoe:json-defaultdict
Dec 16, 2023
Merged

Vendor dataclasses.asdict#3813
Zac-HD merged 16 commits intoHypothesisWorks:masterfrom
Liam-DeVoe:json-defaultdict

Conversation

@Liam-DeVoe
Copy link
Copy Markdown
Member

@Liam-DeVoe Liam-DeVoe commented Dec 15, 2023

closes #3812.

A pair of related questions: how heavily should this vendored implementation be tested, and how much (if any) of it can be # pragma: no cover'd? There are currently a number of uncovered lines in _asdict_inner.

Comment on lines +196 to +199
# _ATOMIC_TYPES was introduced as an optimization in 3.12's dataclasses.
_ATOMIC_TYPES = frozenset(
{
types.NoneType,
Copy link
Copy Markdown
Member Author

@Liam-DeVoe Liam-DeVoe Dec 15, 2023

Choose a reason for hiding this comment

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

I thought I was being clever by backporting this optimization, but e.g. types.NoneType is new in python3.10, and I don't particularly want to special case that for an optimization that wasn't even present in older versions to begin with. It's probably more effort than it's worth to try and muck about with this. If people want the micro performance improvement, I think it's reasonable to say they should upgrade their python version.

@Liam-DeVoe
Copy link
Copy Markdown
Member Author

It turned out to not be too difficult to add a coverage test for this, so I've gone ahead and done so, except for the custom dict_factory case (which we don't currently use).

@Liam-DeVoe
Copy link
Copy Markdown
Member Author

Liam-DeVoe commented Dec 15, 2023

Apologies for the commit turnover here. I'm done making changes now.

Note that with the new guard on to_jsonable, we needed some additional cover tests.

Copy link
Copy Markdown
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Nice! Two small implementation comments, but looks great overall - including the tests 🤩

Comment thread hypothesis-python/src/hypothesis/internal/compat.py Outdated
Comment thread hypothesis-python/src/hypothesis/strategies/_internal/utils.py Outdated
@Liam-DeVoe
Copy link
Copy Markdown
Member Author

Liam-DeVoe commented Dec 15, 2023

ruff wasn't happy with e9ba14c, so I pushed 0739659. It's probably more readable anyway.

@Zac-HD Zac-HD enabled auto-merge December 16, 2023 03:57
@Zac-HD Zac-HD closed this Dec 16, 2023
auto-merge was automatically disabled December 16, 2023 03:57

Pull request was closed

@Zac-HD Zac-HD reopened this Dec 16, 2023
@Zac-HD Zac-HD enabled auto-merge December 16, 2023 03:58
@Liam-DeVoe
Copy link
Copy Markdown
Member Author

Liam-DeVoe commented Dec 16, 2023

coverage failure due to a 3.12 only branch; my bad. Let's see if this passes ci.

@Zac-HD Zac-HD merged commit 867e56a into HypothesisWorks:master Dec 16, 2023
@Liam-DeVoe Liam-DeVoe deleted the json-defaultdict branch December 16, 2023 05:21
@Liam-DeVoe Liam-DeVoe restored the json-defaultdict branch December 16, 2023 05:21
@Liam-DeVoe Liam-DeVoe deleted the json-defaultdict branch December 16, 2023 05:21
@Zac-HD
Copy link
Copy Markdown
Member

Zac-HD commented Dec 16, 2023

🎉

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.

TypeError when drawing from a strategy that produces dataclasses with a defaultdict attribute

2 participants