StefanBRas opened a new issue, #8515:
URL: https://github.com/apache/opendal/issues/8515

   ### OpenDAL version or commit
   
   commit a042fea
   
   ### Affected service or component
   
   python sdk
   
   ### Describe the bug
   
   Currently `AsyncFile.__aexit__` looks like this:
   ```python
   
   @final
   class AsyncFile:
       def __aexit__(
           self,
           /,
           exc_type: type[BaseException] | None,
           exc_value: BaseException | None,
           traceback: types.TracebackType | None,
       ) -> None: ...
   ```
   
   Per the python [docs `__aexit__` _must return an 
awaitable_](https://docs.python.org/3/reference/datamodel.html#asynchronous-context-managers),
 but since the function is not marked as async and returns a plain `None` type 
checkers will flag it as incorrect.
   
   ### Steps to Reproduce
   
   ```python
   import collections.abc
   import types
   from typing import Any, final
   
   import typing_extensions
   
   @final
   class AsyncFile:
       def __aenter__(self, /) -> 
collections.abc.Awaitable[typing_extensions.Self]: ...
       def __aexit__(
           self,
           /,
           exc_type: type[BaseException] | None,
           exc_value: BaseException | None,
           traceback: types.TracebackType | None,
       ) -> None: ...
   
   
   async def main(): 
       async with AsyncFile() as b: # Object of type `AsyncFile` cannot be used 
with `async with` because `__aexit__` does not return an awaitable 
(invalid-context-manager) [Ln 26, Col 16]
           ...
   ```
   
   [link to ty playground showing 
this](https://play.ty.dev/5b323bd9-bf02-442f-8394-a19a4f698217)
   
   ### Actual Behavior
   
   A type checker like `ty` complains with
   ```
   Object of type `AsyncFile` cannot be used with `async with` because 
`__aexit__` does not return an awaitable (invalid-context-manager) [Ln 26, Col 
16]
   ```
   
   ### Expected Behavior
   
   It should type check correctly by either marking the function as `async` or 
the return type as `collections.abc.Awaitable[None]` in the same way 
`__aenter__` does
   
   ### Additional Context
   
   I saw that your run `ty` as a type checker in `lint` check, however in 
`pyproject.toml` it's scoped to only include the .pyi files because of 
`tool.ty.src.include = ["python/opdendal"]`.
   
   Since the actual code being typechecked are just the types, it's never 
confirmed that the types hold for actual use. I think you could gain a lot from 
also running the typechecker on your tests (and then adding more type hints to 
the checks).  For example, if done to `test_read.py` this error would have been 
caught. Additionally when I added type hints to `test_read.py` I found two 
other potential type inconsistencies
   
   - in line 129 `Operator.write` is called with `bs=bytearray` but typed as 
`bs: bytes`
   - in line 373 `File.readinto` is called with `bytes` but typed to take 
`bytearray | memoryview`
   
   Both of which `ty` flags.
   
   Pyrefly has an [article about the value of running type checkers (even 
multiple) on your test code](https://pyrefly.org/blog/too-many-type-checkers/)
   
   I can submit a PR for the whole thing - let me know if you would like that 
and how you feel about adding typecheck to your tests
   
   ### Reproduction confirmation
   
   - [x] I reproduced this issue against the affected component. For a 
service-specific report, I used the actual service rather than only a mock, 
emulator, or source-code analysis.
   
   ### Are you willing to submit a PR to fix this bug?
   
   - [x] Yes, I would like to submit a PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to