[adapter] refactor: tidy _resolve_checkpoint_path call sites#161
Merged
Jayce-Ping merged 2 commits intoMay 17, 2026
Merged
Conversation
…_path
Move the `path = os.path.expanduser(path)` call out of `load_checkpoint`
and into `_resolve_checkpoint_path` itself. The resolver is now the
single entry point that takes a raw user-provided spec and returns a
usable local directory; callers no longer need to remember to normalize
`~` separately.
Safe in all three input branches:
- Local path with `~`: expanded correctly (unchanged behavior).
- Local path without `~`: no-op.
- HF spec ('owner/repo' or 'hf://...'): no-op since `expanduser`
only acts on a leading `~`.
Co-authored-by: Cursor <cursoragent@cursor.com>
Two small cleanups in the HF-checkpoint resolve path, both follow-ups
to the un-gating change:
A. Remove the dead `if not os.path.exists(path)` guard in
`load_checkpoint`. `_resolve_checkpoint_path` now either:
- returns a path it has just confirmed via `os.path.exists`, or
- returns a downloaded path that `download_hf_checkpoint` has
verified with `os.path.isdir`, or
- raises `FileNotFoundError` with the actual root cause.
The guard was useful pre-ungating (the old post-barrier
`snapshot_download` call could return a non-existent path on
cache-miss-after-failure); now it's unreachable AND its error
message would lie about the failure mode.
B. Collapse the redundant `spec` local in `_resolve_checkpoint_path`.
`parse_hf_checkpoint_path` already strips the `hf://` prefix
internally, and the local-path `os.path.exists` check is gated on
`not force_hf` so passing `path` (with the prefix still attached on
the HF branch) is safe. Removes one variable from the reader's
mental model.
Pure refactor: no behavior change. The 8 happy-path + 5 original-error
+ 6 path-traversal parser cases all still pass.
Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Small follow-up refactor to BaseAdapter checkpoint path handling. Moves os.path.expanduser into _resolve_checkpoint_path, removes a now-unreachable os.path.exists guard with a misleading error message in load_checkpoint, and collapses a redundant spec local variable since parse_hf_checkpoint_path strips the hf:// prefix internally. No behavioral change.
Changes:
- Hoist
expanduserinto_resolve_checkpoint_pathso callers don't need to remember to normalize~separately. - Drop the unreachable post-resolve
os.path.existscheck inload_checkpointwhose error message lied about the failure mode. - Pass
pathdirectly toparse_hf_checkpoint_path(which already handles thehf://prefix) and remove the redundantspecvariable.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
Two small follow-up refactors to
BaseAdapter._resolve_checkpoint_pathandBaseAdapter.load_checkpoint, both prompted by re-reading the code after PR #160 was merged. No behavior change.What changes
A. Hoist
os.path.expanduserinto_resolve_checkpoint_path(commit 1)Currently
load_checkpointdoes:The two normalization steps belong together.
_resolve_checkpoint_pathis the one entry point that takes a raw user-provided spec (local path OR HF repo spec, with or withouthf://) and returns a usable local directory; making it ownexpandusertoo means callers no longer have to remember to normalize~separately.Safe across all three input branches:
~: expanded correctly (unchanged behavior).~: no-op.owner/repoorhf://...): no-op sinceexpanduseronly acts on a leading~.ModelArguments.__post_init__still callsexpanduser(resume_path)defensively at config-parse time, so other code that displays/logsmodel_args.resume_pathkeeps seeing the normalized form. The double call is idempotent.B. Drop dead
os.path.existsguard + collapsespeclocal (commit 2)After the un-gating in PR #160,
_resolve_checkpoint_pathhas these exit paths:os.path.exists.download_hf_checkpointhas verified withos.path.isdir.FileNotFoundErrorwith the actual root cause.So the post-resolve check in
load_checkpointwas unreachable AND its error message lied about the failure mode (it said "not found locally or on HF Hub", but_resolve_checkpoint_pathwould already have raised the real reason). Removed:Same commit also collapses the redundant
speclocal in_resolve_checkpoint_path.parse_hf_checkpoint_pathalready strips thehf://prefix internally, and the local-pathos.path.existscheck is gated onnot force_hf, so passingpathdirectly (with the prefix still attached on the HF branch) is safe. One fewer variable for the reader to track.Diff
Net:
-9/+8insrc/flow_factory/models/abc.py. No other files touched.Test plan
resume_path: "owner/repo"(will exercise the cleaned-up flow).Out of scope
ModelArguments.__post_init__'sexpandusercall — kept; defensive normalization at config-parse time, idempotent with the resolver, removing it could surprise other readers ofmodel_args.resume_path.Made with Cursor