Skip to content

Add type hints to core test suite functions - #1265

Merged
bact merged 5 commits into
devfrom
copilot/add-type-hints-to-submodules-one-more-time
Feb 3, 2026
Merged

Add type hints to core test suite functions#1265
bact merged 5 commits into
devfrom
copilot/add-type-hints-to-submodules-one-more-time

Conversation

Copilot AI commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

What do these changes do

Adds type hints to functions tested in the core test suite. Fixes 35+ mypy errors across 8 files while maintaining Python 3.9 compatibility.

What was wrong

Core-tested functions lacked type annotations, causing mypy validation failures:

  • corpus/core.py: JSON return types declared as dict[str, str] but actual structure is dict[str, Any]
  • corpus/common.py: Missing type annotation for list_synonym, incorrect Union type handling
  • transliterate/core.py: Incorrect/unused type: ignore comments
  • tag/unigram.py: json.load() returns Any but functions declared dict return type
  • util/spell_words.py: Module-level list comprehensions had no type annotations
  • tag/named_entity.py: Outdated type: ignore annotations
  • transliterate/lookup.py: Dictionary access returning Union[str, bool, None] incompatible with function return types

How this fixes it

Type system fixes:

  • Use dict[str, Any] for nested JSON structures
  • Add explicit type annotations to module-level variables
  • Add type: ignore[no-any-return] for external library returns
  • Use isinstance checks instead of assertions for runtime type guards
  • Separate variables for different types (temptemp_parts for list type)

Guidelines applied:

  • Native types (list, tuple) not typing.List, typing.Tuple
  • Union[] syntax instead of | for Python 3.9 compatibility
  • TYPE_CHECKING imports to avoid circular dependencies
  • Specific error codes in type: ignore comments

Modified files:

  1. corpus/core.py - 10 errors fixed
  2. corpus/common.py - 3 errors fixed
  3. transliterate/core.py - 8 errors fixed
  4. tag/unigram.py - 5 errors fixed
  5. util/spell_words.py - 3 errors fixed
  6. tag/named_entity.py - 4 errors fixed
  7. transliterate/lookup.py - 2 errors fixed

Your checklist for this pull request

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

Gradually add type hints to submodules,
based on works done in these PRs:
#1262
#1263
#1264

  • Start with small functions and functions that require few external dependencies

  • Moving gradually one function by one function, in the four test suites: starting from "core", then "compact", "extra", and "noauto"

  • Immediate goal for this session - 100% type hints for functions being called in "core" test suite.

  • Ultimate goal (outside of this session) is to make the package a typed package, while keeping maintainability.

  • Start small in the area with high confidence, then gradually grow one submodule at a time.

  • Use mypy as main assistant.

    • mypy is already in "dev" extra dependencies in pyproject.toml
    • mypy configuration is in pyproject.toml
  • Use pyright, pyrefly, and pytype for second opinions.

  • Start with highly tested submodules first.

  • Use native type in type hint (list, tuple, .. instead of List, Tuple, ..)

  • Do not use A | B style Union, use full Union[] instead.

  • We want to keep compatibility with Python 3.9 strict and allow type inspection tools to work correctly.

  • When insert typing imports, put it in appropriate location and order

  • Use "if TYPE_CHECKING import" block when possible.

  • When reordering the imports, beware of circular import. Read comments near imports to get more information.

  • Remove unused imports

  • Try to fix the type, the scope logic, or do the casting, before putting #type: ignore comment.

  • When dealing with types from external libraries:

    • Check if type information is provided (maybe through stubs). If it is, cast wrapper may not necessary.
    • If return type is not provided, may be it is appropriate to use # type: ignore[no-any-return] annotation for clarity.
  • Recheck necessity when do casting.

  • Provide stubs as necessary.

  • After changes for type hints, recheck for functionality correctness.

  • Recheck code, docstring, documentation consistency.

  • Recheck formatting with ruff


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

Copilot AI assigned Copilot and bact Feb 3, 2026
@bact bact added the refactoring a technical improvement which does not add any new features or change existing features. label Feb 3, 2026
@bact bact added this to the 5.3 milestone Feb 3, 2026
Copilot AI and others added 3 commits February 3, 2026 09:29
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>
@bact
bact marked this pull request as ready for review February 3, 2026 09:40
Co-authored-by: bact <128572+bact@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Feb 3, 2026

Copy link
Copy Markdown

Copilot AI changed the title [WIP] Add type hints to submodules incrementally Add type hints to core test suite functions Feb 3, 2026
Copilot AI requested a review from bact February 3, 2026 09:48
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 65.761% (+0.008%) from 65.753%
when pulling d09c7f9 on copilot/add-type-hints-to-submodules-one-more-time
into 7db158d on dev.

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