Skip to content

improve typing for uctypes.struct #829

@Josverl

Description

@Josverl

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,
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestreferencereference stubs for micropython

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions