Skip to content

Align docstrings with type hints across codebase - #1270

Merged
bact merged 8 commits into
devfrom
copilot/verify-type-hints-annotations
Feb 3, 2026
Merged

Align docstrings with type hints across codebase#1270
bact merged 8 commits into
devfrom
copilot/verify-type-hints-annotations

Conversation

Copilot AI commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

What do these changes do

Synchronizes docstrings with type hints, eliminating 42 mismatches where documentation used outdated typing module syntax while code used Python 3.9+ native types. Additionally ensures non-standard types use fully qualified module names for clarity.

What was wrong

Docstrings documented types as List[str], Dict[...], Tuple[...] (typing module style) while function signatures used list[str], dict[...], tuple[...] (native Python 3.9+ style). Additionally, some docstrings used local import aliases (like pd.DataFrame) or unqualified type names (like Word2VecKeyedVectors) instead of fully qualified module names, making it unclear which module provides the type.

How this fixes it

Documentation updates (42 fixes across 16 files):

  • Listlist, Dictdict, Tupletuple in :rtype: annotations
  • Union[List[...], ...]Union[list[...], ...] for consistency
  • Non-standard types now use fully qualified names:
    • pd.DataFramepandas.DataFrame
    • Word2VecKeyedVectorsgensim.models.keyedvectors.Word2VecKeyedVectors
    • ndarraynumpy.ndarray

Type hint improvements (3 instances):

  • dictdict[str, Any] in benchmarks/word_tokenization.py for flattened result structures
  • listlist[tuple[int, int]] where docstrings specified structure

High-impact modules:

  • tokenize/core.py: 8 fixes (tokenization return types)
  • benchmarks/word_tokenization.py: 8 fixes (benchmark metrics)
  • phayathaibert/core.py: 4 fixes (NER outputs)
  • word_vector/core.py: 4 fixes (word vector model types)

Your checklist for this pull request

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

Verify precision of type hints and annotations and docstring.

Current codebase contains some type hints and comments for static type checkers,
including ones that added by these PRs:

#1262
#1263
#1264
#1265
#1266
#1267
#1268
#1269

With the scope of the entire package (100%), incrementally verify if the annotations are correct and precise, and if the docstring and other documentation are up to date to the type hints.

Goals

  • When a type hint exist, it is correct and the docstring is correct
  • Use "ignore" comments only when it is necessary and with a scoped/specific type
  • Report percentage of type hints coverage of the entire package

Instructions

  • Follow best practices and standard Python type hint patterns.
  • 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

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: bact <128572+bact@users.noreply.github.com>
@bact bact added this to PyThaiNLP Feb 3, 2026
@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
Copilot AI and others added 2 commits February 3, 2026 12:22
…oint None check

Co-authored-by: bact <128572+bact@users.noreply.github.com>
… code

Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot AI changed the title [WIP] Verify precision of type hints and docstrings Verify and improve type hints precision across codebase Feb 3, 2026
Copilot AI requested a review from bact February 3, 2026 12:29
@coveralls

coveralls commented Feb 3, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 65.571% (-0.08%) from 65.653%
when pulling c656076 on copilot/verify-type-hints-annotations
into a248ba9 on dev.

@bact
bact marked this pull request as ready for review February 3, 2026 13:04
Copilot AI and others added 3 commits February 3, 2026 13:10
Update docstrings to use native Python types (list, dict, tuple) instead of typing module style (List, Dict, Tuple) to match the type hints. This affects:
- pythainlp/phayathaibert/core.py (4 fixes)
- pythainlp/benchmarks/word_tokenization.py (4 fixes)
- pythainlp/word_vector/core.py (2 fixes)
- pythainlp/tag/named_entity.py (2 fixes)
- pythainlp/spell/core.py (2 fixes)
- pythainlp/tag/tltk.py (1 fix)
- pythainlp/tag/thainer.py (1 fix)
- pythainlp/tag/chunk.py (1 fix)
- pythainlp/spell/words_spelling_correction.py (1 fix)
- pythainlp/spell/pn.py (1 fix)
- pythainlp/coref/core.py (1 fix)

Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot AI changed the title Verify and improve type hints precision across codebase Align docstrings with type hints across codebase Feb 3, 2026
Copilot AI requested a review from bact February 3, 2026 13:21

:return: dataframe with row x col = len(samples) x len(metrics)
:rtype: pandas.DataFrame
:rtype: pd.DataFrame

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

keep the fullname. "pd" is an alias used only locally.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in c656076 - changed to pandas.DataFrame

Comment thread pythainlp/word_vector/core.py Outdated

:return: `gensim` word2vec model
:rtype: gensim.models.keyedvectors.Word2VecKeyedVectors
:rtype: Word2VecKeyedVectors

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use fullname of the imported.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in c656076 - changed to gensim.models.keyedvectors.Word2VecKeyedVectors

Comment thread pythainlp/word_vector/core.py Outdated
:return: 300-dimension vector representing the given sentence
in form of :mod:`numpy` array
:rtype: :class:`numpy.ndarray((1,300))`
:rtype: ndarray

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use full qualified name "numpy.ndarray"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in c656076 - changed to numpy.ndarray

@bact bact left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot in docstring, for the non-standard types, use full qualified name. So the user can know that the type comes from which module.

Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot AI requested a review from bact February 3, 2026 13:32
@sonarqubecloud

sonarqubecloud Bot commented Feb 3, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
17.3% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

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

Status: Done

Development

Successfully merging this pull request may close these issues.

Incompatible types in benchmarks.word_tokenization

3 participants