Replace treading.local with contextvar - #107
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 |
icemac
left a comment
There was a problem hiding this comment.
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 <mh@gocept.com>
Co-authored-by: Michael Howitz <mh@gocept.com>
|
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.
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 codeotherwise 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.