-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-5623: Loosen AsyncClientSession.with_transaction
callback type
#2591
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
base: master
Are you sure you want to change the base?
PYTHON-5623: Loosen AsyncClientSession.with_transaction
callback type
#2591
Conversation
AsyncClientSession.with_transaction
callback typeAsyncClientSession.with_transaction
callback type
Thanks. For clarity, could you provide a code example that incorrectly fails type checking before this change? |
Hey! Thanks for your work on this! I acknowledge the need for this change. |
We're using Trio, and for compatibility with asyncio libraries like So, I want to use Of course, I can imagine other scenarios where someone may have a callback that returns something that is
Ah... I see. Well, the gist of this is that
I'd be happy to, assuming it's a simple change, but I'm not sure exactly how it works. If this is simple for you to do, then by all means, go for it! We'll use a |
Is the required change to mongo-python-driver/tools/synchro.py Lines 316 to 325 in 6a796c8
mongo-python-driver/tools/synchro.py Line 290 in 6a796c8
If so: diff --git a/tools/synchro.py b/tools/synchro.py
index e3d48355..640af42b 100644
--- a/tools/synchro.py
+++ b/tools/synchro.py
@@ -287,6 +287,7 @@ def process_files(
with open(file, "r+") as f:
lines = f.readlines()
lines = apply_is_sync(lines, file)
+ lines = translate_awaitable_types(lines)
lines = translate_coroutine_types(lines)
lines = translate_async_sleeps(lines)
if file in docstring_translate_files:
@@ -313,6 +314,18 @@ def apply_is_sync(lines: list[str], file: str) -> list[str]:
return lines
+def translate_awaitable_types(lines: list[str]) -> list[str]:
+ awaitable_types = [line for line in lines if "Awaitable[" in line]
+ for type in awaitable_types:
+ res = re.search(r"Awaitable\[([A-z]+)\]", type)
+ if res:
+ old = res[0]
+ index = lines.index(type)
+ new = type.replace(old, res.group(1))
+ lines[index] = new
+ return lines
+
+
def translate_coroutine_types(lines: list[str]) -> list[str]:
coroutine_types = [line for line in lines if "Coroutine[" in line]
for type in coroutine_types: |
That looks about right -- @aclark4life can you confirm? |
Summary
AsyncClientSession.with_transaction
'scallback
is typed asCallable[[AsyncClientSession], Coroutine[Any, Any, _T]]
but does not use any ofCoroutine
's interface that isn't already provided in its parent,Awaitable
.In other words: at runtime, any function that returns an
Awaitable
works ascallback
, butcallback
's type unnecessarily requires that the function's return type matchesCoroutine
.The type should be changed from
Callable[[AsyncClientSession], Coroutine[Any, Any, _T]]
toCallable[[AsyncClientSession], Awaitable[_T]]
so that a non-Coroutine
Awaitable
can be used ascallback
.Changes in this PR
Changed the type of
AsyncClientSession.with_transaction
'scallback
argument fromCallable[[AsyncClientSession], Coroutine[Any, Any, _T]]
toCallable[[AsyncClientSession], Awaitable[_T]]
.Testing Plan
Type-checking should cover this change.
Checklist
Checklist for Author
Checklist for Reviewer {@primary_reviewer}