Repository navigation
fix(nlp): remove runtime pip install from Scorer and fix missing-module shadowing - #1756
Conversation
|
Thanks for splitting #1750. Why this one has |
Test CoverageTotal: 88.39% (58561 / 66254 lines)
|
|
/review |
| if len(missing) > 0 and not self._install_all(missing): | ||
| logger.warning(f'method {meth} is not available: ' + | ||
| f'missing {missing}') | ||
| meth_missing: tuple[str, ...] = meth.missing_modules() |
There was a problem hiding this comment.
[low] Annotation says tuple but missing_modules returns a list
meth_missing: tuple[str, ...] = meth.missing_modules() is annotated as a tuple, but ScoreMethod.missing_modules (pyhealth/nlp/metrics.py:287-298) builds and returns a List[str]. The code works because extend accepts either type, but the annotation is wrong, and the PR description says the annotations were updated for list/tuple. Fix: annotate it as list[str], or change the base method to return a tuple, as its own annotation claims.
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: human review
Thank you for this focused fix. Removing the runtime pip install from Scorer, fixing the missing/meth_missing shadowing crash, and declaring the nlp extra for the Pixi environment all make the NLP scorer safer and more predictable.
The change looks ready to merge. It does what the description says and nothing unrelated. The new test covers the missing-module path: the method is removed, a warning is logged, the tuple is returned, and no os.system or subprocess call happens. Coverage meets the bar. pyhealth/nlp/metrics.py is at 84.17% (bar: 80%) and the whole repository is at 88.39% (bar: 60%). The change adds no new links between components and no new dataset.
Optional suggestions:
- The
osandsysimports at the top ofmetrics.pymay now be unused, so you could remove them if nothing else needs them. missing_modules()may return a list in some implementations, while the annotation saystuple. Consider making the annotation and return type consistent.- If a method is named explicitly in
ScoreContextafter it has been removed, the error says "No scoring method" and doesn't mention the missing dependency. This was already the behavior before your change, so it isn't required here.
Reviewed commit fdadb7c
solarsys
left a comment
There was a problem hiding this comment.
Approved by a maintainer. The AI review suggests approve on the current commit, and the build passes.
Summary
Remove runtime
os.system("... -m pip install ...")execution frompyhealth.nlp.metrics.Scorer, fix a variable-shadowing crash inScorer._get_missing_modules(), and declareextras = ["nlp"]for the Pixinlptest environment inpyproject.toml.(Split out from #1750 per maintainer feedback.)
Changes
pyhealth/nlp/metrics.py:Scorer._install()andScorer._install_all()so scoring never shells out or mutates the user's Python environment at runtime.Scorer._get_missing_modules()(all_missingvsmeth_missing), which previously raisedAttributeError: 'tuple' object has no attribute 'extend'when a dependency was missing.pip install pyhealth[nlp], updated type annotations (list/tuple), and added aScorerdocstring example.pyproject.toml: addedextras = ["nlp"]to[tool.pixi.feature.nlp.pypi-dependencies]so CI'sRun NLP testsinstallsrapidfuzz,nltk, androuge_scoreexplicitly instead of relying on runtimepip install.tests/core/test_nlp_scorer_missing_modules.py: unit test verifying unavailable methods are removed, a warning is logged, the missing-module tuple is returned withoutAttributeError, and noos.systemorsubprocesscall occurs at runtime.Verification
python3 tools/check_pr_rules.py— all PR contribution rules passed.pytest tests/core/test_nlp_scorer_missing_modules.py— passed.