Skip to content

fix(nlp): remove runtime pip install from Scorer and fix missing-module shadowing - #1756

Merged
solarsys merged 1 commit into
sunlabuiuc:masterfrom
irvifa:fix/nlp-remove-runtime-pip
Dec 1, 2026
Merged

solarsys merged 1 commit into
sunlabuiuc:masterfrom
irvifa:fix/nlp-remove-runtime-pip

Conversation

@irvifa

@irvifa irvifa commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Remove runtime os.system("... -m pip install ...") execution from pyhealth.nlp.metrics.Scorer, fix a variable-shadowing crash in Scorer._get_missing_modules(), and declare extras = ["nlp"] for the Pixi nlp test environment in pyproject.toml.

(Split out from #1750 per maintainer feedback.)

Changes

  • pyhealth/nlp/metrics.py:
    • Removed Scorer._install() and Scorer._install_all() so scoring never shells out or mutates the user's Python environment at runtime.
    • Fixed variable shadowing in Scorer._get_missing_modules() (all_missing vs meth_missing), which previously raised AttributeError: 'tuple' object has no attribute 'extend' when a dependency was missing.
    • Logged a clear warning directing users to pip install pyhealth[nlp], updated type annotations (list/tuple), and added a Scorer docstring example.
  • pyproject.toml: added extras = ["nlp"] to [tool.pixi.feature.nlp.pypi-dependencies] so CI's Run NLP tests installs rapidfuzz, nltk, and rouge_score explicitly instead of relying on runtime pip 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 without AttributeError, and no os.system or subprocess call 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.

@solarsys

Copy link
Copy Markdown
Collaborator

Thanks for splitting #1750. Why this one has status: manual: it changes pyproject.toml, adding the nlp extra to the pixi dev environment so CI installs what Scorer needs. Files outside docs/ examples/ pyhealth/ test-resources/ tests/ need a maintainer. The code change itself is a clear improvement: no more os.system pip install at runtime, and there are tests. It's flagged in #1668.

@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 88.39% (58561 / 66254 lines)

Modified file Coverage Lines
pyhealth/nlp/metrics.py 84.17% 218 / 259

Workflow run · fdadb7c

@solarsys

Copy link
Copy Markdown
Collaborator

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit fdadb7c

Comment thread pyhealth/nlp/metrics.py
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 os and sys imports at the top of metrics.py may 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 says tuple. Consider making the annotation and return type consistent.
  • If a method is named explicitly in ScoreContext after 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 solarsys left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved by a maintainer. The AI review suggests approve on the current commit, and the build passes.

@solarsys
solarsys merged commit b452e76 into sunlabuiuc:master Dec 1, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: manual Pending maintainer's review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants