Delay external dependency imports to class initialization - #1257
Conversation
- word_vector/core.py: Move gensim and numpy imports into methods - summarize/keybert.py: Move numpy and transformers imports into methods - summarize/mt5.py: Move transformers imports into __init__ - wangchanberta/core.py: Lazy load tokenizer via helper function - coref/_fastcoref.py: Move spacy import to inside __init__, avoid default mutable arg Co-authored-by: bact <128572+bact@users.noreply.github.com>
- augment/lm/wangchanberta.py: Move transformers imports to __init__ - augment/lm/fasttext.py: Move gensim imports to __init__ - coref/han_coref.py: Remove spacy import from default parameter - parse/transformers_ud.py: Move heavy ML imports to __init__ and __call__ Co-authored-by: bact <128572+bact@users.noreply.github.com>
- Add TYPE_CHECKING imports for type hints without runtime imports - Restore type hints using proper forward references - Address code review feedback about preserving type information Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@copilot review again |
- Restore tests/data/text-misspelled-r0.05-seed1.txt (was accidentally deleted) - Use Python 3.9+ native union syntax (list[list[str]] | str) instead of Union - Remove unused typing imports (List, Union) Co-authored-by: bact <128572+bact@users.noreply.github.com>
All review comments have been addressed in commit 6a2a398:
All 111 core tests pass and Ruff linting checks succeed. |
|
@copilot |
- Change from PEP 604 union syntax (list[list[str]] | str) to Union[list[list[str]], str] - Maintain native lowercase list types for Python 3.9+ - Ensure compatibility with type inspection tools in Python 3.9 strict mode Co-authored-by: bact <128572+bact@users.noreply.github.com>
Updated in commit 0ec00ae to use |
|



What do these changes do
Moves module-level imports of heavy external dependencies (transformers, torch, gensim, numpy, spacy) to class initialization methods, enabling module imports without requiring optional dependencies.
What was wrong
Module-level imports forced dependency resolution at import time, blocking:
Example failure case:
How this fixes it
Defers dependency imports to where they're actually needed:
Pattern 1: Import in
__init__Pattern 2: Import in method
Pattern 3: Lazy module-level via getter
Used
TYPE_CHECKINGguards to preserve type hints without runtime imports. Updated type hints to use native Python 3.9+ lowercase types (list,dict) withUnionfromtypingmodule for compatibility with Python 3.9 strict mode and type inspection tools.Modified modules (9):
word_vector/core.py- gensim, numpysummarize/keybert.py- numpy, transformerssummarize/mt5.py- transformerswangchanberta/core.py- transformers (tokenizer)coref/_fastcoref.py- spacycoref/han_coref.py- spacyparse/transformers_ud.py- transformers, numpy, torch, ufal (usesUnion[list[list[str]], str]syntax)augment/lm/wangchanberta.py- transformersaugment/lm/fasttext.py- gensimYour checklist for this pull request
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.