Skip to content

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

Closed

Description

@cdce8p

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Jun 10, 2025
  2. sobolevn commented on Jun 11, 2025

    @sobolevn
    Member
  3. ericvsmith commented on Jun 11, 2025

    @ericvsmith
    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.

  4. sobolevn commented on Jun 11, 2025

    @sobolevn
    Member

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

  5. cjw296 commented on Jun 11, 2025

    @cjw296
    Contributor

    @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?

  6. sobolevn commented on Jun 11, 2025

    @sobolevn
    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

  7. self-assigned this
    on Jun 11, 2025
  8. cjw296 commented on Jun 11, 2025

    @cjw296
    Contributor

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

  9. 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[/+] on Jun 11, 2025
  10. added 2 commits that reference this issue on Jun 12, 2025
  11. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Jun 13, 2025
  12. added a commit that references this issue on Jun 14, 2025
  13. added a commit that references this issue on Jun 14, 2025
  14. added a commit that references this issue on Jun 14, 2025
  15. efimov-mikhail commented on Jun 14, 2025

    @efimov-mikhail
    Member

    So, this issue could be closed, right?

  16. cdce8p commented on Jun 14, 2025

    @cdce8p
    ContributorAuthor

    Thanks everyone! Looking forward to the next beta release.

  17. added a commit that references this issue on Jun 19, 2025
  18. added a commit that references this issue on Jul 12, 2025
  19. added a commit that references this issue on Aug 4, 2025
  20. added a commit that references this issue on Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.14bugs and security fixes3.15bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-dataclassestype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions