Repository navigation
fix code action for Call to deprecated overload contextlib.contextmanager #4245 - #4281
asukaminato0721 wants to merge 2 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
|
This pull request has been imported. If you are a Meta employee, you can view this in D114356296. (Because this pull request was imported automatically, there will not be any future comments.) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
5e796cf to
10b589f
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
stroxler
left a comment
There was a problem hiding this comment.
Thanks! I think there are a number of issues to clear up before we merge this:
-
The replacement fails at runtime before Python 3.13.
[deprecated_contextmanager.rs:38–51](https://github.com/meta/pyrefly/blob/7bd91ff3c57020fc2f9e9856967b3173a07b5262/pyrefly/lib/state/lsp/quick_fixes/deprecated_contextmanager.rs#L38-L51)
Without postponed annotations, the generatedtyping.Generator[int]andtyping.AsyncGenerator[int]raiseTypeError: Too few argumentson Python 3.12. Emit full-arity annotations for older Python versions. -
Adding the import can invalidate
__future__imports.
[lsp.rs:3633–3644](https://github.com/meta/pyrefly/blob/7bd91ff3c57020fc2f9e9856967b3173a07b5262/pyrefly/lib/state/lsp.rs#L3633-L3644)
The action insertsfrom typing import Generatorbeforefrom __future__ import annotations, causing aSyntaxError. This is an existing import-helper defect exposed by the new action. Ordinary imports must go after future imports. -
Explicit calls rewrite the wrong function.
[deprecated_contextmanager.rs:36–44](https://github.com/meta/pyrefly/blob/7bd91ff3c57020fc2f9e9856967b3173a07b5262/pyrefly/lib/state/lsp/quick_fixes/deprecated_contextmanager.rs#L36-L44)
Forcontextmanager(producer)insidedef wrapper() -> int, the fix changes the wrapper’s return annotation toGenerator, leavingproduceruntouched. Require the diagnostic to belong to the decorator of the function being edited. -
The annotation’s outer name is replaced indiscriminately.
[deprecated_contextmanager.rs:38–51](https://github.com/meta/pyrefly/blob/7bd91ff3c57020fc2f9e9856967b3173a07b5262/pyrefly/lib/state/lsp/quick_fixes/deprecated_contextmanager.rs#L38-L51)
Annotated[Iterator[int], "tag"]becomesGenerator[Iterator[int], "tag"], corrupting both the yielded type and metadata. Resolve the annotation base to the expected iterator, or decline wrapped annotations and bound aliases. -
Replacement bindings are not checked.
[deprecated_contextmanager.rs:42–76](https://github.com/meta/pyrefly/blob/7bd91ff3c57020fc2f9e9856967b3173a07b5262/pyrefly/lib/state/lsp/quick_fixes/deprecated_contextmanager.rs#L42-L76)
A module exporting onlyIteratorgets rewritten fromapi.Iterator[int]to nonexistentapi.Generator[int]. A localGenerator = intalso shadows the inserted import. Preserve qualifiers only when they export the replacement, and use collision-safe bindings.
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Summary
Fixes #4245
Adds a quick fix replacing Iterator with Generator, including the required import.
Supports qualified annotations such as typing.Iterator.
Also supports the equivalent AsyncIterator -> AsyncGenerator deprecation.
Test Plan
add test