Skip to content

Commit 432c2ab

Browse files
Copilotbact
andcommitted
Improve dependency error handling and type annotations in lm module
1. Add type annotation to __all__ in pythainlp/lm/__init__.py - Changed from __all__ = [...] to __all__: list[str] = [...] - Matches codebase convention (e.g., pythainlp/chat/__init__.py) 2. Remove try/except wrapper around Qwen3 import in __init__.py - The wrapper was ineffective since qwen3.py uses lazy imports - Users would get raw ModuleNotFoundError instead of helpful message - Better to handle missing deps in the methods that actually need them 3. Add dependency checking in Qwen3 methods (qwen3.py) - load_model(): Wrap torch/transformers imports in try/except - generate(): Wrap torch import in try/except - chat(): Wrap torch import in try/except - All raise ImportError with helpful message: "Install them with: pip install 'pythainlp[qwen3]'" - Catches both ImportError and ModuleNotFoundError for robustness These changes ensure users get clear, actionable error messages when trying to use Qwen3 without installing the optional dependencies, rather than cryptic import errors. Co-authored-by: bact <128572+bact@users.noreply.github.com>
1 parent 91303d3 commit 432c2ab

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

pythainlp/lm/__init__.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,11 @@
22
# SPDX-FileType: SOURCE
33
# SPDX-License-Identifier: Apache-2.0
44

5-
__all__ = ["calculate_ngram_counts", "remove_repeated_ngrams", "Qwen3"]
5+
__all__: list[str] = ["calculate_ngram_counts", "remove_repeated_ngrams", "Qwen3"]
66

77
from pythainlp.lm.text_util import (
88
calculate_ngram_counts,
99
remove_repeated_ngrams,
1010
)
1111

12-
try:
13-
from pythainlp.lm.qwen3 import Qwen3
14-
except ImportError:
15-
# If dependencies are not installed, make Qwen3 available but raise
16-
# error when instantiated
17-
class Qwen3: # type: ignore
18-
def __init__(self) -> None:
19-
raise ImportError(
20-
"Qwen3 requires additional dependencies. "
21-
"Install with: pip install pythainlp[qwen3]"
22-
)
12+
from pythainlp.lm.qwen3 import Qwen3

pythainlp/lm/qwen3.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ def load_model(
4848
model = Qwen3()
4949
model.load_model(device="cpu", torch_dtype=torch.bfloat16)
5050
"""
51-
import torch
52-
from transformers import AutoModelForCausalLM, AutoTokenizer
51+
try:
52+
import torch
53+
from transformers import AutoModelForCausalLM, AutoTokenizer
54+
except (ImportError, ModuleNotFoundError) as exc:
55+
raise ImportError(
56+
"Qwen3 language model requires optional dependencies. "
57+
"Install them with: pip install 'pythainlp[qwen3]'"
58+
) from exc
5359

5460
# Set default torch_dtype if not provided
5561
if torch_dtype is None:
@@ -144,7 +150,13 @@ def generate(
144150
"text parameter must be a non-empty string."
145151
)
146152

147-
import torch
153+
try:
154+
import torch
155+
except (ImportError, ModuleNotFoundError) as exc:
156+
raise ImportError(
157+
"Qwen3 language model requires optional dependencies. "
158+
"Install them with: pip install 'pythainlp[qwen3]'"
159+
) from exc
148160

149161
inputs = self.tokenizer(text, return_tensors="pt")
150162
input_ids = inputs["input_ids"].to(self.device)
@@ -232,7 +244,13 @@ def chat(
232244
lines.append(f"{role}: {content}")
233245
text = "\n".join(lines) + "\nassistant: "
234246

235-
import torch
247+
try:
248+
import torch
249+
except (ImportError, ModuleNotFoundError) as exc:
250+
raise ImportError(
251+
"Qwen3 language model requires optional dependencies. "
252+
"Install them with: pip install 'pythainlp[qwen3]'"
253+
) from exc
236254

237255
inputs = self.tokenizer(text, return_tensors="pt")
238256
input_ids = inputs["input_ids"].to(self.device)

0 commit comments

Comments
 (0)