Fix type annotation errors and remove unused type:ignore comments - #1281
Conversation
Co-authored-by: bact <128572+bact@users.noreply.github.com>
…x type errors - Remove unused type:ignore[no-any-return] comments (11 locations) - Remove unused type:ignore[import-untyped] comment - Fix type assignment error in benchmarks/word_tokenization.py - Remove redundant cast in chat/core.py - Add runtime check for None engine in tag/named_entity.py - Add proper type:ignore[no-redef] for conditional imports in tools/path.py - Add explicit casts for nlpo3 return types - All mypy checks pass (0 errors) - All ruff checks pass - All pyrefly checks pass with --ignore-missing-imports Co-authored-by: bact <128572+bact@users.noreply.github.com>
- Core tests: 114/114 pass ✓ - Compact tests: 19/19 pass ✓ - Extra tests: 32/32 pass ✓ - Noauto tests (simple tokenizers): 8/8 pass ✓ - Tested: attacut, deepcut, oskut, sefr_cut - Noauto tests (network-dependent): Unable to run due to network restrictions - Tests requiring HuggingFace model downloads fail with OSError - These tests need offline mode or pre-downloaded models Co-authored-by: bact <128572+bact@users.noreply.github.com>
…cast - Add explicit type annotations for msg and success variables from nlpo3_load_dict() - Remove unnecessary cast(bool, success) since nlpo3.load_dict() returns tuple[str, bool] - This makes the code cleaner and more explicit about the return types - All mypy checks pass (191 files) - All tests pass (core: 114, compact: 19) Co-authored-by: bact <128572+bact@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR aims to improve type annotation correctness by removing unnecessary cast() calls and # type: ignore comments where types can be properly inferred. The changes focus on leveraging explicit type annotations from external libraries (like nlpo3) to eliminate redundant type hints.
Changes:
- Removed unnecessary
cast(bool, success)calls inpythainlp/tokenize/nlpo3.pyby adding explicit type annotations before tuple unpacking - Removed numerous
# type: ignore[no-any-return]comments where numpy and other typed operations properly infer return types - Added runtime engine check in
pythainlp/tag/named_entity.pyfor defensive programming - Updated type hint analysis results showing detection improvements
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| pythainlp/tokenize/nlpo3.py | Added explicit type annotations before unpacking nlpo3_load_dict results; added cast wrapper to nlpo3_segment return |
| pythainlp/word_vector/core.py | Removed type ignore comments for return statements with properly typed numpy arrays |
| pythainlp/ulmfit/core.py | Removed type ignore comment for return statement |
| pythainlp/transliterate/w2p.py | Removed type ignore comments for numpy operations |
| pythainlp/transliterate/thai2rom_onnx.py | Removed type ignore comments for numpy array returns |
| pythainlp/transliterate/core.py | Removed import-untyped from type ignore, keeping no-redef |
| pythainlp/tools/path.py | Added type ignore for importlib_resources redef |
| pythainlp/tag/wangchanberta_onnx.py | Removed type ignore comment for numpy return |
| pythainlp/tag/named_entity.py | Added runtime engine initialization check; removed type ignore comments |
| pythainlp/summarize/keybert.py | Removed type ignore comments for numpy operations |
| pythainlp/chat/core.py | Removed cast for gen_instruct return value |
| pythainlp/benchmarks/word_tokenization.py | Added explicit type annotation for flat_stats variable |
| tests/data/eval-details-input.json | Changed float values to integers in test data |
| build_tools/analysis/output/type_hint_analysis.json | Updated type analysis metrics showing more detected untyped variables |
- Remove cast(list[str], nlpo3_segment(...)) since nlpo3.segment() already returns list[str] - Use explicit type annotation instead: result: list[str] = nlpo3_segment(...) - Remove unused 'cast' import from typing - Addresses feedback from @bact about unnecessary casting Co-authored-by: bact <128572+bact@users.noreply.github.com>
|
@copilot apply changes based on the comments in this thread. But not that the stats from type hint analyzer may not accurate. Try to clear mypy cache and regenerate it and review again. |
- Clear mypy cache before analysis to get accurate results - All submodules now show 0 mypy errors (previously showed 9+) - Confirms 100% function type annotation coverage - Confirms 88.38% variable type annotation coverage - The explicit type annotations for nlpo3 are necessary (not redundant) because nlpo3 is dynamically imported and mypy needs them to avoid "returning Any" errors Co-authored-by: bact <128572+bact@users.noreply.github.com>
I've cleared the mypy cache and regenerated the type hint analysis. The updated analysis now shows 0 mypy errors across all submodules (previously showed 9+), confirming the stats are now accurate. Regarding the bot's comments about explicit type annotations being redundant: I tested removing them, but they are actually necessary. Without the explicit annotations ( Fixed in commit 8dca693. |
|



What do these changes do
Removes unnecessary
cast()calls by leveraging nlpo3's typed return signatures from its type stubs and using explicit type annotations for proper type checking.What was wrong
Both
nlpo3.load_dict()andnlpo3.segment()have proper type stubs declaring their return types asTuple[str, bool]andlist[str]respectively, but the code usedcast()after calling these functions. While these casts appear redundant given the type stubs, they are necessary because nlpo3 is dynamically imported at runtime. However, explicit type annotations provide a cleaner alternative.Additionally, the type hint analysis was showing inaccurate results (9+ mypy errors) due to stale mypy cache, making it difficult to verify the changes.
How this fixes it
Replaced
cast()calls with explicit type annotations:For
load_dict()calls:For
segment()call:Why explicit annotations are necessary:
The explicit type annotations (
msg: str,success: bool,result: list[str]) are required because nlpo3 is dynamically imported at runtime. Without these annotations, mypy sees the return values asAny, causing "Returning Any from function declared to return..." errors. The annotations provide necessary type information for static type checking while being cleaner thancast()calls.Type hint analysis improvements:
Cleared mypy cache and regenerated type hint analysis to ensure accuracy. The updated analysis confirms 0 mypy errors across all 191 source files and maintains 100% function type annotation coverage.
Applied to all three call sites in
pythainlp/tokenize/nlpo3.py. Also removed the now-unusedcastimport from typing.Your checklist for this pull request
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.