Fix type hint mismatches with None defaults throughout codebase - #1190
Conversation
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
str = None defaults in function parameters
|
@copilot check further to other cases of None defaults that do not match type hints. Convert them to Optional[] when possible. Test if results are the same. Update docstring. Target Python 3.9, with future imports. |
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Done! Converted all None defaults that don't match type hints to use Optional[] with Python 3.9+ union syntax ( Fixed in commits:
All docstrings updated to document default values. Tested thai_strptime to confirm behavior unchanged. No remaining type mismatches found in codebase. |
str = None defaults in function parameters
|



What does this changes
Comprehensively fixes type hint inconsistencies where parameter defaults are
Nonebut type hints don't includeNone. Changes are made in two phases:Phase 1: Empty String Conversion (4 parameters)
str = Nonewithstr = ""where empty string is semantically equivalentPhase 2: Optional Type Hints (11 parameters)
Type | Nonesyntax using Python 3.9+ union notationstr,bool,int, andlisttypes whereNonehas semantic meaningAll changed parameters:
pythainlp.summarize.mt5.mT5Summarizer.__init__(pretrained_mt5_model_name)- converted tostr = ""pythainlp.generate.wangchanglm.WangChanGLM.instruct_generate(context)- converted tostr = ""pythainlp.classify.param_free.GzipModel.__init__(model_path)- converted tostr = ""pythainlp.classify.param_free.GzipModel.__init__(training_data)- converted tolist[tuple[str, str]] | Nonepythainlp.corpus.core.get_hf_hub(filename)- converted tostr = ""pythainlp.transliterate.wunsen.WunsenTransliterate.transliterate(jp_input, zh_sandhi, system)- 3 params with Optionalpythainlp.util.date.thai_strptime(add_year)- converted toint | Nonepythainlp.augment.wordnet.WordNetAug.find_synonyms(pos)- converted tostr | Nonepythainlp.corpus.wordnet- 5 functions (synsets,all_lemma_names,all_synsets,lemmas,morphy) withpos: str | NoneExample:
What was wrong
Type hints specified concrete types (e.g.,
str,int,bool,list) but defaults wereNone, creating type inconsistency. Python 3.9+ supports proper type annotations; usingNonefor optional parameters requiresType | Noneunion syntax.How this fixes it
Noneto""where semantically appropriate (empty string is equivalent to None)Type | Noneunion syntax for parameters whereNonehas distinct semantic meaningis None→not x,is not None→if x,in (None, "")→not xType | None) as all files havefrom __future__ import annotationsFully backward compatible: empty string evaluates to
Falseidentically toNone, and Optional type hints don't change runtime behavior.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.