Skip to content

Fix type hints for Python 3.9 runtime compatibility#1231

Merged
bact merged 6 commits intodevfrom
copilot/fix-type-hints-inconsistent
Jan 29, 2026
Merged

Fix type hints for Python 3.9 runtime compatibility#1231
bact merged 6 commits intodevfrom
copilot/fix-type-hints-inconsistent

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

What does this changes

Fixes type hints that break Python 3.9 runtime introspection (typing.get_type_hints()). Replaces PEP 604 | union syntax with Union[]/Optional[], adds missing return type hints, and ensures from __future__ import annotations is present where needed.

Files changed: 11 files across tag, tokenize, spell, benchmarks, transliterate, coref, and word_vector modules.

What was wrong

The | union operator (PEP 604) evaluates at runtime in Python 3.9, causing failures when static analysis tools, documentation generators, or runtime type inspection call typing.get_type_hints(). For example:

# This breaks in Python 3.9 runtime type inspection
def func(x: str | None) -> dict[str, str | bool]:
    ...

Additionally, ~20 functions lacked return type hints, and one module with type hints was missing the future annotations import.

How this fixes it

Union syntax fixes (2 occurrences):

  • pythainlp/tag/thainer.py: str | boolUnion[str, bool]
  • pythainlp/tokenize/han_solo.py: str | NoneOptional[str]

Missing imports (1 file):

  • pythainlp/tokenize/__init__.py: Added from __future__ import annotations

Missing return types (8 files):

  • spell/core.py: NorvigSpellChecker (with TYPE_CHECKING)
  • spell/symspellpy.py: SymSpell
  • tag/perceptron.py: PerceptronTagger (5 functions)
  • tag/unigram.py: dict (5 functions)
  • transliterate/core.py: Callable[[str], str]
  • benchmarks/word_tokenization.py: np.ndarray
  • coref/core.py: list[dict]
  • word_vector/core.py: None

With from __future__ import annotations, type hints become strings at parse time, avoiding runtime evaluation issues while maintaining compatibility with Python 3.9.

Your checklist for this pull request

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

Find in the code, code comments, and documentation for wrong type hints or inconsistent type hints.

Fix them all, in every submodules.
Fix one submodule at a time, review, revise, commit, then iterate to other submodules.

Keep the Python 3.9 compatibility (with import future annotations).
Do not the | union syntax as it will fail at runtime in Python 3.9 when introspection tools call typing.get_type_hints().
We need our modules to be useable by static analysis tools, documentation generators, and runtime type inspection.

Use PR #1230 as reference.

mypy, pyrefly, and pyright can be used to help find type issues but we don't have to taken care of every suggestions made by these tools at this stage. The priority now is to make all functions have type hints and have them in a consistent way.


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

Copilot AI and others added 5 commits January 29, 2026 18:46
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>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
@sonarqubecloud
Copy link

@coveralls
Copy link

Coverage Status

coverage: 53.665% (+0.03%) from 53.632%
when pulling b93128a on copilot/fix-type-hints-inconsistent
into 59695bd on dev.

Copilot AI changed the title [WIP] Fix wrong and inconsistent type hints in submodules Fix type hints for Python 3.9 runtime compatibility Jan 29, 2026
Copilot AI requested a review from bact January 29, 2026 18:58
@bact bact marked this pull request as ready for review January 29, 2026 20:45
@bact bact merged commit 12d7768 into dev Jan 29, 2026
32 checks passed
@bact bact deleted the copilot/fix-type-hints-inconsistent branch January 29, 2026 20:45
@bact bact added the refactoring a technical improvement which does not add any new features or change existing features. label Jan 30, 2026
@bact bact added this to the 5.3 milestone Jan 30, 2026
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