Skip to content

Tidy exception forwarding, warning consistency, and document conventions#1355

Merged
bact merged 3 commits intodevfrom
copilot/tidy-code-for-consistency
Mar 22, 2026
Merged

Tidy exception forwarding, warning consistency, and document conventions#1355
bact merged 3 commits intodevfrom
copilot/tidy-code-for-consistency

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 22, 2026

18 raise statements inside except blocks were missing from chaining, silently discarding original tracebacks. Three warning calls omitted the category argument and used inconsistent messages for the same condition. No documented convention existed for either.

Exception forwarding (chaining)

Every except ImportError/ModuleNotFoundError: that re-raised was updated to capture the original and chain it:

# Before
except ImportError:
    raise ImportError("tltk is not installed...")

# After
except ImportError as e:
    raise ImportError("tltk is not installed. Install it with: pip install tltk") from e

raise e in tools/misspell.py → bare raise (preserves original traceback; unused as e removed).

raise ValueError(str(e)) in corpus/core.pyraise ValueError(str(e)) from e.

Files: cli/benchmark.py, corpus/core.py, parse/{esupar,attaparse}_engine.py, spell/{symspellpy,phunspell,tltk}.py, tag/{tltk,pos_tag,thai_nner}.py, tokenize/{deepcut,tltk}.py, tools/misspell.py, translate/en_th.py, transliterate/tltk.py, util/abbreviation.py

Warning consistency

Three warnings.warn calls in phayathaibert/core.py and wangchanberta/core.py (two locations) had no explicit category and used three different phrasings for the same condition. All normalized to:

warnings.warn("This model does not support POS tag output.", UserWarning, stacklevel=2)

ImportError message normalization

All ImportError messages updated to the canonical form established by the new convention:

<Package> is not installed. Install it with: pip install <package>

CONTRIBUTING.md — new section: "Error messages, warnings, and exception handling"

Added under Code Guidelines to codify the conventions above for contributors and AI agents:

  • Exception type selection table
  • Exception message format (sentences, pip-install templates, param-value template)
  • Exception forwarding rules (from e vs. from None vs. bare raise), with correct/incorrect examples
  • Warning conventions (always explicit category + stacklevel, recommended categories table, message style)

Your checklist for this pull request

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

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…onventions to CONTRIBUTING.md

Co-authored-by: bact <128572+bact@users.noreply.github.com>
Agent-Logs-Url: https://github.com/PyThaiNLP/pythainlp/sessions/1a240bb9-52d0-40e6-8739-79cf52fa9e65
Copilot AI changed the title [WIP] Tidy code for consistency, predictability, and scriptability Tidy exception forwarding, warning consistency, and document conventions Mar 22, 2026
Copilot AI requested a review from bact March 22, 2026 16:20
Copilot finished work on behalf of bact March 22, 2026 16:20
@sonarqubecloud
Copy link
Copy Markdown

@bact bact marked this pull request as ready for review March 22, 2026 17:39
@bact bact merged commit 570aea1 into dev Mar 22, 2026
29 checks passed
@bact bact deleted the copilot/tidy-code-for-consistency branch March 22, 2026 17:39
@bact bact added the refactoring a technical improvement which does not add any new features or change existing features. label Mar 23, 2026
@bact bact added this to the 5.3.3 milestone Mar 23, 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.

2 participants