Refactor featurize() to eliminate Union types and cast() calls#1236
Merged
Refactor featurize() to eliminate Union types and cast() calls#1236
Conversation
…type and cast() calls 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] Refactor featurize function in han_solo.py
Refactor featurize() to eliminate Union types and cast() calls
Jan 30, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the Featurizer.featurize() method in pythainlp/tokenize/han_solo.py to eliminate Union types and cast() calls, addressing technical debt from PR #1235. The refactoring improves type safety and code maintainability by building features in a single, consistent representation and converting to the requested format only at the method's return point.
Changes:
- Eliminated Union types and 6 cast() calls by building features as
list[str]throughout the method - Improved variable naming (
all_features_list,all_labels_int) for better type clarity - Consolidated conversion logic at the end of the method, reducing code duplication
bact
approved these changes
Jan 30, 2026
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
Refactors
Featurizer.featurize()inpythainlp/tokenize/han_solo.pyto eliminate type casting and Union types, addressing PR #1235 review comments.What was wrong
The
featuresvariable was typed asUnion[dict[str, int], list[str]], requiring 6cast()calls scattered throughout the feature extraction loop for type narrowing. This violated type safety principles and created maintenance burden—any logic change required updating two code paths.How this fixes it
Core logic now builds features in their natural representation (list of strings), converting to dict only at method exit when needed:
Changes:
UnionandcastimportsYour checklist for this pull request
Original prompt
💡 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.