Skip to content

Requests Helpers

Refer to the smp documentation for a complete description of each SMP Request and Response.

Generics and TypeGuards for SMP Requests and Responses.

TEr1 = TypeVar('TEr1', bound=smperror.ErrorV1) module-attribute

Type of SMP Error V1.

TEr2 = TypeVar('TEr2', bound=smperror.ErrorV2) module-attribute

Type of SMP Error V2.

TRep = TypeVar('TRep', bound=Union[smpmessage.ReadResponse, smpmessage.WriteResponse]) module-attribute

Type of successful SMP Response (ReadResponse or WriteResponse).

SMPRequest

Bases: Protocol[TRep, TEr1, TEr2]

A Protocol that groups the expected response and errors with a request.

To use, inherit from an SMP Read or Write Request and define its expected Response, ErrorV1, and ErrorV2.

Example:

class ImageStatesRead(smpimg.ImageStatesReadRequest):
    _Response = smpimg.ImageStatesReadResponse
    _ErrorV1 = smpimg.ImageManagementErrorV1
    _ErrorV2 = smpimg.ImageManagementErrorV2

Source code in smpclient/generics.py
class SMPRequest(Protocol[TRep, TEr1, TEr2]):
    """A `Protocol` that groups the expected response and errors with a request.

    To use, inherit from an SMP Read or Write `Request` and define its expected
    `Response`, `ErrorV1`, and `ErrorV2`.

    Example:
    ```python
    class ImageStatesRead(smpimg.ImageStatesReadRequest):
        _Response = smpimg.ImageStatesReadResponse
        _ErrorV1 = smpimg.ImageManagementErrorV1
        _ErrorV2 = smpimg.ImageManagementErrorV2
    ```
    """

    _Response: Type[TRep]
    _ErrorV1: Type[TEr1]
    _ErrorV2: Type[TEr2]

    @property
    def BYTES(self) -> bytes:  # pragma: no cover
        ...

    @property
    def header(self) -> smphdr.Header:  # pragma: no cover
        ...

BYTES: bytes property

header: smphdr.Header property

error_v1(response: smperror.ErrorV1 | TEr2 | TRep) -> TypeGuard[smperror.ErrorV1]

TypeGuard that returns True if the response is an ErrorV1.

Parameters:

Name Type Description Default
response ErrorV1 | TEr2 | TRep

The response to check.

required

Returns:

Type Description
TypeGuard[ErrorV1]

True if the response is an ErrorV1.

Source code in smpclient/generics.py
def error_v1(response: smperror.ErrorV1 | TEr2 | TRep) -> TypeGuard[smperror.ErrorV1]:
    """`TypeGuard` that returns `True` if the `response` is an `ErrorV1`.

    Args:
        response: The response to check.

    Returns:
        `True` if the `response` is an `ErrorV1`.
    """
    return response.RESPONSE_TYPE == smpmessage.ResponseType.ERROR_V1

error_v2(response: smperror.ErrorV1 | TEr2 | TRep) -> TypeGuard[TEr2]

TypeGuard that returns True if the response is an ErrorV2.

Parameters:

Name Type Description Default
response ErrorV1 | TEr2 | TRep

The response to check.

required

Returns:

Type Description
TypeGuard[TEr2]

True if the response is an ErrorV2.

Source code in smpclient/generics.py
def error_v2(response: smperror.ErrorV1 | TEr2 | TRep) -> TypeGuard[TEr2]:
    """`TypeGuard` that returns `True` if the `response` is an `ErrorV2`.

    Args:
        response: The response to check.

    Returns:
        `True` if the `response` is an `ErrorV2`.
    """
    return response.RESPONSE_TYPE == smpmessage.ResponseType.ERROR_V2

error(response: smperror.ErrorV1 | TEr2 | TRep) -> TypeGuard[smperror.ErrorV1 | TEr2]

TypeGuard that returns True if the response is an ErrorV1 or ErrorV2.

Parameters:

Name Type Description Default
response ErrorV1 | TEr2 | TRep

The response to check.

required

Returns:

Type Description
TypeGuard[ErrorV1 | TEr2]

True if the response is an ErrorV1 or ErrorV2.

Source code in smpclient/generics.py
def error(response: smperror.ErrorV1 | TEr2 | TRep) -> TypeGuard[smperror.ErrorV1 | TEr2]:
    """`TypeGuard` that returns `True` if the `response` is an `ErrorV1` or `ErrorV2`.

    Args:
        response: The response to check.

    Returns:
        `True` if the `response` is an `ErrorV1` or `ErrorV2`.
    """
    return error_v1(response) or error_v2(response)

success(response: smperror.ErrorV1 | TEr2 | TRep) -> TypeGuard[TRep]

TypeGuard that returns True if the response is a successful Response.

Parameters:

Name Type Description Default
response ErrorV1 | TEr2 | TRep

The response to check.

required

Returns:

Type Description
TypeGuard[TRep]

True if the response is a successful Response.

Source code in smpclient/generics.py
def success(response: smperror.ErrorV1 | TEr2 | TRep) -> TypeGuard[TRep]:
    """`TypeGuard` that returns `True` if the `response` is a successful `Response`.

    Args:
        response: The response to check.

    Returns:
        `True` if the `response` is a successful `Response`.
    """
    return response.RESPONSE_TYPE == smpmessage.ResponseType.SUCCESS