Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

reset_mock resetsMagicMock's magic methods in an unexpected way #123934

Closed
Assignees
sobolevn
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@mentalAdventurer

Description

@mentalAdventurer

Bug report

Bug description:

Thereset_mock(return_value=True) method behaves in a wrong/inconsistent way.
When used withMagicMock, the methodreset_mock(return_value=True) does not reset the return values of the magic methods. Only if you call for example__str__ and then call the reset_mock function, the return value will be reset, but not to the default value.

fromunittestimportmockmm=mock.MagicMock()print(type(mm.__str__()))mm.reset_mock(return_value=True)print(type(mm.__str__()))print(type(mm.__hash__()))mm.reset_mock(return_value=True)print(type(mm.__hash__()))

Output

<class'str'><class'unittest.mock.MagicMock'><class'int'><class'unittest.mock.MagicMock'>

Since Python 3.9PRreset_mock now also resets child mocks. This explains the behaviour. Calling the__str__ method creates a childMagicMock with a set return value. Since this child mock now exists, its return value is reset when reset_mock(return_value=True) is called.
Although this can be logically explained, it's counter-intuitive and annoying as I'm never sure which values are being reset.

I would expect the same behaviour asMock. The return value of__str__ and other magic methods should not be effected.

fromunittestimportmockm=mock.Mock()print(type(m.__str__()))m.reset_mock(return_value=True)print(type(m.__str__()))print(type(m.__hash__()))m.reset_mock(return_value=True)print(type(m.__hash__()))

Output

<class'str'><class'str'><class'int'><class'int'>

CPython versions tested on:

3.10

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

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

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp