Skip to content

refactor: enhance type hints in diagnosis.py#24

Merged
alingse merged 1 commit intomasterfrom
feature/type-hint-improvements
Feb 20, 2026
Merged

refactor: enhance type hints in diagnosis.py#24
alingse merged 1 commit intomasterfrom
feature/type-hint-improvements

Conversation

@alingse
Copy link
Member

@alingse alingse commented Feb 20, 2026

Summary

  • Replace Any with proper recursive JSONValue type definition
  • Use cast() instead of type: ignore for type assertions
  • Add Protocol classes for better type safety
  • Improve function signatures with TypeVar
  • Reduce type: ignore comments from 7 to 2

Details

Changes Made

  1. JSONValue Type Definition

    • Changed from JSONValue = Any to a proper recursive type:
    JSONValue = Union[
        dict[str, "JSONValue"],
        list["JSONValue"],
        str, int, float, bool, None,
    ]
  2. Protocol Classes Added

    • _ParseError: Define exception interface
    • _ExtendedDecoder: Define extended JSONDecoder interface
    • _JsonDecoderModule: Define json.decoder module interface
  3. Type Assertions

    • Use cast(_ExtendedDecoder, ...) instead of # type: ignore[assignment]
    • Use cast(_ParseError, e) for exception handling
  4. Function Signatures

    • _record_parser_name: Now uses TypeVar for better type inference

Test Plan

  • mypy checks pass
  • ruff checks pass
  • All 108 pytest tests pass

Notes

The remaining 2 type: ignore comments are necessary for CPython internal
functions (py_scanstring, py_make_scanner) not exposed in typeshed stubs.

- Replace Any with proper recursive JSONValue type definition
- Use cast() instead of type: ignore for type assertions
- Add Protocol classes for better type safety
- Improve function signatures with TypeVar
- Reduce type: ignore comments from 7 to 2

The remaining 2 type: ignore comments are necessary for
CPython internal functions not exposed in typeshed stubs.
@alingse alingse merged commit d25f97f into master Feb 20, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant