Skip to content

Add type hints to noauto test suite modules - #1269

Merged
bact merged 3 commits into
devfrom
copilot/add-type-hints-to-submodules-one-more-time
Feb 3, 2026
Merged

Add type hints to noauto test suite modules#1269
bact merged 3 commits into
devfrom
copilot/add-type-hints-to-submodules-one-more-time

Conversation

Copilot AI commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

What do these changes do

Adds complete type annotations to spell and tag modules tested by the noauto test suite, achieving 100% type hints coverage for these entry points.

What was wrong

Functions and classes in wanchanberta_thai_grammarly, words_spelling_correction, named_entity, and thai_nner modules lacked type annotations, preventing static type checking and degrading IDE support for noauto test suite functionality.

How this fixes it

Spell modules:

  • wanchanberta_thai_grammarly.py: Typed BertModel class methods and evaluate_one_text() helper
  • words_spelling_correction.py: Typed all 11 FastTextEncoder methods with appropriate use of Any for numpy/ONNX types

Tag modules:

  • named_entity.py: Typed NER.tag() and NNER.tag() parameters; improved return type precision to dict[str, Any]
  • thai_nner.py: Typed Thai_NNER methods; fixed mutable default argument anti-pattern

All type hints maintain Python 3.9 compatibility using native types and Union[] syntax.

Your checklist for this pull request

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

Iterating to incrementally add type hints to submodules,
based on works done in these PRs:
#1262
#1263
#1264
#1265
#1266
#1267
#1268

Strategy

  • Start with small functions/classes and functions/classes that require few external dependencies.
  • Moving gradually one function by one function, in the four test suites: starting from "core", then "compact", then "extra", and "noauto".
  • Note that we don't have test suite for "noauto", so be carefully verify and validate the functionality after making changes

Goals

  • Immediate goal for this session - 100% type hints for functions/classes being called in "noauto" test suite.
  • Ultimate goal (outside of this session) is to reach 100% type hints in the entire repo, make the package a typed package, while keeping maintainability.

Instructions

  • Follow best practices and standard Python type hint patterns.
  • Start small in the area with high confidence (like highly tested submodules or functions), then gradually grow one submodule at a time.
  • Use mypy as main assistant.
    • mypy is already in "dev" extra dependencies in pyproject.toml
    • mypy configuration is in pyproject.toml
    • Sometimes mypy may report errors wrongly due to cache issues. Try to reset the cache.
  • Use pyright, pyrefly, and pytype for second opinions.
  • Required dependencies for each test suite are in pyproject.toml. Install them to avoid errors. See https://github.com/PyThaiNLP/pythainlp/blob/dev/tests/README.md
  • Use native type in type hint (list, tuple, .. instead of List, Tuple, ..)
  • Do not use A | B style Union, use full Union[] instead.
  • We want to keep compatibility with Python 3.9 strict and allow type inspection tools to work correctly.
  • When insert typing imports, put it in appropriate location and order
  • Use "if TYPE_CHECKING import" block when possible.
  • When reordering the imports, beware of circular import. Read comments near imports to get more information.
  • Remove unused imports
  • Do not allow the use of assert in production code (it is only allowed for testing and debugging).
  • Try to fix the type, the scope logic, or do the casting, before putting #type: ignore comment.
  • When dealing with types from external libraries:
    • Check if type information is provided (maybe through stubs). If it is, cast wrapper may not necessary.
    • If return type is not provided, may be it is appropriate to use # type: ignore[no-any-return] annotation for clarity.
    • Analyse source code of external libraries to validate correct types
  • Recheck necessity when do casting.
  • Recheck necessity of # noqa: and # type: ignore
  • Provide type stubs as necessary.
  • After changes for type hints, recheck for functionality correctness.
  • Recheck docstring and documentation consistency with the code; They should match the updated type hints.
  • Recheck formatting with ruff
  • Do another code review
  • Address code review feedback

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits February 3, 2026 12:00
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Feb 3, 2026

Copy link
Copy Markdown

Copilot AI changed the title [WIP] Add type hints to submodules incrementally Add type hints to noauto test suite modules Feb 3, 2026
Copilot AI requested a review from bact February 3, 2026 12:06
@bact bact added the refactoring a technical improvement which does not add any new features or change existing features. label Feb 3, 2026
@bact bact added this to the 5.3 milestone Feb 3, 2026
@bact
bact marked this pull request as ready for review February 3, 2026 12:08
@bact
bact merged commit a248ba9 into dev Feb 3, 2026
22 of 32 checks passed
@bact
bact deleted the copilot/add-type-hints-to-submodules-one-more-time branch February 3, 2026 12:12
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 65.653% (-0.03%) from 65.683%
when pulling e92bdbb on copilot/add-type-hints-to-submodules-one-more-time
into adefafc on dev.

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.

3 participants