Skip to content
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

Add async method mock #148

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

MetalHurlant
Copy link

No description provided.

@MetalHurlant
Copy link
Author

Not sure this is the best way to do it. please let me know if you have comments

@ollipa
Copy link
Member

ollipa commented Nov 3, 2023

@MetalHurlant, thanks for the PR! Some comments:

  • I think the sync_ and async_ methods with the underscore don't fit so well in the API. How about calling them make_sync and make_async?
  • Dependencies are probably missing pytest-asyncio so tests in CI are failing.
  • Test coverage should be increased to test that everything works with different types of methods and spying too. You can see some examples in my earlier attempt to add async support: 626ca53

Anyway, this looks like a simple an pragmatic way to add async support to flexmock 👍

@christophe-riolo, @adarshk7 what do you think?

@MetalHurlant
Copy link
Author

Thanks for the comments. I will update the PR.

@christophe-riolo
Copy link
Contributor

I had a quick glance it looks like a nice change, I'll look more in detail asap !

Copy link
Contributor

@christophe-riolo christophe-riolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your contribution 🙏
I have a couple of questions and suggestions, but overall I like the way you have designed this addition 👍

src/flexmock/_api.py Outdated Show resolved Hide resolved
@@ -778,6 +797,14 @@ def _match_args(self, given_args: Any) -> bool:
return False
return True

def _verify_not_async_spy(self):
"""Check if trying to assert the output of an async call."""
is_spy = self._replace_with is self.__dict__.get("_original")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking, this could potentially be extracted in a property, maybe outside of this PR what do you think @ollipa ?

src/flexmock/_api.py Outdated Show resolved Hide resolved
src/flexmock/_api.py Outdated Show resolved Hide resolved
@@ -872,6 +899,7 @@ def and_return(self, *values: Any) -> "Expectation":
>>> plane.passenger_count()
3
"""
self._verify_not_async_spy()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to forbid this ? It feels like we could check the result of the spied function in an asynchronous way. It could require more work that could be on us @ollipa

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could forbid it now and allow it in the future when we make an implementation for it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As olippa said. For now it would not work properly so I preferred raising a clear error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I was suggesting, but so we could create an issue for that 👍

tests/features/mocking.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (58a2eb8) 100.00% compared to head (0757ea1) 99.53%.
Report is 2 commits behind head on master.

Files Patch % Lines
src/flexmock/_api.py 86.20% 4 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##            master     #148      +/-   ##
===========================================
- Coverage   100.00%   99.53%   -0.47%     
===========================================
  Files            4        4              
  Lines          834      863      +29     
===========================================
+ Hits           834      859      +25     
- Misses           0        4       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@ollipa ollipa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a few comments comments but overall looks very good!

"pypy-3.8",
"pypy-3.9",
"3.7",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that Pypy has released 3.10 version. You can include that here.

@@ -57,7 +55,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.7"
python-version: "3.11"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are meant to be executed with the lowest Python version we support. Otherwise

Suggested change
python-version: "3.11"
python-version: "3.8"

@@ -89,7 +87,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.7"
python-version: "3.11"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
python-version: "3.11"
python-version: "3.8"

@@ -21,7 +21,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.7"
python-version: "3.11"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
python-version: "3.11"
python-version: "3.8"

- Python 3.8
- Python 3.9
- Python 3.10
- Python 3.11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also add Python 3.12 here.

@@ -28,6 +28,24 @@
RE_TYPE = type(re.compile(""))


async def future_raise(anything: type[BaseException]) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using type directly requires Python 3.9.

Suggested change
async def future_raise(anything: type[BaseException]) -> None:
async def future_raise(anything: Type[BaseException]) -> None:

@@ -28,6 +28,24 @@
RE_TYPE = type(re.compile(""))


async def future_raise(anything: type[BaseException]) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we don't need these functions to be part of the public API?

Suggested change
async def future_raise(anything: type[BaseException]) -> None:
async def _future_raise(anything: type[BaseException]) -> None:

raise anything


async def future(anything: Any) -> Any:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def future(anything: Any) -> Any:
async def _future(anything: Any) -> Any:

Comment on lines +1105 to +1106
"""Set the return values of the expectation to coroutines
Need to be set before the return value is set
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Set the return values of the expectation to coroutines
Need to be set before the return value is set
"""Set the return value to be a coroutine.
Needs to be set before the return value is set.


def make_sync(self) -> "Expectation":
"""Make the mocked method synchronous.
Need to be set before the return value is set
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Need to be set before the return value is set
Needs to be set before the return value is set.

@ollipa
Copy link
Member

ollipa commented Dec 3, 2023

@MetalHurlant, if you rebase your changes you can remove docs/requirements.txt. That file is not needed anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants