-
Notifications
You must be signed in to change notification settings - Fork 31
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
Replace treading.local with contextvar #107
base: master
Are you sure you want to change the base?
Conversation
@dokime7 Thanks for the PR - please note, that this package needs to stay compatible with Python 2.7 and Python 3.5 |
OK, I will update my PR |
Updated, feel free to give feedback. |
Thank you for your contribution. According to the contributing policies of the zopefoundation organization you need to sign a contributor agreement before any non-trivial change can be merged. For details please consult the Contributing guidelines for zopefoundation projects. |
This package uses https://pypi.org/project/coverage-python-version/ so marking the code paths needed for Python 2 resp. 3 should bring the coverage back to the expected value. |
OK, I've pushed a fix |
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.
That's what I meant with my comment regarding https://pypi.org/project/coverage-python-version/.
Explanation: We are running coverage only on Python 3.8 so code needed just for older Python versions (especially) Python 2.7 should be marked with # pragma: PY2
instead of no cover
as we want to see coverage for code which has different branches for different Python versions.
Co-authored-by: Michael Howitz <[email protected]>
Co-authored-by: Michael Howitz <[email protected]>
OK, sorry, it is much clear for me now :) |
This still requires signing the contributor agreement. Please see the Contributing guidelines for zopefoundation projects. |
class ThreadTransactionManager(threading.local): | ||
"""Thread-local | ||
`transaction manager <transaction.interfaces.ITransactionManager>`. | ||
if USE_CONTEXTVAR: # pragma: PY3 |
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.
If you switch the if
order it might make the diff much more easy to read:
if not USE_CONTEXTVAR:
# old/current code that on the diff is only shown as being indented
else:
# new code, that shows up as new code
otherwise a quick glimse at the code feels like the new code is actually the old one 😅
In order to use 'transaction' on an asyncio framework like FastAPI, it's needed to use ContextVar instead of threading.local that is not compatible with asyncio.
ContextVar is fully compatible with threading.local and can be used as a replacement of.
ContextVar is a Python standard lib since 3.7 and can be installed as a lib on 3.6 but unfortunately not compatible with older Python versions.