-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Enforce that sync-ness matches. #17
Open
ssanderson
wants to merge
1
commit into
master
Choose a base branch
from
sync-async
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from textwrap import dedent | ||
|
||
import pytest | ||
|
||
from ..interface import implements, Interface, InvalidImplementation | ||
|
||
|
||
def test_async_interface_sync_impl(): | ||
|
||
class AsyncInterface(Interface): | ||
async def foo(self, a, b): # pragma: nocover | ||
pass | ||
|
||
# This should pass. | ||
class AsyncImpl(implements(AsyncInterface)): | ||
async def foo(self, a, b): # pragma: nocover | ||
pass | ||
|
||
# This should barf because foo isn't async. | ||
with pytest.raises(InvalidImplementation) as e: | ||
class SyncImpl(implements(AsyncInterface)): | ||
def foo(self, a, b): # pragma: nocover | ||
pass | ||
|
||
actual_message = str(e.value) | ||
expected_message = dedent( | ||
""" | ||
class SyncImpl failed to implement interface AsyncInterface: | ||
|
||
The following methods of AsyncInterface were implemented with invalid signatures: | ||
- foo(self, a, b) != async foo(self, a, b)""" | ||
) | ||
assert actual_message == expected_message | ||
|
||
|
||
def test_sync_interface_async_impl(): | ||
|
||
class SyncInterface(Interface): | ||
def foo(self, a, b): # pragma: nocover | ||
pass | ||
|
||
with pytest.raises(InvalidImplementation) as e: | ||
class AsyncImpl(implements(SyncInterface)): | ||
async def foo(self, a, b): # pragma: nocover | ||
pass | ||
|
||
actual_message = str(e.value) | ||
expected_message = dedent( | ||
""" | ||
class AsyncImpl failed to implement interface SyncInterface: | ||
|
||
The following methods of SyncInterface were implemented with invalid signatures: | ||
- async foo(self, a, b) != foo(self, a, b)""" | ||
) | ||
assert actual_message == expected_message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using inspect.iscoroutinefunction instead ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iscoroutinefunction
only checks if a function was defined as anasync def
by looking at theCO_COROUTINE
flag on the code object. For older codebases with lots ofyield from
coroutines, there's also thetypes.coroutine
decorator, which sets a different flag,CO_ITERABLE_COROUTINE
. This implementation handles both styles (and treats them as equivalent).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though, from reading the docs for
iscoroutinefunction
, I think it's just a bug that it doesn't handletypes.coroutine
functions correctly...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, it seems a bit more messy than what I originally thought:
So basically:
async def foo()
works as expectedasyncio.coroutine
generate a coroutine but inspect module doesn't realize this... I guess it's a bugtypes.coroutine
doesn't even turn the function into a coroutine function (i.e. callingtypes_coroutine
in the example returns the result instead of a coroutine), this is definitely a bug !Note that I've done this test with CPython 3.5.2 and 3.6.3, both giving the same result
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've realized I made a mistake with
types.coroutine
which must take a generator function (I was testing with a regular function).So my last bullet point was wrong, but anyway inspect doesn't work with it: