[issue41768] unittest.mock spec calls class properties

2021-12-03 Thread Alex Waygood


Change by Alex Waygood :


--
nosy: +lukasz.langa

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41768] unittest.mock spec calls class properties

2021-12-03 Thread Nikita Sobolev


Nikita Sobolev  added the comment:

Related PR: https://github.com/python/cpython/pull/29901

--
nosy: +sobolevn

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41768] unittest.mock spec calls class properties

2020-10-20 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +cjw296, lisroach, mariocj89

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41768] unittest.mock spec calls class properties

2020-09-18 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

Without lines numbers, I cannot test which of the two identical asserts failed. 
Either comments or msg arguments will differentiate.  Nor can I run snippets 
from two different files. Here is a minimal reproducible self-contained code 
that demonstrates the claim (which I verified on 3.9 and current master).

import unittest
from unittest.mock import Mock

class SomethingElse(object):
def __init__(self):
self._instance = None

@property
def instance(self):
if not self._instance:
self._instance = 'object'

class Test(unittest.TestCase):

def test_property_not_called_with_spec_mock(self):
obj = SomethingElse()
self.assertIsNone(obj._instance, msg='before') # before
mock = Mock(spec=obj)
self.assertIsNone(obj._instance, msg='after') # after

unittest.main()

--
nosy: +michael.foord, terry.reedy
versions: +Python 3.10, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41768] unittest.mock spec calls class properties

2020-09-11 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41768] unittest.mock spec calls class properties

2020-09-11 Thread melwitt


Change by melwitt :


--
keywords: +patch
pull_requests: +21264
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/22209

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41768] unittest.mock spec calls class properties

2020-09-11 Thread melwitt


New submission from melwitt :

When async magic method support was added to unittest.mock.Mock to address 
issue #26467, it introduced a getattr call [1] that causes class properties to 
be called when the class is used as a mock spec.

This caused a problem for a test in my project when running with Python 3.8 
where previously the test worked OK with Python 3.6.

The test aims to verify that a class instance is not created if the called code 
path does not access the class property and thus the class will not create a 
heavy object unless it's needed (lazy create on access via @property).

As of Python 3.8, the @property is always called and is called by the mock spec 
process itself, even though the code path being tested does not access the 
class @property.

Here is a code snippet that illustrates the @property calling from the mock 
spec alone:

class SomethingElse(object):
def __init__(self):
self._instance = None

@property
def instance(self):
if not self._instance:
self._instance = 'object'

...

def test_property_not_called_with_spec_mock(self):
obj = SomethingElse()
self.assertIsNone(obj._instance)
mock = Mock(spec=obj)
self.assertIsNone(obj._instance)

$ ./python -m unittest -v 
unittest.test.testmock.testmock.MockTest.test_property_not_called_with_spec_mock
test_property_not_called_with_spec_mock 
(unittest.test.testmock.testmock.MockTest) ... FAIL

==
FAIL: test_property_not_called_with_spec_mock 
(unittest.test.testmock.testmock.MockTest)
--
Traceback (most recent call last):
  File "/home/vagrant/cpython/Lib/unittest/test/testmock/testmock.py", line 
2173, in test_property_not_called_with_spec_mock
self.assertIsNone(obj._instance)
AssertionError: 'object' is not None

[1] 
https://github.com/python/cpython/blob/fb2718720346c8c7a0ad2d7477f20e9a5524ea0c/Lib/unittest/mock.py#L492

--
components: Library (Lib)
messages: 376757
nosy: melwitt
priority: normal
severity: normal
status: open
title: unittest.mock spec calls class properties
type: behavior
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com