Repository navigation
feat(compat): keep old import paths working across the package reorganization - #5167
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
dc59503 to
9e70f8b
Compare
77a9e2d to
c266804
Compare
svcnvidia-nemo-ci
left a comment
There was a problem hiding this comment.
SHIP WITH CARE
Five RISK findings affect migration correctness and compatibility-test coverage; one NOTE concerns warning frequency. The move tables are currently empty, so these are defects in the new migration/compatibility mechanisms, not demonstrated production outages.
Completed static inspection of all six changed files and directly affected paths. Two delegated report payloads were unavailable; their assignments were re-inspected directly. Tests were not executed, and the suggested corrections require author verification.
Review generated by codex. This was a full review. Request a full review with /review reset=true.
ec68b15 to
e44d49d
Compare
e44d49d to
3b7adfa
Compare
yaoyu-33
left a comment
There was a problem hiding this comment.
SHIP WITH CARE
This PR adds the compatibility layer the package reorganization (#5166) depends on: MovedModuleFinder serves an old module path as the same module object, moved_names_getattr serves names split out of a module, and the PR adds a versioned removal guard, the recorded import surface, and scripts/migrate_imports.py. Aliasing the old path to the same object, instead of stub re-export modules, is the right design. Classes, registries, pickles and mock.patch through old paths all keep working, and we checked isinstance, pickle and Ray's cloudpickle through an alias. Both move tables are empty here, so nothing changes at runtime yet. The comments are about the mechanism that the move PRs will rely on.
Two things are worth fixing before the first move lands:
- RISK: an old-path import replaces the moved module's
__spec__. That breaksimportlib.resourcesfor every caller, for example NeMo RL'sassert_golden_vectors()once token capture moves. The fix is about four lines; see the suggestion. - RISK: the import-surface guard checks names only for modules that already have a
MOVED_*entry. A split PR that forgets the wholeMOVED_NAMESblock for a module still passes CI.
The other inline comments are NOTEs, your call:
python -m <old path>fails;- moved modules stop being attributes of
nemo_gym; - chained moves warn twice;
- the rewriter does not validate its output and drops comments when it splits an import;
- some guard tests only run once the tables have entries.
Two earlier automated-review threads still have a small open case; I replied on both.
Notes for later PRs in the stack
- #5224: needs the
__spec__fix beforenemo_gym.token_id_capturemoves. A NeMo RL usage test that callsassert_golden_vectors()through the old path would pin it. - #5195: 7 of its 12
MOVED_NAMESentries never reach__getattr__, because the old module still imports those names. They are the sixserver_processhelpers inserver_utils.pyandcli.utils.did_you_mean. So they never warn, and deleting the entries in 0.10.0 removes nothing. - #5225 and #5232:
base_responses_api_modelandrollout_collectionalready install amoved_attr_getter__getattr__. A split there has to merge it withmoved_names_getattr, not replace it. - #5189: makes the
nemo_gymattribute note live, for examplenemo_gym.global_configafterimport nemo_gym.server_utils.
Validation. Reviewed at 3b7adfa. 2ee5790 is the same six-file diff rebased onto #5317.
- Locally, the two new test files pass (87 tests).
- The unit suite (sandbox-marked tests deselected) fails the same tests as the base commit, so this PR adds no failures.
- CI ran on 3b7adfa: Server suite shard 0 failed on a Docker Hub pull rate limit in
harbor_agent_general, which is unrelated. CI has not run on the rebased head yet. - Not run: GPU tests.
yaoyu-33
left a comment
There was a problem hiding this comment.
Approving. Both move tables are empty in this PR, so the two RISKs above can't break anything yet. They should land before the first move PR that depends on them:
|
On the notes for later PRs in the stack:
|
yaoyu-33
left a comment
There was a problem hiding this comment.
Re-approving at cb763ae. Both RISKs from the earlier approval are fixed: exec_module now restores the module's own __spec__, so find_spec and reload on the new path work after an old-path import, and the surface test now checks every recorded name at its old path. The failing checks are the non-blocking harness conformance job.
…nization Add the machinery that keeps every current nemo_gym import path working while modules move into the new package layout, before any module moves. - nemo_gym/_moved_paths.py records each moved module and each name split out of a module, with the release the move first ships in. A finder installed first on sys.meta_path serves an old module path as the same module object as its new path, so code never runs twice and patches through the old path still take effect. Each use warns once, naming the new path, the removal release and the running version. - Old paths are removed two minor releases after they move. A unit test fails once the running version reaches an entry's removal release. - A recorded import surface of today's modules and names, checked for every module the reorganization touches. - Tests for the imports and patches NeMo RL relies on. - scripts/migrate_imports.py rewrites old paths to new ones, for move PRs, open branches and downstream projects. Gaps found while moving the first packages: - A one-line `from <module> import <names>` written as text, such as a code sample in docs or a code template in a string, was left on the old path when names split out of that module. It is now rewritten like a real import statement. - fern/docs.yml holds redirects whose sources are the URLs of moved modules, so it is no longer rewritten. Library reference URLs (.../nemo-gym/nemo_gym/<module>) are left alone everywhere, because those redirects cover them. - monkeypatch.setattr(<old module>, "<name>", ...) and patch.object for a name that split out of that module now produce a note: such a patch reaches only code that still looks the name up in the old module, so it needs checking by hand. The recorded import surface left out every typing object, so it missed the type aliases Gym defines, such as NeMoGymResponseInputItem, and a module split could drop one without the surface test noticing. Aliases a module defines are now recorded, names imported from typing itself are still left out, and keys that are not identifiers (pydantic's cached generic classes such as `BaseEpisodeRequest[Any]`) are skipped. The surface is recorded again on the current main. A module that serves names split out of it can later move itself: episode_types, rollout_correlation and server_utils all do. Its MOVED_NAMES entries stay under its old path, but its __getattr__ then runs under the new module name and found nothing. Look the names up under every old path MOVED_MODULES maps to the module, directly or as part of a moved package. The import rewriter rewrote nemo_gym/server_utils.py inside a GitHub link pinned to a commit, which pointed the link at a file that commit does not have. Skip paths that follow /blob/<sha>/, /tree/<sha>/ or /raw/<sha>/. Review fixes for scripts/migrate_imports.py: - A split `from` import that follows other code on its line (`if flag: from ...`, `x = 1; from ...`) now stays in that line's statement list, joined with `;`. One that starts its line keeps that line's own indentation, tabs included. - `old_module.Name` in code, for a name split out of old_module, is rewritten only when the file imports the new module. Otherwise it is left at the old module, which still serves the name, and reported: the new module may not be loaded there. - Relative imports are resolved against the file's package, so `from .server_utils import X` and `from .openai_utils import Name` are reported, and relative imports outside nemo_gym are not. - A patch of a split-out name is reported by the module its receiver is bound to (`import ... as su`, `from nemo_gym import x as su`), not by the name alone, and not for receivers bound otherwise. Review fixes: - moved_names_getattr warned on every access. It now warns once per old name, like old module paths, and still looks the name up on every access, so a patch of the new module shows through. - The recorded import surface missed seven modules that exist before any move, among them nemo_gym.verifiers and four Harbor task modules. Record it again from the whole tree the moves start from. Scripts that a sandbox runs against its own installed nemo-gym need a fallback to the paths before the reorganization. The rewriter now leaves any line marked `# path before nemo_gym 0.8.0` unchanged, in code and in text such as a runner template held in a string. Names split out of a module stay recorded under its old path. Once that module moves too, code imports the names from the new path, and the rewriter did not recognize them there. Register each name move under the module's current path as well. RunHelper reaches nemo_gym.cli through the CLI refactor's __getattr__ shim. monkeypatch.setattr restored it with setattr, which left it in the module's namespace, so a later test that expects the shim's warning failed when it ran after this one on the same worker. Patch the module's namespace instead, so undoing the patch removes the name. Signed-off-by: Ananth Subramaniam <[email protected]>
What and why
The first PR of the
nemo_gympackage reorganization. It moves nothing. It adds what keeps every current import path working while later PRs move modules, so users, open branches, and NeMo RL do not break.__spec__, soimportlib.reloadandfind_specstill work;python -m <old path>runs the new module, with the same warning; and a package attribute such asnemo_gym.global_configresolves before anything imports it.scripts/migrate_imports.pyrewrites old paths for move PRs, open branches, and downstream projects, and reports what it cannot fix safely.# path before nemo_gym 0.8.0keeps its old path. Scripts that a sandbox runs against its own installed nemo-gym use it to fall back to the old layout.Where this PR sits
The stack lands bottom-up; each PR's base is the branch of the PR before it, so each diff shows only that PR's change.
How an old module path keeps working
flowchart LR U["import nemo_gym.server_utils"] --> F{"MovedModuleFinder<br/>first on sys.meta_path"} F -->|"path is in MOVED_MODULES"| W["Warn once: new path,<br/>moved in, removed in, running"] W --> N["Import nemo_gym.server_runtime.server_utils"] N --> S["Register the same module object<br/>under the old name"] F -->|"any other path"| P["Normal import"] classDef this fill:#fde68a,stroke:#b45309,stroke-width:2px,color:#1f2937 class F,S thismock.patch("<old path>.<name>"),from nemo_gym import <old module>, config strings such as"nemo_gym.token_id_capture.lineage:FileLineageStore", and old pickles all reach the code that runs.How a split-out name keeps working
flowchart LR A["nemo_gym.server_utils.request"] --> G{"Still defined or imported<br/>in server_utils?"} G -->|"yes"| R["That object"] G -->|"no"| M["Module __getattr__<br/>reads MOVED_NAMES"] M --> X["Warn once, return the same object from<br/>nemo_gym.client.http_client"] classDef this fill:#fde68a,stroke:#b45309,stroke-width:2px,color:#1f2937 class M,X thisA module that serves split-out names can move later itself, as
episode_typesandserver_utilsdo. Its entries stay under its old path, and its__getattr__still finds them once the module runs under its new name.When old paths go away
What the tests check
Finder, against a temporary package tree: one module object under both names, no second execution of a moved package's submodules, patches and config strings through the old path, old pickles,
find_specorigins, the warning text and the file it points at, one warning per path. After an old-path import the module keeps its own__spec__, soimportlib.resourcesstill reads a moved package's data files andimportlib.reloadworks. A nested key wins over its parent's, andpython -m <old path>runs the new module.Move tables, with fake entries while the real tables are empty: an entry is overdue from the removal release's prereleases on (
0.10.0rc0), a moved name's warning points at the importing file, and no entry's destination is itself an old path, which would warn twice.Recorded import surface:
tests/unit_tests/fixtures/import_surface.jsonlists every module and the names an importer can rely on, including values such asPARENT_DIRand type aliases Gym defines, such asNeMoGymResponseInputItem. A module that moves without an alias, or loses a recorded name, fails. Every recorded module is checked name by name, so a split that forgets its wholeMOVED_NAMESblock fails too, and the names an existingmoved_attr_gettershim serves are recorded with the rest. A PR that removes a name on purpose refreshes that module's entry.NeMo RL: every
from nemo_gym... import ...in NeMo RL's code, tests, and examples, the lineage store it names in config, and its patches ofRunHelperandRolloutCollectionHelper.Import rewriter: split
fromimports, imports in strings and docs, patch targets, CODEOWNERS and CI paths, idempotence, and the files it must skip (the move list, the frozen docs for released versions,fern/docs.ymlredirects, and paths inside links pinned to a commit). It reports what it leaves for a person to check:import ... as suincluded) or names directly (nemo_gym.server_utils), which reaches only code that still looks the name up there;old_module.Namein code when the file does not import the name's new module above that line, so it may not be loaded there;A split
fromimport that follows other code on its line (if flag: from ..., or after a backslash continuation) stays in that line's statement list, and each line of a split keeps a trailing# noqaor# type: ignore. An import with comments inside its parentheses is left as it is, with a note. The script checks that a rewritten file still parses before writing it, keeps CRLF line endings and a UTF-8 byte order mark, and reports files it skips because they do not parse or are not UTF-8.Each finder property was checked by breaking it: registering the finder after the normal finders, blaming the warning on Gym's import machinery, and running a copy of the module each fail the tests.
Validation
pytest -n 16 tests/unit_tests. The failures are the same onmainat 5911d62 on this machine: the benchmark listing test and the Harbor run summary tests, which fail because this machine's hostname resolves to127.0.1.1, and the Docker PTY tests for a PID file that is not a regular file.pre-commit run --all-files.-W errorfor the moved paths, and NeMo RL's Gym-facing unit tests pass against them.Rollout evidence: N/A. Nothing moves here. The finder matches no path until a move adds an entry.
Compatibility
No user-visible change.
import nemo_gyminstalls the finder, which matches only paths listed inMOVED_MODULES.Issue
Part of #5166.