You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pylance Typing checking error for AsyncTransaction #1015
However, the Pylance VSCode extension (powered by Pyright) in Basic Type checking mode is giving a type error: Object of type "AsyncTransaction" cannot be used with "with" because it does not implement __aenter__ Object of type "AsyncTransaction" cannot be used with "with" because it does not implement __aexit__
I believe that it is because AsyncTransaction's __aenter__ and __aexit__ are wrapped with functools.wraps(AsyncTransactionBase._enter) and functools.wraps(AsyncTransactionBase._exit) respectively
I'm thinking this might rather be an issue with the chosen type checker (Pylance, in your case).
Reasons I'm thinking this:
mypy is happy with this
Looking at how functools.wraps is annotated in typeshed should make the type checker infer that wraps(xyz) returns the identity function (only regarding __call__). So wraps(xyz)(abc).__call__(...) should be equivalent to abc.__call__(...). In our example, the type checker should consider __aenter__ and __aexit__ to exist and be callable.
I agree what you said, the bug does seem to come from pyright and not the neo4j code: I ran the code against other type checkers (pyre-check and pytype) and it seems that only pyright raised an issue here.
However, I am wondering if the AsyncTransaction methods should be wrapped with the AsyncTransactionBase ones : the functools.wraps function's documentation seems to indicate that functools.wraps is mostly used for decorated functions. I'm not sure what the value of adding this decorator here: the docstrings could easily be moved to the AsyncTransaction methods, as the AsyncTransactionBase class is not exposed to the library consumers.
You are right in that wraps does not seem to be apt here. While I could use the optional arguments to wraps to make it only copy the docs string (and maybe annotations), there's no good reason for this complication. So I cleaned up the code in the PR linked above. Thanks again for reaching out.
Activity
robsdedude commentedon Jan 31, 2024
Hi and thanks for reaching out.
I'm thinking this might rather be an issue with the chosen type checker (Pylance, in your case).
Reasons I'm thinking this:
mypyis happy with thisfunctools.wrapsis annotated intypeshedshould make the type checker infer thatwraps(xyz)returns the identity function (only regarding__call__). Sowraps(xyz)(abc).__call__(...)should be equivalent toabc.__call__(...). In our example, the type checker should consider__aenter__and__aexit__to exist and be callable.Please let me know if you disagree and why.
eric-nguyen-cs commentedon Feb 3, 2024
Thank you for your quick response
I agree what you said, the bug does seem to come from
pyrightand not theneo4jcode: I ran the code against other type checkers (pyre-checkandpytype) and it seems that onlypyrightraised an issue here.However, I am wondering if the
AsyncTransactionmethods should bewrappedwith theAsyncTransactionBaseones : thefunctools.wrapsfunction's documentation seems to indicate thatfunctools.wrapsis mostly used for decorated functions. I'm not sure what the value of adding this decorator here: the docstrings could easily be moved to theAsyncTransactionmethods, as theAsyncTransactionBaseclass is not exposed to the library consumers.robsdedude commentedon Feb 5, 2024
You are right in that
wrapsdoes not seem to be apt here. While I could use the optional arguments towrapsto make it only copy the docs string (and maybe annotations), there's no good reason for this complication. So I cleaned up the code in the PR linked above. Thanks again for reaching out.eric-nguyen-cs commentedon Feb 6, 2024
And thank you for quickly responding and solving the issue!