Replace Any type annotations with specific types and remove redundant type annotation reassignments - #1280
Conversation
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
…ctor Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
|
@copilot Do not use |
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Replaced all pipe union syntax (
Commit: d0d030e |
There was a problem hiding this comment.
Pull request overview
This pull request improves type safety by replacing 28+ Any type annotations with specific types across 16 files. The changes include using proper types from third-party libraries (transformers, fastai, nltk, epitran), creating type aliases for complex unions, and introducing a TypedDict for structured return values. The PR correctly uses Union[] and Optional[] syntax for Python 3.9 compatibility.
Changes:
- Replaced
Anywith specific types from third-party libraries (transformers.Pipeline, fastai.Learner/DataBunch, nltk.Synset, epitran.Epitran, argparse.Namespace) - Created
NEREngineTypetype alias for complex Union type in named entity recognition - Created
CorefResultDictTypedDict for structured coreference resolution results - Refined dictionary type annotations from
dict[str, Any]to more specificdict[str, Union[str, bool]]anddict[str, Union[int, str]] - Eliminated mypy errors across all affected modules (from 11-18 errors per module down to 0)
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pythainlp/word_vector/core.py | Changed tokenizer type from Any to Callable[[str], list[str]] |
| pythainlp/ulmfit/core.py | Added fastai types: Learner and DataBunch |
| pythainlp/transliterate/ipa.py | Changed epitran type from Any to epitran._epitran.Epitran |
| pythainlp/tag/unigram.py | Refined dict types from dict[str, Any] to dict[str, str] for POS taggers |
| pythainlp/tag/named_entity.py | Created NEREngineType type alias and updated engine types |
| pythainlp/tag/crfchunk.py | Refined feature dict type from dict[str, Any] to dict[str, Union[str, bool]] |
| pythainlp/summarize/keybert.py | Changed pipeline type from Any to transformers.pipelines.base.Pipeline |
| pythainlp/phayathaibert/core.py | Changed model type from Any to Pipeline |
| pythainlp/generate/thai2fit.py | Added Path and Learner types from fastai |
| pythainlp/coref/_fastcoref.py | Created CorefResultDict TypedDict and added CorefResult type |
| pythainlp/cli/tokenize.py | Changed args type from Any to argparse.Namespace |
| pythainlp/cli/tag.py | Changed args type from Any to argparse.Namespace |
| pythainlp/chat/core.py | Changed model type from Any to WangChanGLM |
| pythainlp/benchmarks/word_tokenization.py | Refined dict return types to dict[str, Union[int, str]] |
| pythainlp/augment/wordnet.py | Changed synset type from Any to nltk.corpus.reader.wordnet.Synset |
| build_tools/analysis/output/type_hint_analysis.json | Updated type analysis metrics showing elimination of mypy errors |
|
@copilot apply changes based on the comments in this thread |
- Add None to NEREngineType to match engine assignment - Remove redundant type annotation in NNER.__init__ - Use public epitran.Epitran instead of private _epitran module - Convert numpy integers to Python int for accurate type annotations Co-authored-by: bact <128572+bact@users.noreply.github.com>
Applied all suggested changes from the review:
Commit: d79928c |
The model attribute already has a type annotation at the class level (line 16), so the type annotation in the assignment (line 54) is redundant and should be removed per Python best practices. Co-authored-by: bact <128572+bact@users.noreply.github.com>
Fixed redundant type annotations in 8 files where attributes were declared at class level and then unnecessarily re-annotated during assignment in __init__ or other methods. This follows Python typing best practices and avoids annotation redundancy. Files modified: - pythainlp/spell/words_spelling_correction.py - pythainlp/transliterate/thai2rom.py - pythainlp/corpus/core.py - pythainlp/chat/core.py - pythainlp/classify/param_free.py - pythainlp/tag/wangchanberta_onnx.py - pythainlp/wangchanberta/core.py - pythainlp/tag/crfchunk.py Co-authored-by: bact <128572+bact@users.noreply.github.com>
Removed redundant type annotations from __init__ methods in: - Encoder, Attn, and AttentionDecoder classes in thai2rom.py - Words_Spelling_Correction class in words_spelling_correction.py All these classes already had type annotations at the class level, making the instance-level annotations redundant. Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|



What do these changes do
Removes redundant type annotations from instance-level assignments when type is already declared at class level. Affects 14 classes across 10 files with 60+ redundant annotations eliminated.
What was wrong
Type annotations were duplicated at both class and instance levels, violating PEP 484 best practices:
This pattern appeared in:
FastTextEncoder,Words_Spelling_Correction(words_spelling_correction.py)ThaiTransliterator,Encoder,Attn,AttentionDecoder(thai2rom.py)_ResponseWrapper(corpus/core.py)ChatBotModel(chat/core.py)GzipModel(classify/param_free.py)WngchanBerta_ONNX(tag/wangchanberta_onnx.py)ThaiNameTagger(wangchanberta/core.py)CRFchunk(tag/crfchunk.py)How this fixes it
Keeps type annotation only at class level, removes from instance assignments:
Verified with mypy - no new type errors introduced.
Your 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.