Skip to content

Defensive file loading - #1327

Merged
bact merged 3 commits into
PyThaiNLP:devfrom
bact:protect-literal-eval
Mar 14, 2026
Merged

Defensive file loading#1327
bact merged 3 commits into
PyThaiNLP:devfrom
bact:protect-literal-eval

Conversation

@bact

@bact bact commented Mar 13, 2026

Copy link
Copy Markdown
Member
  • Check row completeness and validate data before append to dictionary

  • Skip invalid entry, raise warning, and continue on next entry

  • Passed code styles and structures

  • Passed code linting checks and unit test

Check row completeness before append to dictionary
Check types before evaluate

Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
@bact bact added this to the 5.3.1 milestone Mar 13, 2026
@bact bact added refactoring a technical improvement which does not add any new features or change existing features. security labels Mar 13, 2026
@bact
bact requested a review from wannaphong March 13, 2026 19:46
@coveralls

coveralls commented Mar 13, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 65.923% (+0.3%) from 65.627%
when pulling 7e4b3fe on bact:protect-literal-eval
into a5432cc on PyThaiNLP:dev.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Improves robustness of built-in corpus loaders in pythainlp.corpus.common by skipping malformed records and warning instead of failing, aligning with the goal of defensive file loading.

Changes:

  • Add warning-based skipping for malformed province CSV lines.
  • Refactor thai_dict(), thai_wsd_dict(), and thai_synonyms() to accumulate validated rows before assigning globals.
  • Add parsing/shape checks for thai_wsd_dict() entries.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread pythainlp/corpus/common.py Outdated
Comment thread pythainlp/corpus/common.py Outdated
Comment thread pythainlp/corpus/common.py Outdated
Comment thread pythainlp/corpus/common.py
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds defensive, warning-based validation when loading certain corpus resources, preventing malformed rows from crashing corpus helpers and providing test coverage for these edge cases.

Changes:

  • Add validation + UserWarning emissions for malformed/blank rows in provinces(), thai_dict(), thai_wsd_dict(), and thai_synonyms().
  • Refactor dictionary/synonyms loaders to accumulate validated rows before caching globals.
  • Add unit tests covering skipped rows and warning messages.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pythainlp/corpus/common.py Implements defensive row validation and warning-based skipping for corpus loaders.
tests/core/test_corpus.py Adds tests to verify skipping behavior and warnings for malformed/empty fields.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread pythainlp/corpus/common.py
Comment thread tests/core/test_corpus.py
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

@bact bact added the corpus corpus/dataset-related issues label Mar 14, 2026
@bact
bact merged commit d9dec9e into PyThaiNLP:dev Mar 14, 2026
28 checks passed
@bact
bact deleted the protect-literal-eval branch March 19, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

corpus corpus/dataset-related issues refactoring a technical improvement which does not add any new features or change existing features. security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants