Commit 432c2ab
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
| |||
144 | 150 | | |
145 | 151 | | |
146 | 152 | | |
147 | | - | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
148 | 160 | | |
149 | 161 | | |
150 | 162 | | |
| |||
232 | 244 | | |
233 | 245 | | |
234 | 246 | | |
235 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
236 | 254 | | |
237 | 255 | | |
238 | 256 | | |
| |||
0 commit comments