Skip to content

When mocking with unsafe=True the mocked instances don't receive the unsafe argument. #94478

Open

Description

@LakmalCaldera

Bug report

When the feature Check against misspellings of assert etc. in mock was introduced, the unsafe flag was moved out from kwargs and defined as a named argument.
Previously since unsafe was implicitly part of kwargs, it was silently propagated from _patch class to the instantiation of the klass variable inside the __enter__ method.
But now, the unsafe flag as an explicitly named argument is not being propagated to the instantiation of the Klass variable.
Hence the Mock class is unable to set its internal attribute _mock_unsafe and will always be False regardless of whether the user set unsafe=True or not.

The test cases have to be updated to show this bug.

This bug was introduced in this commit: fdb9efc (bpo-41877) and only included in versions from Python 3.10.

Your environment

  • CPython versions tested on: 3.10, 3.11 and master branch.
  • Operating system and architecture: Ubuntu 22.04

Activity

  1. added a commit that references this issue on Jul 16, 2022
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 25, 2023
  3. ryan-duve commented on Jan 16, 2026

    @ryan-duve
    Contributor

    @LakmalCaldera I see the patch on your fork for this. Is anything preventing a PR to the CPython repo so a core developer can review your change and address this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions