-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Add hint if async context manager is used in non-async with statement #18299
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
31fb5cb
Add info when using 'with' but implementing async variants __aenter_…
lipefree de8aeed
Fix typo in mdtest with sync
lipefree cd9fbe3
Fix format in mdtest with sync
lipefree 75c71ce
Fix format in mdtest and update snapshot in with sync
lipefree 43cd589
Remove markdown syntax for highlighting
lipefree 8ef2373
Subdiagnosis are given even when the function is not well typed or wh…
lipefree 72f9a91
Change arguments type for __exit__ from Type::none(db) to Type::unkno…
lipefree 86721f1
Minor rewording and simplification
sharkdp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...dtest/snapshots/sync.md_-_With_statements_-_Context_manager_with…_(380131e1948d8d01).snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
| --- | ||
| mdtest name: sync.md - With statements - Context manager with `__aenter__` and `__aexit__` | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/with/sync.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | class Manager: | ||
| 2 | async def __aenter__(self): ... | ||
| 3 | async def __aexit__(self, *args): ... | ||
| 4 | | ||
| 5 | # error: [invalid-context-manager] "Object of type `Manager` cannot be used with `with` because it does not implement `__enter__` and `__exit__`" | ||
| 6 | with Manager(): | ||
| 7 | ... | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[invalid-context-manager]: Object of type `Manager` cannot be used with `with` because it does not implement `__enter__` and `__exit__` | ||
| --> src/mdtest_snippet.py:6:6 | ||
| | | ||
| 5 | # error: [invalid-context-manager] "Object of type `Manager` cannot be used with `with` because it does not implement `__enter__` and... | ||
| 6 | with Manager(): | ||
| | ^^^^^^^^^ | ||
| 7 | ... | ||
| | | ||
| info: Objects of type `Manager` *can* be used as async context managers | ||
| info: Consider using `async with` here | ||
| info: rule `invalid-context-manager` is enabled by default | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These
__aenter__and__aexit__calls will fail if you modify/extend your test example and annotated the parameter types of these two methods on theManagerclass accordingly.Nonewill not generally be a valid type for all of the arguments.We do not necessarily need to be very strict with the passed argument types here (since we're only using it to add a diagnostic hint, and it seems fine to emit it even if that call would fail when passing the wrong parameters). So I guess it's fine to pass
Type::unknown()here instead? Another option would be to allow not justOk(_)results, but alsoErr(CallDunderError::CallError(..))?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.
Hey ! Thank you for the review ! If I understand correctly you want to add the sub-diagnosis event if the code was not annotating the types correctly ?
Example :
But even in this case, we should provide the sub-diagnosis even if the provided typing is incorrect. I guess it makes sense. I am just too unexperienced yet, should I implement it to take care of this case ? But then should we also provide the sub-diagnosis if the user is passing the wrong number of arguments ?
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.
Probably? I think you could achieve this by following this route:
I think we should pass
Type::unknown()anyway. PassingNoneis just wrong.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.
I will address this and add more testing then !
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.
Just to clarify, in case it was unintentional: you're still using
Type::nonein your latest commit, instead ofType::unknown.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.
Sorry I just changed it in the last commit. Thank you very much for bearing with me !