33from __future__ import annotations
44
55import struct
6- from collections .abc import ItemsView , KeysView , ValuesView
7- from typing import BinaryIO , Optional , Union
6+ from typing import TYPE_CHECKING , BinaryIO
87
98from dissect .util .stream import RangeStream
109
1615)
1716from dissect .hypervisor .exceptions import InvalidSignature
1817
18+ if TYPE_CHECKING :
19+ from collections .abc import ItemsView , KeysView , ValuesView
20+
1921
2022class HyperVFile :
2123 """HyperVFile implementation.
@@ -278,7 +280,7 @@ def __repr__(self) -> str:
278280 return f"<HyperVStorageKeyTableEntry type={ self .type } size={ self .size } >"
279281
280282 @property
281- def parent (self ) -> Optional [ HyperVStorageKeyTableEntry ] :
283+ def parent (self ) -> HyperVStorageKeyTableEntry | None :
282284 """Return the entry parent, if there is any.
283285
284286 Requires that all key tables are loaded.
@@ -333,8 +335,8 @@ def data(self) -> memoryview:
333335 file_object = self .get_file_object ()
334336 # This memoryview has no purpose, only do it so the return value type is consistent
335337 return memoryview (file_object .read (size ))
336- else :
337- return self .raw [self .header .data_offset :]
338+
339+ return self .raw [self .header .data_offset :]
338340
339341 @property
340342 def key (self ) -> str :
@@ -343,7 +345,7 @@ def key(self) -> str:
343345 return self .raw .tobytes ()[: self .header .data_offset - 1 ].decode ("utf-8" )
344346
345347 @property
346- def value (self ) -> Union [ int , bytes , str ] :
348+ def value (self ) -> int | bytes | str :
347349 """Return a Python native value for this entry."""
348350 data = self .data
349351
@@ -369,6 +371,8 @@ def value(self) -> Union[int, bytes, str]:
369371 if self .type == KeyDataType .Bool :
370372 return struct .unpack ("<I" , data [:4 ])[0 ] != 0
371373
374+ raise TypeError (f"Unknown data type: { self .type } " )
375+
372376 @property
373377 def data_size (self ) -> int :
374378 """Return the total amount of data bytes, including the key name.
@@ -427,10 +431,7 @@ def as_dict(self) -> dict:
427431
428432 obj = {}
429433 for key , child in self .children .items ():
430- if child .type == KeyDataType .Node :
431- value = child .as_dict ()
432- else :
433- value = child .value
434+ value = child .as_dict () if child .type == KeyDataType .Node else child .value
434435
435436 obj [key ] = value
436437
@@ -466,13 +467,10 @@ def read(self, n: int = -1) -> bytes:
466467 if n is not None and n < - 1 :
467468 raise ValueError ("invalid number of bytes to read" )
468469
469- if n == - 1 :
470- read_length = self .size
471- else :
472- read_length = min (n , self .size )
470+ read_length = self .size if n == - 1 else min (n , self .size )
473471
474472 self .file .fh .seek (self .offset )
475473 return self .file .fh .read (read_length )
476474
477- def open (self , size : Optional [ int ] = None ) -> BinaryIO :
475+ def open (self , size : int | None = None ) -> BinaryIO :
478476 return RangeStream (self .file .fh , self .offset , size or self .size )
0 commit comments