Fix Python 3.13 SyntaxError: global declaration before variable use in tokenizer modules #1218
+4
−4
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
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.