binaryninja-api
binaryninja-api copied to clipboard
Wrong python type annotation for TypeParserResult
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
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?
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.
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_structand_to_core_structmethods ofTypeParserResult? 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
BasicTypeParserResultinstead of aTypeParserResult?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