python / python/cpython

unittest mock with __new__ function lead to TypeError

Open
#123,723 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

import unittest
from unittest.mock import patch

class Summary(object):

    def __init__(self, path):
        print("Summary init")

    def __del__(self):
        print("Summary del")

    def get_summary(self):
        print("get_summary")

class MockSummary:
    def __init__(self, path):
        print("Mock start")

    def __del__(self):
        print("Mock end")

    def get_summary(self):
        print("mock_get_summary")

class TestSummary(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_summary(self):
        # 1 success case
        summary1 = Summary('path')
        summary1.get_summary()

        # 2 mock case
        mock = patch.object(Summary, '__new__', return_value=MockSummary('path'))
        mock.start()
        summary2 = Summary('path')
        summary2.get_summary()
        mock.stop()

        # 3 fail case
        summary3 = Summary('path')
        summary3.get_summary()

if __name__ == '__main__':
    unittest.main()

When run this python unittest file, it turns out with error:


Summary init
get_summary
Mock start
mock_get_summary
ESummary del

======================================================================
ERROR: test_summary (__main__.TestSummary.test_summary)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/admin/workspace/python/testSummaryAll.py", line 46, in test_summary
    summary3 = Summary('path')
               ^^^^^^^^^^^^^^^
TypeError: object.__new__() takes exactly one argument (the type to instantiate)

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
Mock end

All the instance init after the mock stop will be failed.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied unittest reproduction and inspect unittest.mock.patch.object behavior when patching Summary.new. Reproduce the failure after mock.stop(), then trace how the original constructor behavior is restored. Done means instances can be created after the patch is stopped and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.