Skip to content

fix: type LEDPattern map_index callback as int - #329

Closed
Amidwestnoob wants to merge 1 commit into
robotpy:mainfrom
Amidwestnoob:fix/168-ledpattern-mapindex-typing
Closed

Amidwestnoob wants to merge 1 commit into
robotpy:mainfrom
Amidwestnoob:fix/168-ledpattern-mapindex-typing

Conversation

@Amidwestnoob

Copy link
Copy Markdown

Fixes #168

Problem

LEDPattern.map_index stubs typed the callback as
Callable[[SupportsInt | SupportsIndex, SupportsInt | SupportsIndex], int].
Callers pass (length: int, idx: int) -> int. Callback parameters are
contravariant, so Pylance/mypy reject valid mappers.

Fix

Bind MapIndex with py::typing::Callable<py::int_(py::int_, py::int_)> so
generated stubs emit Callable[[int, int], int]. Own the Python callable as
shared_ptr<py::function> with gil_scoped_acquire on invoke and destroy,
matching TelemetryRegistry::SetReportWarning, because apply_to uses
release_gil().

index_mapper rejects None (disable_none).

Tests

  • Fail-before pyright/mypy on published stubs (reportArgumentType / arg-type).
  • ./rdev.sh develop --stop-at robotpy-wpilib regenerated
    Callable[[int, int], int] at __init__.pyi line 10041.
  • Runtime: map_index(None) TypeError; callback survives return and is
    invoked from apply_to; reverse remap completes without crash.

Does not retarget other SupportsInt callback stubs.

Callers use (int, int) -> int. Own the callback via shared_ptr with
gil_scoped_acquire on destroy and invoke so apply_to release_gil is safe.

Fixes robotpy#168
@auscompgeek

Copy link
Copy Markdown
Member
  1. I don't think this is the right place to fix this.
  2. This lacks tests, and appears to be AI generated.

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.

[BUG]: mypy type errors in LEDPattern mapIndex

2 participants