-
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 7 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
85 changes: 85 additions & 0 deletions
85
...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,85 @@ | ||
| --- | ||
| 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 | ... | ||
| 8 | class Manager: | ||
| 9 | async def __aenter__(self): ... | ||
| 10 | async def __aexit__(self, exc_type: str, exc_value, traceback): ... | ||
| 11 | | ||
| 12 | # error: [invalid-context-manager] "Object of type `Manager` cannot be used with `with` because it does not implement `__enter__` and `__exit__`" | ||
| 13 | with Manager(): | ||
| 14 | ... | ||
| 15 | class Manager: | ||
| 16 | async def __aenter__(self): ... | ||
| 17 | async def __aexit__(self, exc_type, exc_value, traceback, wrong_extra_arg): ... | ||
| 18 | | ||
| 19 | # error: [invalid-context-manager] "Object of type `Manager` cannot be used with `with` because it does not implement `__enter__` and `__exit__`" | ||
| 20 | with Manager(): | ||
| 21 | ... | ||
| ``` | ||
|
|
||
| # 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 | ... | ||
| 8 | class Manager: | ||
| | | ||
| 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 | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| 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:13:6 | ||
| | | ||
| 12 | # error: [invalid-context-manager] "Object of type `Manager` cannot be used with `with` because it does not implement `__enter__` an... | ||
| 13 | with Manager(): | ||
| | ^^^^^^^^^ | ||
| 14 | ... | ||
| 15 | class Manager: | ||
| | | ||
| 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 | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| 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:20:6 | ||
| | | ||
| 19 | # error: [invalid-context-manager] "Object of type `Manager` cannot be used with `with` because it does not implement `__enter__` an... | ||
| 20 | with Manager(): | ||
| | ^^^^^^^^^ | ||
| 21 | ... | ||
| | | ||
| 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.
This can be simplified to
I think.
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.
Thank you very much for all the reviews you gave, I really learned a lot during this PR about python, rust and ty !