Fix type hints for Python 3.9 runtime compatibility#1231
Merged
Conversation
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>
|
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
bact
approved these changes
Jan 29, 2026
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What does this changes
Fixes type hints that break Python 3.9 runtime introspection (
typing.get_type_hints()). Replaces PEP 604|union syntax withUnion[]/Optional[], adds missing return type hints, and ensuresfrom __future__ import annotationsis 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 calltyping.get_type_hints(). For example: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 | bool→Union[str, bool]pythainlp/tokenize/han_solo.py:str | None→Optional[str]Missing imports (1 file):
pythainlp/tokenize/__init__.py: Addedfrom __future__ import annotationsMissing return types (8 files):
spell/core.py:NorvigSpellChecker(withTYPE_CHECKING)spell/symspellpy.py:SymSpelltag/perceptron.py:PerceptronTagger(5 functions)tag/unigram.py:dict(5 functions)transliterate/core.py:Callable[[str], str]benchmarks/word_tokenization.py:np.ndarraycoref/core.py:list[dict]word_vector/core.py:NoneWith
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
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.