Repository navigation
feat(fabric): add return-type overloads for Fabric.setup, setup_optimizers and setup_dataloaders - #22009
Open
dai123x wants to merge 2 commits into
Open
feat(fabric): add return-type overloads for Fabric.setup, setup_optimizers and setup_dataloaders#22009dai123x wants to merge 2 commits into
dai123x wants to merge 2 commits into
Conversation
…izers and setup_dataloaders Fabric.setup returned Any and the other two methods returned a union of the single- and the multi-object case, so type checkers lost the shape of the most common calls (Lightning-AI#21988). Add typing.overload signatures that follow the number of arguments. Annotations only; runtime behavior is unchanged. Signed-off-by: dai123x <daixuan26@outlook.com>
dai123x
requested review from
ethanwharris,
justusschock and
tchaton
as code owners
October 7, 2026 10:51
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Adds
typing.overloadsignatures forFabric.setup,Fabric.setup_optimizersandFabric.setup_dataloaders, so the declared return type follows the number of arguments.Fixes #21988
Fabric.setuppreviously returnedAny, and the other two methods returned a union of the single- and the multi-object case, so type checkers lost the shape of the most common calls and every access on the result needed a cast or an explicit annotation (examples in #21988). Annotations only — no runtime behavior change.fabric.setup(model)Any_FabricModulefabric.setup(model, optimizer)Anytuple[_FabricModule, _FabricOptimizer]fabric.setup(model, optimizer, scheduler=lr)Anytuple[_FabricModule, _FabricOptimizer, LRScheduler]fabric.setup(model, opt1, opt2)Anytuple[_FabricModule | _FabricOptimizer | LRScheduler, ...]fabric.setup_optimizers(opt)_FabricOptimizerfabric.setup_optimizers(opt1, opt2)tuple[_FabricOptimizer, ...]fabric.setup_dataloaders(loader)DataLoaderfabric.setup_dataloaders(loader1, loader2)list[DataLoader]Design notes (kept as close to the sketch in #21988 as possible):
*args. Calls the runtime rejects (e.g.setup_optimizers()with no arguments) then fall through to the implementation signature instead of being typed with a shape the runtime never returns.setup(module=model)keeps working:modulestays keyword-callable, while the optimizer arguments are positional-only, matching the runtime signature.For reviewers
requirements/typing.txt): no new errors. A consumer snippet resolves every shape in the table above, verified withreveal_type.pytest tests/tests_fabric/test_fabric.py tests/tests_fabric/test_wrappers.pypasses locally (Windows, CPU torch 2.12.0): 112 passed, 66 skipped (distributed/TPU skips), 0 failures.Before submitting
Fabric.setup,setup_optimizers, andsetup_dataloadersfor precise return types #21988 describes the problem and sketches these overloads.No breaking changes — annotations only.