Repository navigation
impl introduce_factory #2207 - #2694
asukaminato0721 wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new LSP refactor code action to “introduce factory” for a selected Python class, inserting a @staticmethod factory method and rewriting constructor call sites across loaded modules.
Changes:
- Implement
introduce_factoryquick-fix that inserts a static factory method with a unique name and rewrites constructor calls. - Wire the new refactor into the
TransactionAPI and the non-wasm LSP server refactor pipeline. - Add LSP code-action tests for same-file rewrites, cross-module rewrites, unique-name fallback, and unsupported-call rejection.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
pyrefly/lib/state/lsp/quick_fixes/introduce_factory.rs |
New refactor implementation: find selected class, insert factory, rewrite constructor call sites across modules. |
pyrefly/lib/state/lsp/quick_fixes/mod.rs |
Exposes the new introduce_factory quick-fix module. |
pyrefly/lib/state/lsp.rs |
Adds Transaction::introduce_factory_code_actions entry point. |
pyrefly/lib/lsp/non_wasm/server.rs |
Wires the refactor into the server’s refactor action pipeline/timing. |
pyrefly/lib/test/lsp/code_actions.rs |
Adds coverage for introduce-factory code actions and edit application. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
This comment has been minimized.
This comment has been minimized.
bd99705 to
8ff478c
Compare
This comment has been minimized.
This comment has been minimized.
|
@jvansch1 this can be reviewed |
9d1b3f0 to
7a49800
Compare
This comment has been minimized.
This comment has been minimized.
7a49800 to
18685c5
Compare
This comment has been minimized.
This comment has been minimized.
18685c5 to
8e86d16
Compare
This comment has been minimized.
This comment has been minimized.
9d24dcd to
ce2c0c4
Compare
This comment has been minimized.
This comment has been minimized.
|
@kinto0 has imported this pull request. If you are a Meta employee, you can view this in D113596086. |
kinto0
left a comment
There was a problem hiding this comment.
i'd love some details on what this feature is in the summary + some kind of description as to why you chose to implement it this way
| let Some(module_info) = transaction.get_module_info(&module_handle) else { | ||
| continue; | ||
| }; | ||
| let Some(refs) = transaction.local_references_from_definition( |
There was a problem hiding this comment.
local references on every module? do we need this?
There was a problem hiding this comment.
still unclear. is this a performance issue?
a094c46 to
af17cbd
Compare
This comment has been minimized.
This comment has been minimized.
• Implemented a native introduce_factory refactor in introduce_factory.rs:36 and wired it into the LSP refactor pipeline in lsp.rs:2452 and server.rs:4145. The action is refactor.rewrite, triggers on a class name, inserts a class-local @staticmethod factory with a generated name like create, and rewrites direct constructor calls across loaded modules to use Class.create(...). It rejects constructor forms it cannot safely rewrite, such as subscripted calls like Box[int](). Added coverage in code_actions.rs:381 and code_actions.rs:3476 for same-file rewrites, cross-module rewrites, unique-name fallback, and unsupported-call rejection.
af17cbd to
7918e05
Compare
This comment has been minimized.
This comment has been minimized.
|
I'm closing this PR: it is too old and has a stale diff attached, which makes review hard. I'm not actually confident that it's worth reopening given that agents don't seem to use these kinds of refactors much; the priority is much lower than it was when we first made the IDE features issue. That said, if you do want to reopen it here are some AI review suggestions - the current implementation is probably not mergable: Correctness (must fix)
Tests
Simplicity and performance
PR description
Lower confidence (not reproduced): an open file with unsaved changes might get the same edit twice, once through the disk copy and once through the editor copy. One test would settle it. |
|
Whoops. I think this auto-closed when I closed the IDE features issue (which I did mean to do - I don't want that issue spinning off new work anymore - but I'm willing to try to land work already done) |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Summary
Fixes part of #2207
The feature adds a static factory such as
Service.create(*args, **kwargs)and rewrites constructor calls fromService(...)toService.create(...).Matching now distinguishes actual constructors from calls such as
Service.some_classmethod(...); unsupported forms such asBox[int]()suppress the action to prevent an incomplete refactor.Reference collection now uses reverse-dependency-based global references for workspace files.
Only uncovered in-memory modules are checked individually because unsaved editor buffers may not appear in the filesystem dependency graph.
This avoids querying local references for every loaded module while preserving edits in open files.
Test Plan
Added coverage for same-file rewrites, cross-module rewrites, unique-name fallback, and unsupported-call rejection.