binaryninja-api icon indicating copy to clipboard operation
binaryninja-api copied to clipboard

Wrong python type annotation for TypeParserResult

Open waskyo opened this issue 6 months ago • 3 comments

Version and Platform (required):

  • Binary Ninja Version: 5.1.7422-dev Ultimate (84083554)
  • OS: Debian Linux
  • OS Version: unstable
  • CPU Architecture: x86_64

Bug Description: The type annotations for TypeParserResult do not match what I get at runtime. According to: https://github.com/Vector35/binaryninja-api/blob/7034620653386995816157558520033738480413/python/typeparser.py#L139 types, variables, and functions are lists, but in reality they're dicts. The annotations for BasicTypeParserResult at https://github.com/Vector35/binaryninja-api/blob/7034620653386995816157558520033738480413/python/typeparser.py#L127 do match reality.

This causes mypy and pyright to issue complains when I try to access the TypeParserResult fields:

error: "List[ParsedType]" has no attribute "keys"

Steps To Reproduce:

I don't have a small enough script to repro, but you can see the mismtach in the UI python console:

>>> t = bv.parse_types_from_string("static const uint32_t test;", options=["-x", "c", "--std", "c99"])
>>> type(t)
<class 'binaryninja.typeparser.TypeParserResult'>
>>> type(t.types)
<class 'dict'>
>>> type(t.variables)
<class 'dict'>
>>> type(t.functions)
<class 'dict'>

I can put together a script if that would be useful.

Expected Behavior:

No complains from mypy/pyright with code such as:

t = bv.parse_types_from_string("static const uint32_t test;", options=["-x", "c", "--std", "c99"])
if 'test' in t.keys():
   print("it works")

Screenshots/Video Recording: N/A

Binary: N/A

Additional Information: N/A

waskyo avatar May 14 '25 14:05 waskyo

Thanks for the bug report! It seems you have already looked into it quite a bit, would you like to submit a PR to fix it?

xusheng6 avatar May 19 '25 04:05 xusheng6

Happy to give it a try.

One thing that I'm not sure about and could use some guidance: what to do with the _from_core_struct and _to_core_struct methods of TypeParserResult? They treat the attributes I mentioned as lists, and they're called by other parts of the python API, so it seems like changing that code to produce dicts could break something?

Or maybe the bug fix is to have bv.parse_types_from_string ( https://github.com/Vector35/binaryninja-api/blob/7034620653386995816157558520033738480413/python/binaryview.py#L7698 ) return a BasicTypeParserResult instead of a TypeParserResult?

Let me know what you think and I can put together a PR.

waskyo avatar May 20 '25 03:05 waskyo

Happy to give it a try.

One thing that I'm not sure about and could use some guidance: what to do with the _from_core_struct and _to_core_struct methods of TypeParserResult? They treat the attributes I mentioned as lists, and they're called by other parts of the python API, so it seems like changing that code to produce dicts could break something?

Or maybe the bug fix is to have bv.parse_types_from_string (

binaryninja-api/python/binaryview.py

Line 7698 in 7034620 def parse_types_from_string(self, text: str, options: Optional[List[str]] = None, include_dirs: Optional[List[str]] = None, import_dependencies: bool = True) -> '_types.TypeParserResult': ) return a BasicTypeParserResult instead of a TypeParserResult?

Let me know what you think and I can put together a PR.

Thanks for looking into this! I see your confusion, but I am not sure what is the intended fix for it. I will bring this up with the team

xusheng6 avatar Jun 11 '25 03:06 xusheng6