Skip to content

chore(lint): modernize typing (PEP 604/585) and enable UP032#1537

Merged
shengliangxu merged 4 commits into
mainfrom
shengliangx/modernize-typing
May 26, 2026
Merged

chore(lint): modernize typing (PEP 604/585) and enable UP032#1537
shengliangxu merged 4 commits into
mainfrom
shengliangx/modernize-typing

Conversation

@shengliangxu
Copy link
Copy Markdown
Collaborator

@shengliangxu shengliangxu commented May 22, 2026

What does this PR do?

Type of change: chore / refactor (no behavior change)

Two small lint-cleanup commits:

1. chore(typing): modernize Union/Optional/List to PEP 604 / 585 syntax (8 files)

  • Replace X = Union[A, B] # noqa: UP007 with X: TypeAlias = A | B for the six module-level type aliases (ModelLike, Criterion, NodeTarget, CalibrationDataType, Hparam.Importance / ActiveSlice). The TypeAlias annotation is required so mypy continues to treat them as aliases under PEP 604.
  • Modernize forward-ref unions in modelopt/onnx/quantization/autotune/ to full-string forward refs (e.g. "RegionPattern | None").
  • Update docstring type tags in examples/puzzletron/evaluation/hf_deployable_anymodel.py.

2. chore(lint): remove UP032 ignore and convert .format() to f-strings (10 files)

  • Drop UP032 from extend-ignore in pyproject.toml.
  • Auto-convert 19 "...".format(...) calls to f-strings across export plugins, examples, tests, and tools. One conversion in modelopt/torch/utils/plugins/megatron_generate.py was wrapped manually to stay under the 100-char limit.

Intentionally left as-is:

  • tools/launcher/slurm_config.py keeps its # ruff: noqa: UP045 — nemo_run's CLI parser can't introspect PEP 604 optional annotations.
  • modelopt/torch/puzzletron/* is not touched. The subtree disables ruff's UP family entirely (per-file-ignore "UP") while migration is in progress, and converting Optional[X] to X | None there would silently break runtime introspection in block_config._get_dataclass_type that uses get_origin(tp) is typing.Union (PEP 604 unions return types.UnionType from get_origin, not typing.Union). Best revisited when puzzletron's lint carve-out is narrowed.
  • UP038 (isinstance(x, (int, float))isinstance(x, int | float)) — ruff has officially deprecated this rule; PEP 604 in isinstance is slightly slower and misleads readers about PEP 695 / Optional. Ignore kept.

Usage

No user-facing API changes.

Testing

  • Pre-commit hooks (ruff check, ruff format, mypy, bandit, license) pass on both commits.
  • Ruff status against main: 37 unrelated pre-existing findings (W291/W293/E501/RUF005/PLR1704); zero new findings introduced by this PR.

Before your PR is "Ready for review"

  • Is this change backward compatible?: ✅ — Runtime behavior of the six type aliases changes from a typing.Union instance to types.UnionType. Downstream code introspecting via get_origin(...) is typing.Union on these aliases would break, but no in-repo caller does this on them. (The introspection in modelopt/torch/puzzletron/block_config.py operates on user-supplied dataclass field types, none of which are these aliases.)
  • If you copied code from any other sources or added a new PIP dependency, did you follow guidance in CONTRIBUTING.md: N/A
  • Did you write any new necessary tests?: N/A (no behavior change)
  • Did you update Changelog?: ❌ — internal style refactor; happy to add a Misc note if reviewers want one.
  • Did you get Claude approval on this PR?: ❌ — not yet.

Summary by CodeRabbit

  • Style

    • Modernized type annotations across the codebase to use Python 3.10+ union syntax and TypeAlias where appropriate.
    • Standardized string formatting to f-strings, improving clarity of logs, errors, and validation messages.
  • Chores

    • Updated linting configuration to reflect modern typing/style rules.

Review Change Stack

@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented May 22, 2026

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.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: b7124ad6-bec7-4c3a-a888-72b0c4df67c4

📥 Commits

Reviewing files that changed from the base of the PR and between 35e9582 and 7e09d0a.

📒 Files selected for processing (1)
  • pyproject.toml
✅ Files skipped from review due to trivial changes (1)
  • pyproject.toml

📝 Walkthrough

Walkthrough

Modernizes typing to use TypeAlias and | unions, updates docstring type annotations to modern forms, converts .format() usage to f-strings across examples and core modules, and adjusts ruff lint rules.

Changes

Python Syntax Modernization

Layer / File(s) Summary
Type system modernization with TypeAlias and union syntax
modelopt/onnx/quantization/calib_utils.py, modelopt/torch/distill/config.py, modelopt/onnx/quantization/autotune/common.py, modelopt/onnx/quantization/autotune/region_pattern.py, modelopt/torch/opt/hparam.py, modelopt/torch/utils/graph.py, modelopt/torch/utils/network.py
Typing imports are updated to use TypeAlias and aliases are re-declared using `
Docstring type annotation modernization
examples/puzzletron/evaluation/hf_deployable_anymodel.py
Docstrings updated to use modern type syntax: List[...]list[...], Dict[...]dict[...], Optional[X]→`X
String formatting modernization in examples
examples/puzzletron/evaluation/hf_deployable_anymodel.py, examples/speculative_decoding/collect_hidden_states/send_conversations_for_hiddens.py, examples/speculative_decoding/scripts/send_conversation_vllm.py
Error messages, conversation-id defaults, and shard input path construction changed from .format() to f-strings while preserving formatting and logic.
String formatting modernization in core modules
modelopt/torch/export/plugins/mcore_custom.py, modelopt/torch/export/plugins/megatron_importer.py, modelopt/torch/utils/plugins/megatron_generate.py, modelopt/torch/utils/plugins/transformers_dataset.py, tools/launcher/common/query.py
Sharding error messages, verbose import logs, memory status reporting, validation errors, and shard output filenames converted to f-strings without behavior change.
Linter configuration and test updates
pyproject.toml, tests/examples/speculative_decoding/test_eagle.py
Ruff lint rules updated (replace UP032 with UP038) and a test skip message converted to an f-string.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 76.19% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main objective: modernizing type syntax per PEP 604/585 standards and enabling the UP032 linting rule.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns ✅ Passed PR modernizes type annotations and f-strings with zero security anti-patterns detected: no unsafe torch.load, numpy.load, trust_remote_code, eval/exec, nosec comments, or new dependencies.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch shengliangx/modernize-typing

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 22, 2026

PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-05-26 23:47 UTC

- Replace `X = Union[A, B]  # noqa: UP007` with `X: TypeAlias = A | B`
  for the six module-level type aliases (ModelLike, Criterion, NodeTarget,
  CalibrationDataType, Hparam.Importance/ActiveSlice). TypeAlias is needed
  so mypy still treats them as aliases under PEP 604 syntax.
- Modernize forward-ref unions in onnx/quantization/autotune to full-string
  forward refs (e.g. `"RegionPattern | None"`).
- Update docstring type tags in examples.

Intentionally left:
- tools/launcher/slurm_config.py: documented `# ruff: noqa: UP045`
  (nemo_run CLI parser can't introspect PEP 604 optional annotations).
- modelopt/torch/puzzletron/*: skipped. Subtree has its UP lint rules
  disabled (per-file-ignore set to "UP"), and converting Optional[X] to
  X | None there silently breaks runtime introspection that uses
  `get_origin(tp) is Union` (see block_config._get_dataclass_type).
  Will revisit when puzzletron's lint carve-outs are narrowed.

Signed-off-by: Shengliang Xu <shengliangx@nvidia.com>
UP045 already had no violations. UP008 stays scoped to puzzletron
(via the broader "UP" carve-out — puzzletron files were intentionally
not touched in this branch; see the typing-modernization commit
for the rationale).

Signed-off-by: Shengliang Xu <shengliangx@nvidia.com>
@shengliangxu shengliangxu force-pushed the shengliangx/modernize-typing branch from 5961c16 to 397bb62 Compare May 22, 2026 22:57
@shengliangxu shengliangxu changed the title chore(typing): modernize Union/Optional/List to PEP 604 / 585 syntax chore(lint): modernize typing (PEP 604/585) and enable UP032 May 22, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.80%. Comparing base (45893f7) to head (7e09d0a).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1537      +/-   ##
==========================================
- Coverage   76.83%   76.80%   -0.03%     
==========================================
  Files         477      477              
  Lines       51954    51957       +3     
==========================================
- Hits        39917    39904      -13     
- Misses      12037    12053      +16     
Flag Coverage Δ
examples 41.69% <66.66%> (+0.94%) ⬆️
gpu 59.54% <100.00%> (-0.55%) ⬇️
regression 15.24% <57.14%> (+0.11%) ⬆️
unit 52.76% <80.95%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shengliangxu shengliangxu marked this pull request as ready for review May 22, 2026 23:25
@shengliangxu shengliangxu requested review from a team as code owners May 22, 2026 23:25
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Warning

CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.

Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.

👉 Steps to fix this

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@examples/puzzletron/evaluation/hf_deployable_anymodel.py`:
- Line 103: Replace the assert-based validation "assert task in SUPPORTED_TASKS"
with an explicit runtime check that raises a ValueError when the provided task
is not in SUPPORTED_TASKS; locate the validation that uses the task variable
(and SUPPORTED_TASKS) in hf_deployable_anymodel.py and change it to: if task not
in SUPPORTED_TASKS: raise ValueError(f"Unsupported task: {task}") so the check
remains active in optimized runs and clearly signals invalid external input.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: e744cc83-6c5e-4e96-9402-977ebe30e16e

📥 Commits

Reviewing files that changed from the base of the PR and between 16a0130 and 35e9582.

📒 Files selected for processing (17)
  • examples/puzzletron/evaluation/hf_deployable_anymodel.py
  • examples/speculative_decoding/collect_hidden_states/send_conversations_for_hiddens.py
  • examples/speculative_decoding/scripts/send_conversation_vllm.py
  • modelopt/onnx/quantization/autotune/common.py
  • modelopt/onnx/quantization/autotune/region_pattern.py
  • modelopt/onnx/quantization/calib_utils.py
  • modelopt/torch/distill/config.py
  • modelopt/torch/export/plugins/mcore_custom.py
  • modelopt/torch/export/plugins/megatron_importer.py
  • modelopt/torch/opt/hparam.py
  • modelopt/torch/utils/graph.py
  • modelopt/torch/utils/network.py
  • modelopt/torch/utils/plugins/megatron_generate.py
  • modelopt/torch/utils/plugins/transformers_dataset.py
  • pyproject.toml
  • tests/examples/speculative_decoding/test_eagle.py
  • tools/launcher/common/query.py
💤 Files with no reviewable changes (1)
  • pyproject.toml

Comment thread examples/puzzletron/evaluation/hf_deployable_anymodel.py
Copy link
Copy Markdown
Collaborator

@kevalmorabia97 kevalmorabia97 left a comment

Choose a reason for hiding this comment

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

LGTM

@shengliangxu shengliangxu requested a review from meenchen May 26, 2026 20:18
Copy link
Copy Markdown
Collaborator

@cjluo-nv cjluo-nv left a comment

Choose a reason for hiding this comment

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

Bot review — DM the bot to share feedback.

Mechanical lint cleanup: PEP 604/585 modernization + f-string conversion. The complexity gate fired on directory count, but no new subsystem/DSL/loader is introduced — the design-review protocol doesn't apply. Spot-checked the non-trivial conversions: the : TypeAlias annotations are correct (and necessary so mypy keeps treating these as aliases under PEP 604), forward-ref unions are kept as full strings where the referent is TYPE_CHECKING-only, and the f-string conversions preserve formatting specs (including the manual line-wrap in megatron_generate.py). The PR body explicitly calls out the puzzletron carve-out and the get_origin is typing.Union foot-gun, which is the right reasoning. No tests needed for a pure style refactor.

Complex PR: spans 13 directories (≥ 5). Looping in a human for approval.

@shengliangxu shengliangxu merged commit 9d0d978 into main May 26, 2026
49 checks passed
@shengliangxu shengliangxu deleted the shengliangx/modernize-typing branch May 26, 2026 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants