[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2019-09-13 Thread Andrew Svetlov


Andrew Svetlov  added the comment:

I believe with AsyncMock implemented we can close the issue

--

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2019-09-13 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

I believe it's still a problem with Mock though MagicMock and AsyncMock work 
but I am not sure what would be the correct behavior that whether Mock should 
behave like a coroutine or not as per the discussion here.

./python.exe
Python 3.9.0a0 (heads/master:375a3e2bdb, Sep 13 2019, 14:18:36)
[Clang 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest.mock import Mock, MagicMock, AsyncMock
>>> import inspect
>>> def f(): pass
...
>>> for m in MagicMock, AsyncMock : print(inspect.iscoroutinefunction(m(f)))
...
True
True
>>> for m in Mock, MagicMock, AsyncMock : 
>>> print(inspect.iscoroutinefunction(m(f)))
...
Traceback (most recent call last):
  File "", line 1, in 
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/inspect.py", 
line 194, in iscoroutinefunction
return _has_code_flag(obj, CO_COROUTINE)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/inspect.py", 
line 180, in _has_code_flag
return bool(f.__code__.co_flags & flag)
TypeError: unsupported operand type(s) for &: 'Mock' and 'int'

--

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2019-09-13 Thread Andrew Svetlov


Andrew Svetlov  added the comment:

I believe after implementing AsyncMock we can close the issue

--
nosy: +asvetlov
resolution:  -> out of date
stage:  -> 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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2019-06-12 Thread STINNER Victor


Change by STINNER Victor :


--
nosy:  -vstinner

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2019-06-12 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +lisroach, xtreak
versions: +Python 3.8, Python 3.9 -Python 3.5

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2016-01-25 Thread Michael Foord

Changes by Michael Foord :


--
nosy:  -gvanrossum

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2016-01-22 Thread Guido van Rossum

Guido van Rossum added the comment:

Am I really still needed on this issue?

On Tue, Jan 19, 2016 at 7:46 PM, Hiroyuki Takagi 
wrote:

>
> Hiroyuki Takagi added the comment:
>
> Thank you for reviewing patch.
>
> I wrote test and updated patch. To pass the test, both this patch and
> issue25599's patch are required.
>
> Changes of the patch:
> - copy __code__ not only functions but also methods
> - add autospec (create_autospec) suppoort
>
> I have completely missed about autospec, thank you for a mention about it.
> For autospec, simply copying original __code__ to funcopy makes error on
> existing tests.
> That's why I changed the src of exec, but it seems to be quite ad-hoc. It
> may be better to be improved, but I don't have any good idea, sorry.
>
> On the tests of this patch, I wonder if it's better to use assertIs(..,
> True/False) instead of assertTrue/False, since it was one of the problem in
> issue25599.
> To apply this change and pass test, need to change
> asyncio.iscoroutinefunction to return bool. The change would be very easy,
> just update issue25599's patch like `return_value = bool(getattr(func, ...`.
>
> --
> Added file: http://bugs.python.org/file41664/mock2.patch
>
> ___
> Python tracker 
> 
> ___
>

--

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2016-01-19 Thread Michael Foord

Michael Foord added the comment:

In inspect checking that __code__ is a code object, or that co_flags is an int, 
would be better than special casing mock. 

However, the patch to mock looks reasonable to me. It copies the whole code 
object from the original function to the mock object. The patch needs a test 
(I'd like to see a test for the Mock(func) case and the create_autospec(func) 
case.)

--

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2016-01-19 Thread Hiroyuki Takagi

Hiroyuki Takagi added the comment:

Thank you for reviewing patch.

I wrote test and updated patch. To pass the test, both this patch and 
issue25599's patch are required.

Changes of the patch:
- copy __code__ not only functions but also methods
- add autospec (create_autospec) suppoort

I have completely missed about autospec, thank you for a mention about it.
For autospec, simply copying original __code__ to funcopy makes error on 
existing tests.
That's why I changed the src of exec, but it seems to be quite ad-hoc. It may 
be better to be improved, but I don't have any good idea, sorry.

On the tests of this patch, I wonder if it's better to use assertIs(.., 
True/False) instead of assertTrue/False, since it was one of the problem in 
issue25599.
To apply this change and pass test, need to change asyncio.iscoroutinefunction 
to return bool. The change would be very easy, just update issue25599's patch 
like `return_value = bool(getattr(func, ...`.

--
Added file: http://bugs.python.org/file41664/mock2.patch

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2016-01-18 Thread Hiroyuki Takagi

Hiroyuki Takagi added the comment:

Thank you for review and comment.

Honestly speaking, I couldn't find any other good place to fix it.

One possible solution might be to use isinstance(mock, Mock) in 
iscoroutinefunction, but I don't think it's good for inspect module to add 
special check and depend on unittest.mock. Mocks are usually used only in 
debug/test, but iscoroutinefunction is used in production code. Adding some 
check to iscoroutinefunction only for test is not good for performance (though, 
actually its effect will be very small).

The reasons why I think this behavior should be fixed are,

- Raising error and stopping test is not kind for mock users
- After the patch (issue25599), no mock object can become `True` to 
iscoroutinefunction, which will make it impossible to test the block after 
if-iscoroutinefunction by using mock.


Now, I checked inspect module again, and found one more unexpected behavior 
(not error).

>>> def a(): yield 1
>>> inspect.isgeneratorfunction(a)
True

>>> inspect.isgeneratorfunction(Mock(a))
False

With the patch, inspect.isgeneratorfunction(Mock(a)) returns True.

--

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2016-01-18 Thread Yury Selivanov

Yury Selivanov added the comment:

It looks like this should be fixed in the mock module, as special casing it in 
inspect doesn't look right.  Unfortunately, I can't review this patch, as I 
don't know the mock module internals and I don't think I understand all 
consequences of this patch.

Michael, what do you think?

--
nosy: +michael.foord

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2016-01-17 Thread Guido van Rossum

Guido van Rossum added the comment:

Why fix this in mock rather than in the iscoroutinefunction functions? (Not
a rhetorical question -- I really am not sure what the better place is to
fix this, or if we should even consider fixing it.)

--

___
Python tracker 

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



[issue26140] inspect.iscoroutinefunction raises TypeError when checks Mock of function or coroutinefunction

2016-01-17 Thread Hiroyuki Takagi

New submission from Hiroyuki Takagi:

inspect.iscoroutinefunction and asyncio.iscoroutinefunction with patch of 
issue25599 (https://bugs.python.org/issue25599) raise TypeError when used to 
check Mock object which mocks function or coroutinefunction.

How to reproduce:

- For the mock of function

>>> from unittest.mock import Mock
>>> import inspect
>>> def a():...
... 
>>> inspect.iscoroutinefunction(Mock(a))

Expected: False
Actual: 
Traceback (most recent call last):
  File "", line 1, in 
  File ".../cpython/Lib/inspect.py", line 187, in iscoroutinefunction
object.__code__.co_flags & CO_COROUTINE)
TypeError: unsupported operand type(s) for &: 'Mock' and 'int'


- For the mock of coroutine-function

>>> async def b():...
... 
>>> inspect.iscoroutinefunction(Mock(b))

Expected: True
Actual:
Traceback (most recent call last):
  File "", line 1, in 
  File ".../cpython/Lib/inspect.py", line 187, in iscoroutinefunction
object.__code__.co_flags & CO_COROUTINE)
TypeError: unsupported operand type(s) for &: 'Mock' and 'int'


Without the patch of issue25599, asyncio.iscoroutinefunction does not raise 
error and returns Mock object. But I don't think it is expected behavior, as 
discussed in that issue.

I wrote a patch to solve this problem.

--
components: asyncio
files: mock.patch
keywords: patch
messages: 258464
nosy: gvanrossum, haypo, miyakogi, yselivanov
priority: normal
severity: normal
status: open
title: inspect.iscoroutinefunction raises TypeError when checks Mock of 
function or coroutinefunction
type: behavior
versions: Python 3.5
Added file: http://bugs.python.org/file41638/mock.patch

___
Python tracker 

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