Fix np.load allow_pickle=False to work correctly with .npz format - #1329
Merged
Conversation
2 tasks
- Replace .item().get(key) with [key] dict-style access on NpzFile - Remove variables instance attribute; use local variable instead - Add type annotation for variables local var as np.lib.npyio.NpzFile - Add allow_pickle=False to embeddings.npy load in words_spelling_correction.py Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
Contributor
Author
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Copilot
AI
changed the title
[WIP] [WIP] Update np.load to disallow allow_pickle
Fix np.load allow_pickle=False to work correctly with .npz format
Mar 13, 2026
|
wannaphong
marked this pull request as ready for review
March 13, 2026 22:09
wannaphong
added a commit
that referenced
this pull request
Mar 14, 2026
* Update np.load to disallow allow_pickle Change np.load to disallow pickling for security. * Fix np.load allow_pickle=False to work correctly with .npz format (#1329) * Initial plan * Fix np.load allow_pickle=False to work with .npz NpzFile format - Replace .item().get(key) with [key] dict-style access on NpzFile - Remove variables instance attribute; use local variable instead - Add type annotation for variables local var as np.lib.npyio.NpzFile - Add allow_pickle=False to embeddings.npy load in words_spelling_correction.py Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com> * Initial plan * Fix np.load allow_pickle for legacy .npy corpus (ValueError fix) The thai_w2p corpus v0.2 is stored as a .npy pickled dict. Loading it with allow_pickle=False raises ValueError. Detect file format by extension: - .npz: use allow_pickle=False (secure, for future corpus versions) - .npy (legacy): use allow_pickle=True + dict validation Also add `import os` for os.path.splitext(). Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com> * Initial plan * Sync dev, add pickle warning, fix docstring and code style Co-authored-by: bact <128572+bact@users.noreply.github.com> * Update CHANGELOG for release 5.3.1 This release focuses on security issues related to corpus file loading, including improved pickle handling and defensive file loading. * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Only load pickle file for PYTHAINLP_W2P_ALLOW_LEGACY_PICKLE is set Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Add PYTHAINLP_W2P_ALLOW_LEGACY_PICKLE: 1 to untitest env * Change PYTHAINLP_W2P_ALLOW_LEGACY_PICKLE to PYTHAINLP_ALLOW_UNSAFE_PICKLE Add is_unsafe_pickle_allowed() function * Sort import * Add test for PYTHAINLP_ALLOW_UNSAFE_PICKLE * Fix import * Set PYTHAINLP_ALLOW_UNSAFE_PICKLE for tone detector test * Refactor is_unsafe_pickle_allowed() * Refactor model loading to use .npz format only Updated model name and refactored variable loading to use .npz format exclusively, removing legacy .npy handling. * Clean up w2p.py by removing os and warnings imports Removed unused imports from w2p.py * Remove PYTHAINLP_ALLOW_UNSAFE_PICKLE tests * Update CHANGELOG.md * Fix imports * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Update CHANGELOG.md * Remove PYTHAINLP_ALLOW_UNSAFE_PICKLE from doc We no longer use pickle. Do not advertise this env var. Keep it internally for future use. (may remove in 6.0.0) --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com> Co-authored-by: Arthit Suriyawongkul <arthit@gmail.com> Co-authored-by: bact <128572+bact@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.



np.load(..., allow_pickle=False)was set inw2p.pybut loading still used.item().get(key)— only valid for pickle-based.npyobject arrays. This made the security fix non-functional and would raise anAttributeErrorat runtime.What do these changes do
w2p.py: Replace allself.variables.item().get(key)withvariables[key](NpzFile dict-style access); convertself.variablesto a typed local variable; remove the now-unused class attribute declarationwords_spelling_correction.py: Addallow_pickle=Falseto theembeddings.npyload (plain float array, no pickle needed)What was wrong
The Thai W2P checkpoint is a
.npzfile (saved vianp.savez).np.loadon.npzreturnsnumpy.lib.npyio.NpzFile, which:.item()— so the old access pattern would raiseAttributeErrorallow_pickle=Falsenatively viavariables["key"]subscript accessThe previous commit set
allow_pickle=Falsebut kept the pickle-era.item().get()access pattern, making the fix both broken and incomplete.How this fixes it
Switch to dict-style subscript access (
variables["key"]) which is the correct API forNpzFileobjects and works without pickle:Your checklist for this pull request
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.