-
Notifications
You must be signed in to change notification settings - Fork 12
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
Support top level await #158
Conversation
The CI is failing because the As a workaround bump the minor part of I'm guessing that 3.6 might not be easy to make work with this, but I've been dropping support for 3.6 and 3.7 in all my other repos, and I'm willing to do that here too. |
Ah, I missed this comment and I've added a commit to fix the CI by overwriting the pypi version with the stored artefact wheel. Happy to revert if you don't like it. N.B. pypy seems to be failing for some reason.
Yes it won't work with 3.6 or 3.7 but as they're end of life probably makes sense to drop support. |
I do like it, but as I mentioned in my comment I think it doesn't actually solve the issue. I think the reason pypy is failing is because the download command doesn't have the |
So my latest commit installs the wheel directly from the path and works ok now with pypy. I would have thought in general that |
True, but users will also likely have a compiler such that the source install will work. It's an imperfect strategy, but I think its better than pinning versions to the ones that do have a binary on pypi, as the status of existent binaries can change at any time, and in this case these are test-time dependencies anyway, so its rare a user will actually WRT to this PR, I think a few things need to happen. There is a bug fix for Python 3.13 that was merged into main and I'd like to release that before adding this feature. Then I will make a new MR that bumps to In the meantime it would be good to add some documentation about this feature. This could be done by adding more text to the docstring in We also need a note in CHANGELOG.md |
Ok cool that all makes sense. w.r.t the CI what did you think about doing it this way:
Seems to work fine and solves the version issue. Do you want me to add |
I feel like at some point I had an issue with using something like the above. Perhaps being able to specify a file path is a newer feature in pip? If this does work such that it guarantees you install from the specified wheel, then I like specifying the filepath much better. I do like keeping the The relevant line in The idea behind the library is that I was getting fed up with applying every CI improvement I discover to all repos I maintain (there are 36 of them), so I've made an effort to codify the way I do CI into a templating or "cookiecutter" package. It is still a work in progress with a focus on the repos I maintain, but I'm slowly working on generalizing it for use beyond myself. |
I've finished up the release for 1.1.7, update the main branch is now on version 1.2.0, and dropped support for Python 3.6 and 3.7. Please rebase this PR on main so we can polish and land it. |
Ok cool, will do that tomorrow am. Will also look at the documentation as per your comments. |
1a25260
to
60db54f
Compare
Hi, so I've rebased and added the
Top level awaitsxdoctest natively supports top level awaits: >>> import asyncio
>>> await asyncio.sleep(0) If your code examples will contain top level awaits it is advisable to mention in your docs to start a repl session with (and maybe add it as another item in the Enhancements section) |
Now that the
I would like a small RST file in Doctests With Async Code
------------------------
Blurb with a small introduction and links to more resource about Python's async capabilities and asyncio in general.
The following example provides a file with async doctests:
.. code:: python
# async_doctest_demo.py
# Code that illustrates doctests on / with async code
async def read_laggy_resource(address):
# pretend to do work that takes a long time
return f"result from {address}"
async def read_all_resources():
"""
You can write doctests that test async functions directly.
Example:
>>> result = await read_all_resources()
>>> sorted(result)
>>> ['result from address1', 'result from address2']
"""
future1 = read_laggy_resource('address1')
future2 = read_laggy_resource('address2')
results = [
await future1,
await future2,
]
return results
If this file is named ``async_doctest_demo.py``, you can execute it with:
.. code:: bash
xdoctest async_doctest_demo.py
Caveats
-------
* If you are executing code in a doctest directly by copy/pasting it into a REPL, be sure that the REPL supports top-level ``await``. IPython supports this by default. For the standard Python REPL, use ``python -m asyncio``.
* Using top level awaits in tests that are already running in an event loop is not supported.
It would be nice if the example made it clear as to why you would want to have async code in your doctest. This is a question I don't currently know how to answer because I almost never use async when writing Python. If I need non-blocking code I almost always use |
5f8599b
to
463e932
Compare
I reverted rather dropped the commit in case you ever decide to re-instate. I do think a CI should be able to fully pass tests for individual PRs without relying on a version bump but it's your call.
It's not really a question of why you would want to have async code in the doctest. If you are an author of a library that exposes async methods you need to have async examples so users know how to the use the library. The problem is that without support for top level awaits the code examples get very messy as you need to wrap everything in a function and call it with |
Hi @Erotemic, anything else to do here? |
I agree as well, but this is a problem across all my repos, and I want to think about the best way to handle it, so it is out of the scope of this PR. Even though I'm not going to merge it here, I appreciate the snippet and discussion and and have found both helpful.
I noticed that my linting checks on the CI are fairly weak, so I fixed some of those issues. I also broke out the handling of the All of the main logic looks good, the only other concern I have is the name of the exception |
Ok that's done |
LGTM. Thank you, this is a big improvement! |
New version is released. Pip install and try it out. |
Tried this out in our production CI and it's working great. Many thanks for the quick release! |
This is now natively supported since [xdoctest #158](Erotemic/xdoctest#158) has been released so no need for the monkey patching fixture anymore.
While I had the fork open I figured it'd worthwhile adding this. I've had it running for a while in a test fixture that patches
compile
,exec
andeval
.Happy to connect on discord if you want to discuss, my handle is sdb9696.
Closes #115