Skip to content

Fix type hint mismatches with None defaults throughout codebase - #1190

Merged
bact merged 6 commits into
devfrom
copilot/fix-str-default-parameter-usage
Jan 11, 2026
Merged

Fix type hint mismatches with None defaults throughout codebase#1190
bact merged 6 commits into
devfrom
copilot/fix-str-default-parameter-usage

Conversation

Copilot AI commented Jan 10, 2026

Copy link
Copy Markdown
Contributor

What does this changes

Comprehensively fixes type hint inconsistencies where parameter defaults are None but type hints don't include None. Changes are made in two phases:

Phase 1: Empty String Conversion (4 parameters)

  • Replaces str = None with str = "" where empty string is semantically equivalent
  • Updates conditional checks and docstrings accordingly

Phase 2: Optional Type Hints (11 parameters)

  • Adds proper Type | None syntax using Python 3.9+ union notation
  • Covers str, bool, int, and list types where None has semantic meaning

All changed parameters:

  • pythainlp.summarize.mt5.mT5Summarizer.__init__(pretrained_mt5_model_name) - converted to str = ""
  • pythainlp.generate.wangchanglm.WangChanGLM.instruct_generate(context) - converted to str = ""
  • pythainlp.classify.param_free.GzipModel.__init__(model_path) - converted to str = ""
  • pythainlp.classify.param_free.GzipModel.__init__(training_data) - converted to list[tuple[str, str]] | None
  • pythainlp.corpus.core.get_hf_hub(filename) - converted to str = ""
  • pythainlp.transliterate.wunsen.WunsenTransliterate.transliterate(jp_input, zh_sandhi, system) - 3 params with Optional
  • pythainlp.util.date.thai_strptime(add_year) - converted to int | None
  • pythainlp.augment.wordnet.WordNetAug.find_synonyms(pos) - converted to str | None
  • pythainlp.corpus.wordnet - 5 functions (synsets, all_lemma_names, all_synsets, lemmas, morphy) with pos: str | None

Example:

# Phase 1: Empty string conversion
# Before
def __init__(self, model_path: str = None):
    if model_path is not None:
        self.load(model_path)

# After
def __init__(self, model_path: str = ""):
    if model_path:
        self.load(model_path)

# Phase 2: Optional type hints
# Before
def synsets(word: str, pos: str = None, lang: str = "tha"):
    return wordnet.synsets(lemma=word, pos=pos, lang=lang)

# After
def synsets(word: str, pos: str | None = None, lang: str = "tha"):
    return wordnet.synsets(lemma=word, pos=pos, lang=lang)

What was wrong

Type hints specified concrete types (e.g., str, int, bool, list) but defaults were None, creating type inconsistency. Python 3.9+ supports proper type annotations; using None for optional parameters requires Type | None union syntax.

How this fixes it

  • Phase 1: Changes defaults from None to "" where semantically appropriate (empty string is equivalent to None)
  • Phase 2: Adds proper Type | None union syntax for parameters where None has distinct semantic meaning
  • Updates conditional checks: is Nonenot x, is not Noneif x, in (None, "")not x
  • Updates all docstrings to document default values
  • Uses Python 3.9+ union syntax (Type | None) as all files have from __future__ import annotations

Fully backward compatible: empty string evaluates to False identically to None, and Optional type hints don't change runtime behavior.

Your checklist for this pull request

  • Passed code styles and structures
  • Passed code linting checks and unit test
Original prompt

Fix inappropriate use of "str = None" defaults in function parameters.

  • Scan the codebase and find the inappropriate use of defaults that do not match the type of parameter
  • Use str = "" instead of str = None when possible. In that case, recheck relevant values in the function. Adjust comparison accordingly. For example, replace "if x is not None" with "if x" as appropriate.
  • Target Python version is 3.9

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: bact <128572+bact@users.noreply.github.com>
@bact bact added the refactoring a technical improvement which does not add any new features or change existing features. label Jan 10, 2026
@bact
bact marked this pull request as ready for review January 10, 2026 23:05
@bact
bact requested a review from wannaphong January 10, 2026 23:05
Co-authored-by: bact <128572+bact@users.noreply.github.com>
@coveralls

coveralls commented Jan 10, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 52.791% (-0.09%) from 52.878%
when pulling e796081 on copilot/fix-str-default-parameter-usage
into da8c3b6 on dev.

Copilot AI changed the title [WIP] Fix inappropriate default usage of str = None in functions Fix inappropriate str = None defaults in function parameters Jan 10, 2026
Copilot AI requested a review from bact January 10, 2026 23:07
@bact

bact commented Jan 10, 2026

Copy link
Copy Markdown
Member

@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.

Copilot AI and others added 2 commits January 10, 2026 23:30
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Co-authored-by: bact <128572+bact@users.noreply.github.com>

Copilot AI commented Jan 10, 2026

Copy link
Copy Markdown
Contributor Author

@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 f...

Done! Converted all None defaults that don't match type hints to use Optional[] with Python 3.9+ union syntax (Type | None).

Fixed in commits:

  • 7e77fe2: Added Optional type hints for 10 parameters across 4 files (transliterate/wunsen.py, util/date.py, augment/wordnet.py, corpus/wordnet.py)
  • e7f140a: Fixed training_data list type hint in classify/param_free.py

All docstrings updated to document default values. Tested thai_strptime to confirm behavior unchanged. No remaining type mismatches found in codebase.

Copilot AI changed the title Fix inappropriate str = None defaults in function parameters Fix type hint mismatches with None defaults throughout codebase Jan 10, 2026
@sonarqubecloud

Copy link
Copy Markdown

@bact
bact merged commit 0beadd8 into dev Jan 11, 2026
42 checks passed
@bact
bact deleted the copilot/fix-str-default-parameter-usage branch January 11, 2026 01:10
@bact bact mentioned this pull request Jan 11, 2026
@bact bact added this to the 5.3 milestone Jan 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring a technical improvement which does not add any new features or change existing features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants