Skip to content

Revert union type syntax from | to Union[] for Python 3.9 runtime compatibility#1230

Merged
bact merged 8 commits intodevfrom
copilot/fix-type-hints-inconsistencies
Jan 29, 2026
Merged

Revert union type syntax from | to Union[] for Python 3.9 runtime compatibility#1230
bact merged 8 commits intodevfrom
copilot/fix-type-hints-inconsistencies

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

What does this changes

Replaces all A | B union type syntax with Union[A, B] and Optional[A] in type hints across 8 files in corpus, tools, util, and tag modules.

What was wrong

The | union syntax fails at runtime in Python 3.9 when introspection tools call typing.get_type_hints(), despite from __future__ import annotations allowing it to parse. This breaks static analysis tools, documentation generators, and runtime type inspection.

# Before: Parses but breaks get_type_hints() in Python 3.9
def provinces(details: bool = False) -> frozenset[str] | list[dict[str, str]]:
    ...

# After: Works with runtime introspection
def provinces(details: bool = False) -> Union[frozenset[str], list[dict[str, str]]]:
    ...

How this fixes it

  • corpus/common.py: Union[frozenset[str], list[dict[str, str]]]
  • corpus/core.py: Optional[_ResponseWrapper]
  • tools/misspell.py: Optional[tuple[...]], Optional[list[str]]
  • util/date.py: Optional[int]
  • util/keyboard.py: Optional[list[list[str]]]
  • util/wordtonum.py: tuple[bool, Optional[str]]
  • tag/wangchanberta_onnx.py: Union[str, list[tuple[str, str]]]
  • tag/crfchunk.py: Union[str, bool], Optional[type[BaseException]], Optional[types.TracebackType]

Added necessary Union and Optional imports where missing.

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. Start with pythainlp.tokenize, pythainlp.corpus, pythainlp.util, pythainlp.tools, and pythainlp.tag.
Keep the Python 3.9 compatibility (with import future annotations).


✨ 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 and others added 2 commits January 29, 2026 17:49
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
@coveralls
Copy link

coveralls commented Jan 29, 2026

Coverage Status

coverage: 53.644% (+0.03%) from 53.614%
when pulling b373acc on copilot/fix-type-hints-inconsistencies
into e4c6f6b on dev.

Copilot AI and others added 2 commits January 29, 2026 17:55
…tations

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

Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix wrong and inconsistent type hints in Pythainlp Fix type hint inconsistencies across tokenize, corpus, util, tools, and tag modules Jan 29, 2026
Copilot AI requested a review from bact January 29, 2026 18:04
…typing.get_type_hints()

Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot AI changed the title Fix type hint inconsistencies across tokenize, corpus, util, tools, and tag modules Revert union type syntax from | to Union[] for Python 3.9 runtime compatibility Jan 29, 2026
@sonarqubecloud
Copy link

@bact bact marked this pull request as ready for review January 29, 2026 18:39
@bact bact added the refactoring a technical improvement which does not add any new features or change existing features. label Jan 29, 2026
@bact bact added this to the 5.3 milestone Jan 29, 2026
@bact bact merged commit 59695bd into dev Jan 29, 2026
32 checks passed
@bact bact deleted the copilot/fix-type-hints-inconsistencies branch January 29, 2026 18:40
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