Skip to content

unittest.mock: Regression with create_autospec and dataclasses in 3.14.0b2 #135368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cdce8p opened this issue Jun 10, 2025 · 6 comments
Open
Assignees
Labels
topic-dataclasses type-bug An unexpected behavior, bug, or error

Comments

@cdce8p
Copy link
Contributor

cdce8p commented Jun 10, 2025

Bug report

Bug description:

from dataclasses import dataclass
from unittest.mock import create_autospec

@dataclass
class Description:
    name: str

mock = create_autospec(Description, instance=True)
print(isinstance(mock, Description))
# 3.13.3
True

# 3.14.0b2
False

Furthermore the resulting mock doesn't have attributes present on a dataclass instance, like __annotations__, __class__, __dataclass_fields__, __dataclass_params__.

print(dir(mock))
print(dir(Description("Hello World"))

Likely related to #124429. /CC @sobolevn

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

@cdce8p cdce8p added the type-bug An unexpected behavior, bug, or error label Jun 10, 2025
@sobolevn
Copy link
Member

cc @ncoghlan and @ericvsmith

@ericvsmith
Copy link
Member

As I said on #124429, I'm not a mock user. That said, the regression does seem problematic. @cjw296 is in CODEOWNERS for mock.

@sobolevn
Copy link
Member

sobolevn commented Jun 11, 2025

We might need to revert the change, it seems to be rather hard and the fix is not obvious, at least for me now :(

@cjw296
Copy link
Contributor

cjw296 commented Jun 11, 2025

@sobolevn - #124429 was your PR so I hope you can dig into this...

I'd probably tackle this as follows:

  • create new branch from the commit before gh-124176: Add special support for dataclasses to create_autospec #124429 landed, and add tests for isinstance(mock, Description) and the attributes the OP mentions, verifying the past so we basically confirm the PR is the problem
  • rebase the original PR branch (I think GH hangs onto its HEAD commit on the PR even if it was deleted)
  • See what happens with the new tests

If they fail, #124429 should probably be reverted for now, andthe old PR plus new tests put on a branch somewhere that a suitably motivated contributor could pick up.

If a fix can be created, then a new PR should go up containing the additional tests we missed first time round along with the commit for the fix.

Hopefully this all makes sense?

@sobolevn
Copy link
Member

Yes, I will surely work on this. If I can find the solution - I will create a PR with the fix. Otherwise, I propose to revert #124429

@sobolevn sobolevn self-assigned this Jun 11, 2025
@cjw296
Copy link
Contributor

cjw296 commented Jun 11, 2025

Start with the missing tests first plz, they'll be a valuable artefact whatever the outcome :-)

@vstinner vstinner changed the title Regression with create_autospec and dataclasses in 3.14.0b2 unittest.mock: Regression with create_autospec and dataclasses in 3.14.0b2 Jun 11, 2025
sobolevn added a commit to sobolevn/cpython that referenced this issue Jun 12, 2025
sobolevn added a commit to sobolevn/cpython that referenced this issue Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-dataclasses type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants