Vector35 / Vector35/binaryninja-api

Python traverse functions have wrong type annotations

Open
#7,495 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Python API Effort: Trivial Impact: Low
Dominant language
C++
Stars
1.3k
Forks
298
Avg merge
5d 5h
Merged PRs (30d)
19

Description

Version and Platform (required):

  • Binary Ninja Version: dev/5.2.8414

Bug Description:
The Python IL bindings have 6 traverse functions, with signatures like:

def traverse(self, cb: Callable[['HighLevelILInstruction', Any], Any], *args: Any, **kwargs: Any) -> Iterator[Any]:

As written, this expects the callback to take exactly two arguments, one of type HighLevelILInstruction and one of type Any. In reality, the callback is called with an arbitrary number of arguments: the instruction, plus whatever args and kwargs were passed to traverse. This causes spurious type errors in clients of the API if you pass a callback that takes a different number of arguments.

This can be properly typed with something like:

P = ParamSpec('P')
R = TypeVar('R')
def traverse(self, cb: Callable[Concatenate['HighLevelILInstruction', P], R], *args: P.args, **kwargs: P.kwargs) -> Iterator[R]:

This would work for 5 out of the 6 traverse functions, but not the one on HighLevelILInstruction, because it has an extra keyword-only argument shallow: bool = True which is not passed on to the callback. Unfortunately there is no way to express this situation with type annotations, so the arguments to traverse would still have to use Any. But it can at least be partly fixed:

P = ParamSpec('P')
R = TypeVar('R')
def traverse(self, cb: Callable[Concatenate['HighLevelILInstruction', P], R], *args: Any, shallow: bool = True, **kwargs: Any) -> Iterator[R]:

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the six Python IL traverse definitions in the bindings and compare their callback signatures, especially HighLevelILInstruction.traverse. Update the annotations according to the issue's proposed typing shapes, preserving shallow as a keyword-only argument where needed, and confirm callbacks with forwarded arguments no longer produce spurious type errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, developer-experience, reverse-engineering
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.