Coverage for dataclasses_struct/types.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-15 09:30 +1200

1from typing import Annotated 

2 

3from . import field 

4 

5# Single char type 

6Char = bytes 

7 

8# Boolean type 

9Bool = Annotated[bool, field.BoolField()] 

10 

11# Standard integer types 

12I8 = Annotated[int, field.SignedStdIntField(1)] 

13U8 = Annotated[int, field.UnsignedStdIntField(1)] 

14I16 = Annotated[int, field.SignedStdIntField(2)] 

15U16 = Annotated[int, field.UnsignedStdIntField(2)] 

16I32 = Annotated[int, field.SignedStdIntField(4)] 

17U32 = Annotated[int, field.UnsignedStdIntField(4)] 

18I64 = Annotated[int, field.SignedStdIntField(8)] 

19U64 = Annotated[int, field.UnsignedStdIntField(8)] 

20 

21# Native integer types 

22SignedChar = Annotated[int, field.NativeIntField("b", "byte")] 

23UnsignedChar = Annotated[int, field.NativeIntField("B", "ubyte")] 

24Short = Annotated[int, field.NativeIntField("h", "short")] 

25UnsignedShort = Annotated[int, field.NativeIntField("H", "ushort")] 

26Int = Annotated[int, field.NativeIntField("i", "int")] 

27UnsignedInt = Annotated[int, field.NativeIntField("I", "uint")] 

28Long = Annotated[int, field.NativeIntField("l", "long")] 

29UnsignedLong = Annotated[int, field.NativeIntField("L", "ulong")] 

30LongLong = Annotated[int, field.NativeIntField("q", "longlong")] 

31UnsignedLongLong = Annotated[int, field.NativeIntField("Q", "ulonglong")] 

32 

33# Native size types 

34UnsignedSize = Annotated[int, field.SizeField(signed=False)] 

35SignedSize = Annotated[int, field.SizeField(signed=True)] 

36 

37# Native pointer types 

38Pointer = Annotated[int, field.PointerField()] 

39 

40# Floating point types 

41F16 = Annotated[float, field.FloatingPointField("e")] 

42F32 = Annotated[float, field.FloatingPointField("f")] 

43F64 = Annotated[float, field.FloatingPointField("d")] 

44 

45 

46class _Padding: 

47 before: bool 

48 

49 def __init__(self, size: int): 

50 if not isinstance(size, int) or size < 0: 

51 raise ValueError("padding size must be non-negative int") 

52 self.size = size 

53 

54 def __repr__(self) -> str: 

55 return f"{type(self).__name__}({self.size})" 

56 

57 

58class PadBefore(_Padding): 

59 before = True 

60 

61 def __init__(self, size: int): 

62 super().__init__(size) 

63 

64 

65class PadAfter(_Padding): 

66 before = False 

67 

68 def __init__(self, size: int): 

69 super().__init__(size)