micropython-stubs icon indicating copy to clipboard operation
micropython-stubs copied to clipboard

improve typing for uctypes.struct

Open Josverl opened this issue 3 months ago • 0 comments

improve typing of uctypes.struct in https://github.com/Josverl/micropython-stubs/blob/main/reference/micropython/uctypes/init.pyi#L24-L40

The typing borrowed from CPython does not work

# _ScalarProperty: TypeAlias = int
# _RecursiveProperty: TypeAlias = tuple[int, _property]
# _ArrayProperty: TypeAlias = tuple[int, int]
# _ArrayOfAggregateProperty: TypeAlias = tuple[int, int, _property]
# _PointerToAPrimitiveProperty: TypeAlias = tuple[int, int]
# _PointerToAaAggregateProperty: TypeAlias = tuple[int, "_property"]
# _BitfieldProperty: TypeAlias = int
# _property: TypeAlias = (
#     _ScalarProperty
#     | _RecursiveProperty
#     | _ArrayProperty
#     | _ArrayOfAggregateProperty
#     | _PointerToAPrimitiveProperty
#     | _PointerToAaAggregateProperty
#     | _BitfieldProperty
# )

_property: TypeAlias = Incomplete
#------------------------------------------------------------------------------------
# Example 1: Subset of ELF file header
# https://wikipedia.org/wiki/Executable_and_Linkable_Format#File_header
ELF_HEADER = {
    "EI_MAG": (0x0 | uctypes.ARRAY, 4 | uctypes.UINT8),
    "EI_DATA": 0x5 | uctypes.UINT8,
    "e_machine": 0x12 | uctypes.UINT16,
}

# "f" is an ELF file opened in binary mode
buf = f.read(uctypes.sizeof(ELF_HEADER, uctypes.LITTLE_ENDIAN))
header = uctypes.struct(
    uctypes.addressof(buf),
    ELF_HEADER,
    uctypes.LITTLE_ENDIAN,
)


Josverl avatar Aug 29 '25 07:08 Josverl