[issue37015] Fix asyncio mock warnings

2019-05-30 Thread Andrew Svetlov


Andrew Svetlov  added the comment:

Fixed

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

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



[issue37015] Fix asyncio mock warnings

2019-05-30 Thread miss-islington


miss-islington  added the comment:


New changeset 0f39c2b1919727904f4fac2d79cb41dc6bfe41fe by Miss Islington (bot) 
(Xtreak) in branch 'master':
bpo-37015: Ensure tasks created by _accept_connection2 due to AsyncMock are 
completed (GH-13661)
https://github.com/python/cpython/commit/0f39c2b1919727904f4fac2d79cb41dc6bfe41fe


--
nosy: +miss-islington

___
Python tracker 

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



[issue37015] Fix asyncio mock warnings

2019-05-29 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


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

___
Python tracker 

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



[issue37015] Fix asyncio mock warnings

2019-05-24 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

@asvetlov No problem then if it can be fixed after beta.

--

___
Python tracker 

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



[issue37015] Fix asyncio mock warnings

2019-05-24 Thread Andrew Svetlov


Andrew Svetlov  added the comment:

xtreak thanks for investigation.
I have no time to work on it now, sorry.
Maybe the next week.
Anyway, the problem can be fixed even in python beta stage if we don't do it 
earlier.

--

___
Python tracker 

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



[issue37015] Fix asyncio mock warnings

2019-05-24 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

This logic is present in create_autospec too at [0]. The behavior for patch 
seems to be documented at [1] which also needs to be updated to reflect that an 
AsyncMock by default is created instead of MagicMock if the target is an async 
function unlike 3.7. Probably also a versionchanged directive to note this if 
the current behavior is okay. Since it generates a RuntimeWarning I think it 
will be good if the behavior is decided before 3.8 beta 1.

> If new is omitted, then the target is replaced with a MagicMock. If patch() 
> is used as a decorator and new is omitted, the created mock is passed in as 
> an extra argument to the decorated function. If patch() is used as a context 
> manager the created mock is returned by the context manager.

$ python3.7 -q
>>> import asyncio
>>> from unittest.mock import create_autospec
>>> loop = asyncio.get_event_loop()
>>> loop_mock = create_autospec(loop)
>>> loop_mock._accept_connection2


$ ./python.exe -q
>>> import asyncio
>>> from unittest.mock import create_autospec
>>> loop = asyncio.get_event_loop()
>>> loop_mock = create_autospec(loop)
>>> loop_mock._accept_connection2


[0] 
https://github.com/python/cpython/blob/11b38e5409861f4db345a4dd45dcc9ba470c/Lib/unittest/mock.py#L2564
[1] https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch

--

___
Python tracker 

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



[issue37015] Fix asyncio mock warnings

2019-05-22 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

I guess cause is at [0] . When there is no new value specified and with spec 
being None if mock.patch is used on an async object (original = 
_accept_connection2). Here original is an async object then AsyncMock is 
returned. 

Changing this causes test failures where patching AsyncClass.async_method now 
expects an AsyncMock but as per older behavior it returns MagicMock. I think 
it's a behavior that needs to be discussed as it differs from 3.7 and not sure 
if AsyncMock would always be awaited as seen in test_accept_connection_multiple.

[0] 
https://github.com/python/cpython/blob/b121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5/Lib/unittest/mock.py#L1313

if spec is None and _is_async_obj(original):
Klass = AsyncMock
else:
Klass = MagicMock

--
nosy: +lisroach, mariocj89

___
Python tracker 

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



[issue37015] Fix asyncio mock warnings

2019-05-22 Thread Karthikeyan Singaravelan

Karthikeyan Singaravelan  added the comment:

patching _accept_connection2 attribute on loop object seems to return an 
AsyncMock.

➜  cpython git:(master) ✗ cat ../backups/bpo37015.py
import asyncio
from unittest.mock import patch
with patch.object(asyncio.get_event_loop(), '_accept_connection2') as f:
print(f)
f()
➜  cpython git:(master) ✗ ./python.exe ../backups/bpo37015.py

../backups/bpo37015.py:5: RuntimeWarning: coroutine 'AsyncMockMixin._mock_call' 
was never awaited
  f()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback


Relevant test

def test_accept_connection_multiple(self):
sock = mock.Mock()
sock.accept.return_value = (mock.Mock(), mock.Mock())
backlog = 1
# Mock the coroutine generation for a connection to prevent
# warnings related to un-awaited coroutines.
mock_obj = mock.patch.object
with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
print(f"{accept2_mock=}")
accept2_mock.return_value = None
with mock_obj(self.loop, 'create_task') as task_mock:
task_mock.return_value = None
self.loop._accept_connection(
mock.Mock(), sock, backlog=backlog)
self.assertEqual(sock.accept.call_count, backlog)

When I specify new value which defaults to DEFAULT as Mock() then there is no 
AsyncMock. Same can be done in test and the warnings go away. My suspicion is 
that if there is a loop object with _accept_connection2 earlier in Python 3.7 
 is returned by patch.object but now it 
returns an  instead

# use explicit mock

import asyncio
from unittest.mock import patch, Mock

with patch.object(asyncio.get_event_loop(), '_accept_connection2', Mock()) as f:
print(f)
f()

➜  cpython git:(master) ✗ ./python.exe ../backups/bpo37015.py


--

___
Python tracker 

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



[issue37015] Fix asyncio mock warnings

2019-05-22 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Not sure if it helps. Set backlog in the below test as 1. Add a print statement 
print(type(_mock_call)) at [0] . I could see some of _mock_call to be AsyncMock 
. So instead of calling return _mock_self._mock_call(*args, **kwargs) directly 
if I call them only with they are not instance of AsyncMock (not 
isinstance(_mock_self, AsyncMock)) then the warning goes away. 

Guess somewhere an AsyncMock is created as I can see _accept_connection2. When 
the backlog value is set to 100 it shows lot of warnings.

$ ./python.exe -Werror -X tracemalloc -m unittest -vv 
test.test_asyncio.test_selector_events.BaseSelectorEventLoopTests.test_accept_connection_multiple


test_accept_connection_multiple 
(test.test_asyncio.test_selector_events.BaseSelectorEventLoopTests) ... 






ok

--
Ran 1 test in 0.123s

OK
Exception ignored in: 
Traceback (most recent call last):
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/warnings.py", 
line 510, in _warn_unawaited_coroutine
warn(msg, category=RuntimeWarning, stacklevel=2, source=coro)
RuntimeWarning: coroutine 'AsyncMockMixin._mock_call' was never awaited

[0] 
https://github.com/python/cpython/blob/b121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5/Lib/unittest/mock.py#L991

--

___
Python tracker 

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



[issue37015] Fix asyncio mock warnings

2019-05-22 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



[issue37015] Fix asyncio mock warnings

2019-05-22 Thread Andrew Svetlov


Change by Andrew Svetlov :


--
title: Fix asyncio mock wranings -> Fix asyncio mock warnings

___
Python tracker 

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