Use pyupgrade and ruff to modernize LMDeploy Python Code#4392
Open
windreamer wants to merge 4 commits intoInternLM:mainfrom
Open
Use pyupgrade and ruff to modernize LMDeploy Python Code#4392windreamer wants to merge 4 commits intoInternLM:mainfrom
windreamer wants to merge 4 commits intoInternLM:mainfrom
Conversation
bfe5556 to
059bdc9
Compare
f9987c2 to
0ac89ac
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces Ruff/pyupgrade-driven modernization across LMDeploy’s Python codebase (type hints, imports, small refactors) and wires Ruff into the project’s linting workflow.
Changes:
- Add Ruff configuration (targeting Python 3.10+) and enable
pyupgrade-related linting viaUPrules. - Replace legacy
typing.*generics/Optional/Unionusage with Python 3.10+ syntax (list[...],dict[...],X | None,collections.abc). - Apply small pyupgrade refactors (e.g., f-strings, comprehensions, simplified decorators / caching helpers) and update pre-commit hooks.
Reviewed changes
Copilot reviewed 272 out of 272 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| pyproject.toml | Add Ruff configuration (line length, target version, lint selects/ignores). |
| .pre-commit-config.yaml | Add ruff-check hook; remove yapf hook. |
| lmdeploy/vl/utils.py | Modernize type hints to `X |
| lmdeploy/vl/time_series_utils.py | Minor file open argument simplification. |
| lmdeploy/vl/model/yi.py | Modernize collection type hints. |
| lmdeploy/vl/model/utils.py | Switch Callable/MutableSequence imports to collections.abc. |
| lmdeploy/vl/model/qwen3.py | Modernize type hints; simplify typing imports. |
| lmdeploy/vl/model/qwen.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/phi3_vision.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/molmo.py | Modernize type hints and isinstance(..., list) checks. |
| lmdeploy/vl/model/mllama.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/minicpmv.py | Modernize type hints; update dict/list annotations. |
| lmdeploy/vl/model/llava_next.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/llava_hf.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/llava.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/llama4.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/internvl_llava.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/internvl3_hf.py | Modernize Optional[...] to `X |
| lmdeploy/vl/model/interns1_pro.py | Modernize type hints and optional parameters. |
| lmdeploy/vl/model/glm4_v.py | Modernize type hints and isinstance(..., list) checks. |
| lmdeploy/vl/model/glm4_1v.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/gemma3_vl.py | Modernize type hints; update kwargs TypedDict fields. |
| lmdeploy/vl/model/deepseek_vl2.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/deepseek.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/cogvlm.py | Modernize type hints to built-in generics. |
| lmdeploy/vl/model/builder.py | Modernize union type for backend config parameter. |
| lmdeploy/version.py | Modernize return type annotation (Tuple -> tuple). |
| lmdeploy/turbomind/tokenizer_info.py | Modernize type hints (lists/unions). |
| lmdeploy/turbomind/deploy/source_model/base.py | Modernize type hints; switch Iterator import to collections.abc. |
| lmdeploy/turbomind/deploy/policy.py | Modernize list return annotation. |
| lmdeploy/turbomind/deploy/loader.py | Modernize type hints and file open call. |
| lmdeploy/turbomind/deploy/config.py | Modernize type hints for dataclass config objects. |
| lmdeploy/turbomind/init.py | Simplify Windows DLL-path bootstrap logic. |
| lmdeploy/serve/proxy/proxy.py | Minor typing + comprehension modernization. |
| lmdeploy/serve/openai/tool_parser/utils.py | Modernize list/tuple return types. |
| lmdeploy/serve/openai/tool_parser/tool_parser.py | Modernize typing; use collections.abc.Sequence. |
| lmdeploy/serve/openai/tool_parser/qwen3_parser.py | Modernize typing; simplify class declaration. |
| lmdeploy/serve/openai/tool_parser/qwen2d5_parser.py | Modernize typing; use collections.abc.Sequence. |
| lmdeploy/serve/openai/tool_parser/llama3_parser.py | Modernize typing; use built-in generics. |
| lmdeploy/serve/openai/tool_parser/internlm2_parser.py | Modernize typing; use collections.abc.Sequence. |
| lmdeploy/serve/openai/reasoning_parser/reasoning_parser.py | Modernize typing; use collections.abc.Sequence. |
| lmdeploy/serve/openai/reasoning_parser/qwen_qwq_reasoning_parser.py | Modernize typing; use collections.abc.Sequence. |
| lmdeploy/serve/openai/reasoning_parser/deepseek_r1_reasoning_parser.py | Modernize typing; use collections.abc.Sequence. |
| lmdeploy/serve/openai/launch_server.py | Modernize typing (list unions, backend config union). |
| lmdeploy/serve/openai/harmony_utils.py | Modernize typing for token lists. |
| lmdeploy/serve/openai/api_server.py | Switch AsyncGenerator import to collections.abc. |
| lmdeploy/serve/managers/session_manager.py | Modernize history type annotation. |
| lmdeploy/pytorch/weight_loader/model_weight_loader.py | Simplify file open args (mode default). |
| lmdeploy/pytorch/utils.py | Modernize typing; use collections.abc.Sequence; optional types to ` |
| lmdeploy/pytorch/transformers/init.py | Simplify @lru_cache() to @lru_cache. |
| lmdeploy/pytorch/tools/utils.py | Modernize list annotation. |
| lmdeploy/pytorch/strategies/dllm/sequence.py | Modernize typing aliases and optional annotations. |
| lmdeploy/pytorch/strategies/dllm/sampling.py | Modernize typing alias to built-in list. |
| lmdeploy/pytorch/strategies/dllm/model_agent.py | Modernize typing aliases and dict annotations. |
| lmdeploy/pytorch/strategies/base/sequence.py | Modernize typing alias and optional migration request. |
| lmdeploy/pytorch/strategies/base/sampling.py | Modernize typing alias to built-in list. |
| lmdeploy/pytorch/strategies/base/model_agent.py | Modernize typing aliases and dict annotations. |
| lmdeploy/pytorch/strategies/ar_spec/sequence.py | Modernize typing aliases and optional migration request. |
| lmdeploy/pytorch/strategies/ar_spec/model_agent.py | Modernize typing aliases and dict annotations. |
| lmdeploy/pytorch/strategies/ar/sequence.py | Modernize typing aliases and dict annotations. |
| lmdeploy/pytorch/strategies/ar/sampling.py | Modernize typing alias to built-in list. |
| lmdeploy/pytorch/strategies/ar/model_inputs.py | Modernize optional annotations to ` |
| lmdeploy/pytorch/strategies/ar/model_agent.py | Modernize typing aliases and dict annotations. |
| lmdeploy/pytorch/spec_decode/reject_sampler.py | Modernize optional tensor typing to `Tensor |
| lmdeploy/pytorch/spec_decode/proposers/eagle3.py | Modernize dict annotation. |
| lmdeploy/pytorch/spec_decode/proposers/deepseek_mtp.py | Modernize dict annotation. |
| lmdeploy/pytorch/spec_decode/proposers/base.py | Modernize optional annotations and list typing. |
| lmdeploy/pytorch/spec_decode/base.py | Modernize dict annotation. |
| lmdeploy/pytorch/ray.py | Modernize dict/list annotations. |
| lmdeploy/pytorch/paging/scheduler.py | Modernize map/list type aliases. |
| lmdeploy/pytorch/paging/eviction_helper/recompute_eviction_helper.py | Modernize list typing. |
| lmdeploy/pytorch/paging/eviction_helper/base_eviction_helper.py | Modernize list typing. |
| lmdeploy/pytorch/paging/block_trie.py | Modernize dict/set annotations. |
| lmdeploy/pytorch/paging/block_manager/base_block_manager.py | Modernize dict annotation. |
| lmdeploy/pytorch/nn/norm.py | Modernize dict optional annotation. |
| lmdeploy/pytorch/nn/moe/w8a8.py | Modernize typing (lists/dicts/optionals). |
| lmdeploy/pytorch/nn/moe/route.py | Modernize tuple return annotation. |
| lmdeploy/pytorch/nn/moe/default.py | Modernize dict checks/types; Callable from collections.abc. |
| lmdeploy/pytorch/nn/moe/blocked_fp8.py | Modernize typing; dict checks/types. |
| lmdeploy/pytorch/nn/moe/init.py | Modernize typing; Callable from collections.abc. |
| lmdeploy/pytorch/nn/linear/base.py | Modernize typing; list generics. |
| lmdeploy/pytorch/multimodal/image_type.py | Modernize meta dict annotation. |
| lmdeploy/pytorch/multimodal/data_type.py | Modernize multimodal type aliases. |
| lmdeploy/pytorch/models/utils/multimodal.py | Modernize prepared input tuple alias. |
| lmdeploy/pytorch/models/utils/model.py | Modernize iterable/tuple typing. |
| lmdeploy/pytorch/models/siglip.py | Modernize iterable/set/tuple typing. |
| lmdeploy/pytorch/models/sdar.py | Modernize optional and iterable/tuple typing. |
| lmdeploy/pytorch/models/qwen3_vl_moe.py | Modernize typing for expert loading helpers. |
| lmdeploy/pytorch/models/qwen3_5_moe.py | Modernize typing for expert loading helpers. |
| lmdeploy/pytorch/models/qwen3.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/qwen2_reward.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/qwen2.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/qwen.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/phi3_v.py | Modernize optional/iterable typing; multimodal preprocess typing. |
| lmdeploy/pytorch/models/phi3.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/patch.py | Modernize dict typing and dict isinstance checks. |
| lmdeploy/pytorch/models/mistral.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/minicpm3.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/llama_eagle.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/internvl_patch.py | Modernize optional tensor typing. |
| lmdeploy/pytorch/models/interns1_pro_ts.py | Modernize optional/tuple typing; small call simplification. |
| lmdeploy/pytorch/models/internlm2_reward.py | Modernize iterable/tuple typing. |
| lmdeploy/pytorch/models/internlm.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/glm4.py | Modernize optional/iterable/tuple typing. |
| lmdeploy/pytorch/models/gemma3_vl.py | Modernize iterable/tuple typing; multimodal preprocess typing. |
| lmdeploy/pytorch/models/deepseek_vl2.py | Modernize optional/iterable/tuple typing; multimodal preprocess typing. |
| lmdeploy/pytorch/models/deepseek_v32.py | Modernize Sequence import and tuple typing. |
| lmdeploy/pytorch/kernels/dlinfer/pagedattention.py | Modernize optional/sequence typing for kernel wrappers. |
| lmdeploy/pytorch/kernels/dlinfer/moe_gating_topk_softmax.py | Modernize tuple return typing. |
| lmdeploy/pytorch/kernels/dlinfer/linear.py | Modernize optional tensor typing. |
| lmdeploy/pytorch/kernels/dlinfer/fill_kv_cache.py | Modernize optional tensor sequence typing. |
| lmdeploy/pytorch/kernels/dlinfer/awq_kernels.py | Modernize optional tensor typing. |
| lmdeploy/pytorch/kernels/dlinfer/apply_rotary_pos_emb.py | Modernize optional/tuple typing. |
| lmdeploy/pytorch/kernels/dispatcher.py | Switch Callable import to collections.abc. |
| lmdeploy/pytorch/kernels/cuda/rms_norm.py | Modernize print formatting to f-strings. |
| lmdeploy/pytorch/kernels/cuda/pagedattention.py | Switch Sequence import to collections.abc. |
| lmdeploy/pytorch/kernels/cuda/gated_delta_rule.py | Switch Sequence import to collections.abc. |
| lmdeploy/pytorch/kernels/cuda/fused_moe_ep.py | Modernize optional list typing. |
| lmdeploy/pytorch/kernels/cuda/fused_moe.py | Minor tensor allocation call simplification. |
| lmdeploy/pytorch/kernels/cuda/flashattention.py | Minor constant expression cleanup. |
| lmdeploy/pytorch/kernels/cuda/fill_kv_cache.py | Modernize optional tensor typing and optional string typing. |
| lmdeploy/pytorch/kernels/cuda/blocked_gemm_fp8.py | Modernize optional string typing. |
| lmdeploy/pytorch/kernels/cuda/blocked_fp8_fused_moe.py | Switch Callable import to collections.abc. |
| lmdeploy/pytorch/envs.py | Modernize union typing for parameter defaults. |
| lmdeploy/pytorch/engine/mp_engine/ray_engine.py | Modernize dict typing. |
| lmdeploy/pytorch/engine/mp_engine/base_worker.py | Modernize optional list typing. |
| lmdeploy/pytorch/engine/mp_engine/base.py | Modernize optional list typing. |
| lmdeploy/pytorch/engine/model_agent/init.py | Modernize adapters dict typing. |
| lmdeploy/pytorch/engine/logits_process.py | Modernize typing of internal dataclasses and lists/dicts. |
| lmdeploy/pytorch/engine/input_process.py | Modernize type aliases and dataclass field typing. |
| lmdeploy/pytorch/engine/guided_process.py | Modernize typing; dict checks/types. |
| lmdeploy/pytorch/engine/executor/uni_executor.py | Modernize adapters and list typing. |
| lmdeploy/pytorch/engine/executor/base_worker.py | Modernize adapters, dict output typing, and optional list typing. |
| lmdeploy/pytorch/engine/executor/base.py | Modernize adapters and optional list typing. |
| lmdeploy/pytorch/engine/executor/init.py | Modernize adapters dict typing. |
| lmdeploy/pytorch/engine/engine_instance.py | Modernize multimodal typing and input id list typing. |
| lmdeploy/pytorch/distributed.py | Modernize optional/list typing throughout dist helpers. |
| lmdeploy/pytorch/disagg/messages.py | Modernize nested list/tuple typing; optional config typing. |
| lmdeploy/pytorch/disagg/conn/proxy_conn.py | Modernize set/dict/tuple typing for connection pool. |
| lmdeploy/pytorch/disagg/conn/protocol.py | Modernize optional/list typing for protocol models. |
| lmdeploy/pytorch/disagg/conn/engine_conn.py | Modernize dict/list typing for sockets/contexts. |
| lmdeploy/pytorch/disagg/config.py | Modernize optional typing. |
| lmdeploy/pytorch/disagg/backend/mooncake.py | Modernize dict typing for endpoint/link bookkeeping. |
| lmdeploy/pytorch/disagg/backend/dlslime.py | Modernize dict typing for endpoints/links. |
| lmdeploy/pytorch/devices/device_manager.py | Switch Callable import to collections.abc. |
| lmdeploy/pytorch/configurations/init.py | Switch to f-string module import format. |
| lmdeploy/pytorch/check_env/base.py | Modernize list typing for checker list. |
| lmdeploy/pytorch/backends/token_dispatcher.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/rotary_embedding.py | Modernize list typing for scaling parameters. |
| lmdeploy/pytorch/backends/qmodules.py | Modernize optional typing for bias/group args. |
| lmdeploy/pytorch/backends/moe_router.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/moe.py | Modernize typing; optional string typing. |
| lmdeploy/pytorch/backends/linear.py | Modernize optional/list typing. |
| lmdeploy/pytorch/backends/graph_runner.py | Modernize list typing for past key values and batch sizes. |
| lmdeploy/pytorch/backends/dlinfer/qmodules.py | Modernize optional typing for bias/group args. |
| lmdeploy/pytorch/backends/dlinfer/op_backend.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/dlinfer/moe.py | Modernize list typing and callable import. |
| lmdeploy/pytorch/backends/dlinfer/maca/op_backend.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/dlinfer/linear.py | Modernize optional/list typing. |
| lmdeploy/pytorch/backends/dlinfer/camb/op_backend.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/dlinfer/awq_modules.py | Modernize optional typing for bias/group args. |
| lmdeploy/pytorch/backends/dlinfer/attention.py | Modernize optional typing; Sequence from collections.abc. |
| lmdeploy/pytorch/backends/dlinfer/ascend/op_backend.py | Modernize dict/tuple typing and file open args. |
| lmdeploy/pytorch/backends/default/token_dispatcher.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/default/op_backend.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/default/moe_router.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/default/linear.py | Modernize optional/list typing. |
| lmdeploy/pytorch/backends/default/awq_modules.py | Modernize optional typing for bias/group args. |
| lmdeploy/pytorch/backends/cuda/qmodules.py | Modernize optional typing for bias/group args. |
| lmdeploy/pytorch/backends/cuda/op_backend.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/cuda/moe_router.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/cuda/moe/w8a8.py | Modernize list typing. |
| lmdeploy/pytorch/backends/cuda/moe/ep_utils.py | Modernize list typing. |
| lmdeploy/pytorch/backends/cuda/moe/default.py | Modernize list typing and optional typing. |
| lmdeploy/pytorch/backends/cuda/moe/blocked_fp8.py | Modernize list typing and callable import. |
| lmdeploy/pytorch/backends/cuda/graph_runner.py | Modernize dict/list typing and tuple typing. |
| lmdeploy/pytorch/backends/cuda/blockedf8_modules.py | Modernize optional/list typing. |
| lmdeploy/pytorch/backends/cuda/awq_modules.py | Modernize optional typing for bias/group args. |
| lmdeploy/pytorch/backends/cuda/attention/mla.py | Replace lru_cache(maxsize=None) with functools.cache. |
| lmdeploy/pytorch/backends/blockedf8_modules.py | Modernize optional/list typing. |
| lmdeploy/pytorch/backends/base.py | Modernize tuple return typing. |
| lmdeploy/pytorch/backends/awq_modules.py | Modernize optional typing for bias/group args. |
| lmdeploy/pytorch/adapter/adapter.py | Modernize dict/list typing; iterable typing. |
| lmdeploy/profiler.py | Modernize list typing for collected metrics. |
| lmdeploy/metrics/stats.py | Modernize list typing and optional float typing. |
| lmdeploy/metrics/metrics_processor.py | Simplify class declaration syntax. |
| lmdeploy/metrics/loggers.py | Modernize list typing for bucket builders. |
| lmdeploy/logger.py | Modernize optional/list typing for request logging. |
| lmdeploy/lite/utils/memory_efficient.py | Modernize list typing in helper functions. |
| lmdeploy/lite/utils/global_avail.py | Modernize nested dict typing and unions. |
| lmdeploy/lite/utils/collect.py | Modernize dict/list/tuple typing throughout helpers. |
| lmdeploy/lite/utils/cal_qparams.py | Modernize optional typing in NamedTuple. |
| lmdeploy/lite/utils/batch_split.py | Modernize unions/tuples and Tuple -> tuple checks. |
| lmdeploy/lite/quantization/weight/quantizer.py | Modernize typing; nested dict typing for function maps. |
| lmdeploy/lite/quantization/weight/quant_utils.py | Modernize optional/union typing for helpers. |
| lmdeploy/lite/quantization/modules/linear.py | Modernize typing; update classmethod cls annotation. |
| lmdeploy/lite/quantization/calibration.py | Simplify class declarations; modernize union typing. |
| lmdeploy/lite/quantization/awq.py | Modernize list typing for smoothing helpers. |
| lmdeploy/lite/apis/get_small_sharded_hf.py | Replace .format(...) with f-string. |
| lmdeploy/lite/apis/calibrate.py | Modernize union typing; minor set construction simplification. |
| lmdeploy/cli/utils.py | Modernize list typing for CLI utilities. |
| lmdeploy/cli/lite.py | Simplify class declaration syntax. |
| lmdeploy/cli/cli.py | Simplify subprocess call using capture_output=True. |
| lmdeploy/archs.py | Modernize tuple return typing; simplify set literal; fix type comparison to is. |
| lmdeploy/api.py | Modernize typing by removing string-literal types where possible; update list typing. |
Comments suppressed due to low confidence (1)
lmdeploy/lite/quantization/modules/linear.py:36
- The
biasparameter is used as a boolean flag (if bias:and set toTrue/False), but it is annotated astorch.Tensor | Noneand defaults toTrue. This annotation is incorrect and makes the API misleading for callers/type checkers. Consider changing the type tobool(and updating the docstring accordingly).
self,
in_features: int,
out_features: int,
bias: torch.Tensor | None = True,
w_bit: int = 4,
symmetry: bool = False,
group_size: int = 128,
) -> None:
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
45304a8 to
fd43bc0
Compare
fd43bc0 to
7db6ff2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.