Skip to content

Fix type annotation errors and remove unused type:ignore comments - #1281

Merged
bact merged 7 commits into
devfrom
copilot/verify-type-annotations
Feb 4, 2026
Merged

Fix type annotation errors and remove unused type:ignore comments#1281
bact merged 7 commits into
devfrom
copilot/verify-type-annotations

Conversation

Copilot AI commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

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() and nlpo3.segment() have proper type stubs declaring their return types as Tuple[str, bool] and list[str] respectively, but the code used cast() 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:

# Before
msg, success = nlpo3_load_dict(file_path=file_path, dict_name=dict_name)
return cast(bool, success)

# After  
msg: str
success: bool
msg, success = nlpo3_load_dict(file_path=file_path, dict_name=dict_name)
return success

For segment() call:

# Before
return cast(
    list[str],
    nlpo3_segment(
        text=text,
        dict_name=custom_dict,
        safe=safe_mode,
        parallel=parallel_mode,
    ),
)

# After
result: list[str] = nlpo3_segment(
    text=text,
    dict_name=custom_dict,
    safe=safe_mode,
    parallel=parallel_mode,
)
return result

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 as Any, causing "Returning Any from function declared to return..." errors. The annotations provide necessary type information for static type checking while being cleaner than cast() 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-unused cast import from typing.

Your checklist for this pull request

  • Passed code styles and structures
  • Passed code linting checks and unit test
Original prompt

Verify and improve type annotation correctness and preciseness

  • Scan the codebase, submodule by submoduel, file by file, create a map of class, function, variable, type alias, import, export, etc inside each file. Use this map to help to type annotation verification.

  • Recheck for Any type annotation that can be more specific, use information from library source code if available or install the external library if needed

    • Do not use information from private submodule (those starts with _ underscore)
  • Recheck to avoid any type annotation reassignments/redundancy

  • Recheck to remove unused type ignore comments or unused noqa comments

  • Use strict Python 3.9 compatibility; Do not use A | B union syntax, use Union[] and Optional[] syntax to allow introspection tools to work correctly

  • Verify the changes with mypy and pyrefly

  • Run ruff and fix errors (becareful of unused imports and import order; don't introduce cyclic imports)

  • Then review the code again and fix according to feedback

  • Iterate


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI assigned Copilot and bact Feb 4, 2026
@bact bact added the refactoring a technical improvement which does not add any new features or change existing features. label Feb 4, 2026
@bact bact added this to the 5.3 milestone Feb 4, 2026
Copilot AI and others added 2 commits February 4, 2026 22:44
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>
@coveralls

coveralls commented Feb 4, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 64.791% (+0.001%) from 64.79%
when pulling 8dca693 on copilot/verify-type-annotations
into f22c110 on dev.

@bact
bact marked this pull request as ready for review February 4, 2026 23:00
- 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>
Copilot AI changed the title [WIP] Improve type annotation correctness and preciseness Fix type annotation errors and remove unused type:ignore comments Feb 4, 2026
Copilot AI requested a review from bact February 4, 2026 23:09
…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>
Copilot AI changed the title Fix type annotation errors and remove unused type:ignore comments Remove unnecessary cast in nlpo3.load_dict by using explicit type annotations Feb 4, 2026
@bact
bact requested a review from Copilot February 4, 2026 23:26
Comment thread pythainlp/tokenize/nlpo3.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in pythainlp/tokenize/nlpo3.py by 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.py for 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

Comment thread pythainlp/tokenize/nlpo3.py Outdated
Comment thread pythainlp/tokenize/nlpo3.py Outdated
Comment thread pythainlp/tokenize/nlpo3.py
Comment thread tests/data/eval-details-input.json
Comment thread pythainlp/tokenize/nlpo3.py Outdated
Comment thread pythainlp/tokenize/nlpo3.py
Comment thread build_tools/analysis/output/type_hint_analysis.json
- 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 AI changed the title Remove unnecessary cast in nlpo3.load_dict by using explicit type annotations Remove unnecessary casts in nlpo3 module by using explicit type annotations Feb 4, 2026
Copilot AI requested a review from bact February 4, 2026 23:33
@bact

bact commented Feb 4, 2026

Copy link
Copy Markdown
Member

@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>

Copilot AI commented Feb 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot apply changes based on the comments in this thread. But not that the stats from type hint analyzer may not acc...

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 (msg: str, success: bool), mypy reports "Returning Any" errors because nlpo3 is dynamically imported and mypy can't infer the types. The annotations provide essential type information for static type checking.

Fixed in commit 8dca693.

@sonarqubecloud

sonarqubecloud Bot commented Feb 4, 2026

Copy link
Copy Markdown

@bact bact changed the title Remove unnecessary casts in nlpo3 module by using explicit type annotations Fix type annotation errors and remove unused type:ignore comments Feb 4, 2026
@bact
bact merged commit af709c9 into dev Feb 4, 2026
32 checks passed
@bact
bact deleted the copilot/verify-type-annotations branch February 4, 2026 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring a technical improvement which does not add any new features or change existing features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants