Fix Python 3.13 SyntaxError: global declaration before variable use in tokenizer modules#1218
Fix Python 3.13 SyntaxError: global declaration before variable use in tokenizer modules#1218
Conversation
Co-authored-by: bact <128572+bact@users.noreply.github.com>
|
@copilot Got similar issues in other files as well. Looks like the thread-safe improvement PR at #1213 introduces few of this "used prior to global declaration" bug. Please fix them all. Error running coveralls: Got coverage library error: Couldn't parse '/home/runner/work/pythainlp/pythainlp/pythainlp/tokenize/oskut.py' as Python source: "name '_DEFAULT_ENGINE' is used prior to global declaration" at line 44 |
Co-authored-by: bact <128572+bact@users.noreply.github.com>
|
Fixed all similar issues in oskut.py, sefr_cut.py, and wtsplit.py. The |



What does this changes
Moves
globaldeclarations before variable use inpythainlp/tokenize/budoux.py,pythainlp/tokenize/oskut.py,pythainlp/tokenize/sefr_cut.py, andpythainlp/tokenize/wtsplit.py.What was wrong
Python 3.13 enforces stricter scoping rules: using a variable before declaring it
globalin the same scope is now a SyntaxError. The coverage parser failed with errors in multiple files:in budoux.py, and similar errors for
_DEFAULT_ENGINEin oskut.py and sefr_cut.py, and_MODEL, _MODEL_NAMEin wtsplit.py.The original code checked variables (e.g.,
if _parser is None:,if engine != _DEFAULT_ENGINE:) before declaring them asglobal, which violated Python 3.13's scoping rules. These issues were introduced by the thread-safe improvement PR #1213.How this fixes it
Move all
globaldeclarations from inside the lock blocks to before the lock blocks. The variables must be declared global before any use in the function scope.The same pattern was applied to:
budoux.py:global _parseroskut.py:global _DEFAULT_ENGINEsefr_cut.py:global _DEFAULT_ENGINEwtsplit.py:global _MODEL, _MODEL_NAMEYour checklist for this pull request
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.