Transform ID:Documentation Index
Fetch the complete documentation index at: https://docs.refactron.dev/llms.txt
Use this file to discover all available pages before exploring further.
manual_typecheck_to_hints
Language: Python
What it does
Detectsisinstance(x, T1) / elif isinstance(x, T2) / ... chains where every branch discriminates the same parameter x, then annotates the parameter with Union[T1, T2, ...] and adds from typing import Union if needed. The chain itself is preserved (the runtime check still runs); only the parameter signature is enriched.
Detector pattern
The detector atsrc/analyze/detectors/python/manual-typecheck.ts walks If / Elif chains looking for isinstance(<param>, <type>) shapes, groups them by parameter name, and surfaces a finding only when the discriminated parameter has no existing annotation — so every finding maps to a change the transform will actually make.
Preconditions
- The function has at least one parameter without an existing type annotation.
- Every
isinstancebranch in the chain discriminates the same single parameter. - The chain effectively dispatches the function body — i.e. the bare-
elseis either absent or raises. - The parameter is not already annotated.
- The chain references at least two distinct types (a single
isinstanceis not informative enough to be worth annotating).
Before / after
Edge cases handled
- Adds
from typing import Unionwhen not already imported. - Preserves the runtime
isinstancechain — purely additive in semantics. - Records satisfied preconditions as
annotated:<fn>:<param>for traceability.
Edge cases NOT handled (skip via precondition)
- Parameter is already annotated (
def handle(x: int)). - Chain discriminates more than one parameter (e.g.
isinstance(x, int)thenisinstance(y, str)). - Chain contains a single type only.
- The discriminated parameter doesn’t exist in the function signature (defensive check).