refactor: modernize type hints to Python 3.10+ syntax (src/)#2438
refactor: modernize type hints to Python 3.10+ syntax (src/)#2438NJX-njx wants to merge 1 commit intovllm-project:mainfrom
Conversation
Ref vllm-project#1927 Apply Python 3.10+ type hint modernization across 58 source files using pyupgrade --py310-plus: - Replace Union[X, Y] with X | Y (PEP 604) - Replace Optional[X] with X | None (PEP 604) - Replace List[X] with list[X] (PEP 585) - Replace Dict[K, V] with dict[K, V] (PEP 585) - Replace Tuple[X, ...] with tuple[X, ...] (PEP 585) - Replace Set[X] with set[X] (PEP 585) - Replace Type[X] with type[X] (PEP 585) - Remove now-unused typing imports This is the first batch targeting the src/llmcompressor/ directory. Tests directory will follow in a separate PR.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review. Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request systematically modernizes the Python type hints within the core Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request modernizes Python type hints across numerous files in the src/ directory to align with Python 3.10+ syntax. The changes, primarily automated using pyupgrade, include adopting the | operator for unions, using built-in generic types (e.g., list instead of List), and updating imports from typing to collections.abc. Additionally, some minor stylistic improvements such as using f-strings and removing redundant parentheses have been applied. The changes are correct, improve code readability and maintainability, and have no impact on runtime behavior. The codebase is now more aligned with modern Python standards.
There was a problem hiding this comment.
Pull request overview
This PR modernizes type annotations across src/llmcompressor/ to Python 3.10+ syntax (PEP 585/604) and adjusts related imports/usages to match the updated typing style.
Changes:
- Replaced
typing.Union/Optional/List/Dict/Tupleforms with| Noneand built-in generics (e.g.,list[...],dict[...],tuple[...]). - Moved several typing protocols/ABCs (e.g.,
Callable,Iterator,Generator,Iterable,Mapping) tocollections.abc. - Small syntactic cleanups (set comprehensions, f-strings, default read-mode
open()).
Reviewed changes
Copilot reviewed 58 out of 58 changed files in this pull request and generated 47 comments.
Show a summary per file
| File | Description |
|---|---|
| src/llmcompressor/utils/transformers.py | Minor set-construction simplification. |
| src/llmcompressor/utils/pytorch/module.py | Updated function annotations to 3.10+ typing syntax. |
| src/llmcompressor/utils/metric_logging.py | Switched List[...] annotations to list[...]. |
| src/llmcompressor/utils/dist.py | Moved Callable/Hashable to collections.abc and updated return typing. |
| src/llmcompressor/utils/dev.py | Updated Type[...] to type[...] for model class parameter. |
| src/llmcompressor/typing.py | Moved Iterable to collections.abc. |
| src/llmcompressor/transformers/utils/helpers.py | Minor logging formatting cleanup. |
| src/llmcompressor/transformers/tracing/debug.py | Updated Type[...]/Tuple[...] to type[...]/tuple[...]. |
| src/llmcompressor/transformers/data/base.py | Moved Callable to collections.abc and minor set usage cleanup. |
| src/llmcompressor/recipe/utils.py | Updated dict/list typing + improved error string formatting. |
| src/llmcompressor/recipe/recipe.py | Updated model fields and method annotations to built-in generics / ` |
| src/llmcompressor/pytorch/utils/sparsification_info/module_sparsification_info.py | Moved Generator to collections.abc and updated tuple typing. |
| src/llmcompressor/pytorch/utils/sparsification_info/helpers.py | Updated list/optional typing and error formatting. |
| src/llmcompressor/pytorch/utils/sparsification_info/configs.py | Moved Generator to collections.abc and modernized nested union/dict typing. |
| src/llmcompressor/pytorch/utils/sparsification.py | Updated Optional[Dict[...]] to `dict[...] |
| src/llmcompressor/pytorch/model_load/helpers.py | Updated union/optional typing to PEP 604 style. |
| src/llmcompressor/pipelines/sequential/pipeline.py | Moved Iterator to collections.abc. |
| src/llmcompressor/pipelines/sequential/helpers.py | Moved Callable to collections.abc and modernized typing in several helpers. |
| src/llmcompressor/pipelines/data_free/pipeline.py | Updated optional dataloader typing to `DataLoader |
| src/llmcompressor/pipelines/cache.py | Moved Generator to collections.abc, modernized annotations, and minor defaultdict simplification. |
| src/llmcompressor/observers/mse.py | Updated optional tensor typing to `torch.Tensor |
| src/llmcompressor/observers/moving_base.py | Updated optional module typing to `torch.nn.Module |
| src/llmcompressor/observers/helpers.py | Updated optional tensor typing to `torch.Tensor |
| src/llmcompressor/observers/base.py | Converted tuple aliases and optional parameter typing to 3.10+ syntax. |
| src/llmcompressor/modifiers/utils/hooks.py | Moved Callable to collections.abc. |
| src/llmcompressor/modifiers/transform/spinquant/norm_mappings.py | Updated list/dict typing to built-in generics. |
| src/llmcompressor/modifiers/transform/spinquant/mappings.py | Updated optional/list/dict typing to 3.10+ syntax. |
| src/llmcompressor/modifiers/transform/spinquant/base.py | Moved Iterable to collections.abc and modernized multiple field annotations. |
| src/llmcompressor/modifiers/transform/smoothquant/base.py | Moved Callable to collections.abc and minor set construction simplification. |
| src/llmcompressor/modifiers/transform/quip/base.py | Modernized targets/ignore/etc typing to PEP 604/585 style. |
| src/llmcompressor/modifiers/quantization/quantization/mixin.py | Modernized config/observer typing and internal attributes to built-in generics. |
| src/llmcompressor/modifiers/quantization/calibration.py | Updated optional tensor typing to `torch.Tensor |
| src/llmcompressor/modifiers/pruning/wanda/wanda_sparsify.py | Updated device/dict typing to ` |
| src/llmcompressor/modifiers/pruning/sparsegpt/sgpt_sparsify.py | Updated device/dict/tuple typing to built-in generics. |
| src/llmcompressor/modifiers/pruning/sparsegpt/sgpt_base.py | Minor set construction simplification for hooks. |
| src/llmcompressor/modifiers/pruning/magnitude/base.py | Modernized targets/scheduler types to built-in generics / ` |
| src/llmcompressor/modifiers/pruning/helpers.py | Moved Callable to collections.abc and modernized dict typing. |
| src/llmcompressor/modifiers/pruning/constant/base.py | Modernized targets and parameterized layer typing. |
| src/llmcompressor/modifiers/gptq/base.py | Modernized multiple modifier field annotations and internal dict/tuple typing. |
| src/llmcompressor/modifiers/awq/base.py | Moved Iterator to collections.abc and modernized set construction. |
| src/llmcompressor/modifiers/autoround/base.py | Modernized typing in modifier fields and internal structures. |
| src/llmcompressor/modeling/qwen3_moe.py | Removed encoding header comment. |
| src/llmcompressor/modeling/llama4.py | Updated return tuple typing to built-in tuple[...]. |
| src/llmcompressor/modeling/gpt_oss.py | Modernized optional/list typing in MoE utilities. |
| src/llmcompressor/modeling/fuse.py | Moved Iterable to collections.abc. |
| src/llmcompressor/logger.py | Modernized config typing and dict typing in support_log_once. |
| src/llmcompressor/entrypoints/oneshot.py | Moved Callable to collections.abc. |
| src/llmcompressor/entrypoints/model_free/validate.py | Simplified open(..., 'r') to default read mode. |
| src/llmcompressor/entrypoints/model_free/save_utils.py | Simplified open(..., 'r') to default read mode. |
| src/llmcompressor/entrypoints/model_free/reindex_fused_weights.py | Simplified open(..., 'r') to default read mode. |
| src/llmcompressor/entrypoints/model_free/process.py | Moved Iterable to collections.abc. |
| src/llmcompressor/entrypoints/model_free/helpers.py | Moved Mapping to collections.abc and simplified generator expr parens. |
| src/llmcompressor/entrypoints/model_free/init.py | Moved Iterable to collections.abc and modernized optional device typing. |
| src/llmcompressor/datasets/utils.py | Moved Callable to collections.abc and modernized optional int typing. |
| src/llmcompressor/core/session_functions.py | Moved Generator to collections.abc and modernized optional event payload typing. |
| src/llmcompressor/core/session.py | Moved Callable to collections.abc. |
| src/llmcompressor/core/events/event.py | Modernized multiple optional int/float typing. |
| src/llmcompressor/args/dataset_arguments.py | Moved Callable to collections.abc. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| from concurrent.futures import ThreadPoolExecutor, as_completed | ||
| from pathlib import Path | ||
| from typing import Iterable, Optional | ||
| from typing import Optional |
There was a problem hiding this comment.
This import block will likely fail ruff check:
Optionalis unused after switching totorch.device | str | None(F401).- isort typically expects
from collections.abc import Iterableto be sorted with the other stdlib imports and usually beforetyping.
Please remove the unused import and letruff check --fixnormalize ordering.
| from typing import Optional |
| from typing import Any | ||
|
|
||
| from collections.abc import Callable | ||
|
|
There was a problem hiding this comment.
Import order likely violates ruff/isort: from collections.abc import Callable should generally be sorted before from typing import Any within the stdlib section. Running ruff check --fix should reorder this block.
| from typing import Any | |
| from collections.abc import Callable | |
| from collections.abc import Callable | |
| from typing import Any |
| from typing import Any | ||
|
|
||
| from collections.abc import Callable | ||
|
|
There was a problem hiding this comment.
Import order likely violates ruff/isort: from collections.abc import Callable should be sorted with the other stdlib imports and typically come before from typing import Any. Running ruff check --fix should normalize this ordering.
| def dispatch_for_sequential( | ||
| model: PreTrainedModel, | ||
| onload_device: Optional[torch.device | str] = None, | ||
| offload_device: Optional[torch.device | str] = None, | ||
| onload_device: torch.device | str | None = None, | ||
| offload_device: torch.device | str | None = None, |
There was a problem hiding this comment.
After changing onload_device/offload_device to torch.device | str | None, the Optional import (from typing import ... Optional) in this module appears unused and will fail ruff check (F401). Please remove the unused Optional import.
| def make_empty_row_scalars( | ||
| module: torch.nn.Module, device: Optional[torch.device] = None | ||
| module: torch.nn.Module, device: torch.device | None = None | ||
| ) -> torch.Tensor: |
There was a problem hiding this comment.
from typing import Dict, Optional is now unused after migrating annotations to dict[...] and torch.device | None, and will fail ruff check with F401. Please remove the unused typing imports.
| def expand_special_targets(targets: str | list[str]) -> list[str]: | ||
| """ |
There was a problem hiding this comment.
from typing import Dict, List, Union appears unused after converting annotations to list[...]/dict[...] and | unions, and will fail ruff check with F401. Please remove the unused typing imports (or run ruff check --fix).
| type_: EventType | None = None | ||
| steps_per_epoch: int | None = None | ||
| batches_per_step: int | None = None | ||
| invocations_per_step: int = 1 |
There was a problem hiding this comment.
from typing import Optional appears unused after converting annotations to X | None (it’s only referenced in docstrings now). This will fail ruff check with F401; please remove the unused Optional import.
| config_groups: dict[str, QuantizationScheme] | None = None | ||
| # NOTE: targets is not the sole source of truth for finding all matching target | ||
| # layers in a model. Additional information can be stored in `config_groups` | ||
| # Use self.resolved_targets as source of truth. | ||
| targets: Union[str, List[str]] = Field(default_factory=lambda: ["Linear"]) | ||
| ignore: List[str] = Field(default_factory=list) | ||
| scheme: Optional[Union[str, Dict[str, Any]]] = None | ||
| kv_cache_scheme: Optional[QuantizationArgs] = None | ||
| targets: str | list[str] = Field(default_factory=lambda: ["Linear"]) | ||
| ignore: list[str] = Field(default_factory=list) | ||
| scheme: str | dict[str, Any] | None = None | ||
| kv_cache_scheme: QuantizationArgs | None = None | ||
| # Observer parameters for easy specification | ||
| weight_observer: Optional[str] = None | ||
| input_observer: Optional[str] = None | ||
| output_observer: Optional[str] = None | ||
| observer: Optional[Dict[str, str]] = None | ||
| weight_observer: str | None = None | ||
| input_observer: str | None = None | ||
| output_observer: str | None = None | ||
| observer: dict[str, str] | None = None |
There was a problem hiding this comment.
After switching these annotations to built-in generics and | None, the from typing import Any, Dict, List, Optional, Set, Union import looks largely unused (Dict, List, Optional, Set, Union no longer appear in type positions). This will fail ruff check with F401; please trim the typing import to only what’s still used (likely just Any).
| sequential_targets: str | list[str] | None = None | ||
| # AutoRound modifier arguments | ||
| iters: int = 200 | ||
| enable_torch_compile: bool = True | ||
| batch_size: int = 8 | ||
| lr: Optional[float] = None | ||
| device_ids: Optional[str] = None | ||
| lr: float | None = None | ||
| device_ids: str | None = None | ||
|
|
||
| # private variables | ||
| _all_module_input: Dict[str, List[Tuple]] = PrivateAttr(default_factory=dict) | ||
| _q_input: Optional[torch.Tensor] = PrivateAttr(default=None) | ||
| _all_module_input: dict[str, list[tuple]] = PrivateAttr(default_factory=dict) | ||
| _q_input: torch.Tensor | None = PrivateAttr(default=None) |
There was a problem hiding this comment.
from typing import Dict, List, Optional, Tuple, Union appears unused after updating this module’s annotations to built-in generics and | None. This will fail ruff check with F401; please remove the unused typing imports (or run ruff check --fix).
| args: dict[str, Any] = Field(default_factory=dict) | ||
| stage: str = "default" | ||
| modifiers: List[Modifier] = Field(default_factory=list) | ||
| modifiers: list[Modifier] = Field(default_factory=list) | ||
|
|
There was a problem hiding this comment.
The top-level from typing import Any, Dict, List, Optional, Union import now has unused names (Dict, List, Optional) after converting to built-in generics and | None. This will raise ruff F401; please trim the import to only the names still referenced (likely Any and Union).
Summary
Ref #1927
Modernizes type annotations across 58 source files in
src/llmcompressor/to use Python 3.10+ syntax, as proposed in the tracking issue.Changes (automated via
pyupgrade --py310-plus)Union[X, Y]X | YOptional[X]X | NoneList[X]list[X]Dict[K, V]dict[K, V]Tuple[X, ...]tuple[X, ...]Set[X]set[X]Type[X]type[X]Unused
from typing import ...entries are also removed where applicable.Scope
This PR covers the
src/llmcompressor/directory only. Thetests/directory can follow in a separate PR to keep review manageable.Notes
pyupgrade --py310-plusand manually reviewed