Skip to content

Fix type hints in unified_bridge with casts - #1493

Merged
Logiquo merged 1 commit into
sunlabuiuc:masterfrom
Logiquo:fix/unified-bridge-typehints
Nov 16, 2026
Merged

Logiquo merged 1 commit into
sunlabuiuc:masterfrom
Logiquo:fix/unified-bridge-typehints

Conversation

@Logiquo

@Logiquo Logiquo commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

pyright reported 13 errors in pyhealth/processors/unified_bridge.py. This change fixes all of them with casts. It does not change behavior.

  • self.legacy is typed as LegacyFeatureProcessor, so its processor-specific attributes were unknown. It is now cast to the processor each bridge wraps:
    • StageNet: StageNetProcessor (_is_nested)
    • Timeseries: TimeseriesProcessor (n_features, missing_indicators)
    • Tensor: TensorProcessor (missing_indicators)
  • TimeseriesProcessor.impute_strategy is a plain str. It is now cast to ImputeMethod for the level's impute.
  • TensorProcessor._shape items are int | None. The bridge has already ruled out None at that point, so they are cast to tuple[int, ...].
  • Each registered bridge class takes only the legacy processor, but its base class takes more arguments. In transform_processor, the bridge class is cast to Callable[[LegacyFeatureProcessor], UnifiedBridge].
  • transform_batch casts processors[key] to UnifiedBridge. It only does this for keys that passed the isinstance check.
  • StageNetToUnifiedBridge.transform_batch now builds its token tuple in each branch, so offsets can no longer be unbound. It returns the same tuples as before.

Testing: pyright reports 0 errors on the file. tests/**/test_unified_bridge.py and the module's doctests pass (123 passed).

Cast self.legacy to the concrete legacy processor where bridges read its
attributes, the impute strategy to ImputeMethod, the checked tensor shape
to tuple[int, ...], and the registered bridge class and converting
processors to their real types. Build StageNet's token tuple per branch so
offsets is never possibly unbound. pyright now reports no errors.
@Logiquo Logiquo added the status: automatic The PR will go through automatic review process. label Nov 16, 2026
@Logiquo

Logiquo commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator Author

/review

@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 83.63% (37321 / 44625 lines)

Modified file Coverage Lines
pyhealth/processors/unified_bridge.py 99.20% 374 / 377

Workflow run · b1e6c86

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Review

suggestion: approve
decision: approve

Thank you for this PR. It clears all 13 pyright errors in pyhealth/processors/unified_bridge.py with type-only casts and doesn't change behavior.

I checked each change against the diff:

  • Casts: Each cast matches the processor its bridge wraps. The ImputeMethod cast is only a type annotation.
  • StageNetToUnifiedBridge.transform_batch: Building head in each branch returns the same tuples as before and removes the possibly-unbound offsets.
  • transform_processor and transform_batch: The casts only inform the type checker.
  • Scope: The PR adds no new components, no links between components and no datasets, and it contains nothing unrelated.
  • Coverage: unified_bridge.py is at 99.2%, above the 80% bar for a changed file. The whole repository is at 83.63%, above the 60% bar.
  • Tests: The existing tests and doctests passed (123 passed), which is enough since behavior is unchanged.

It looks ready to merge.

Optional suggestion: you could type self.legacy once per bridge class, for example with a class-level annotation. That would avoid repeating the casts at each use site. This is only a style choice and isn't required.

Reviewed commit b1e6c86

@Logiquo
Logiquo merged commit df6df42 into sunlabuiuc:master Nov 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: automatic The PR will go through automatic review process.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant