Skip to content

Commit

Permalink
Merge pull request #16 from remopas/dev
Browse files Browse the repository at this point in the history
fix class format again
  • Loading branch information
remkop22 authored May 19, 2022
2 parents 5a88a88 + fc15bbd commit 9fa942e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/binread/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,14 @@ def __init__(self, cls: type, *args, **kwargs):
super().__init__(*args, **kwargs)
self._cls = cls

def extract(self, data: bytes, fields: Dict[str, Any]) -> Tuple[Any, int]:
value, bytes_read = super().extract(data, fields)
return self._cls(**value), bytes_read
def read(self, *args, **kwargs) -> Dict[str, Any]:
result = super().read(*args, **kwargs)

if isinstance(result, tuple):
result, bytes_read = result
return self._cls(**result), bytes_read # type: ignore
else:
return self._cls(**result)


def formatclass(*args, **kwargs):
Expand Down
3 changes: 2 additions & 1 deletion tests/binread/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class FormatExample:
field1 = binread.U8
field2 = binread.U16

FormatExample.read(b"\x00\xFF\x00")
value = FormatExample.read(b"\x00\xFF\x00")
self.assertEqual(type(value), FormatExample)

0 comments on commit 9fa942e

Please sign in to comment.