# Automatically generated by pb2py
# fmt: off
# isort:skip_file

from trezor import protobuf

from typing import Any, TYPE_CHECKING


def __getattr__(name: str) -> Any:
    try:
        return protobuf.type_for_name(name)
    except ValueError:
        raise AttributeError(name)


if TYPE_CHECKING:
    from typing import TypeGuard
% for enum in sorted(enums, key=lambda e: e.name):
    from trezor.enums import ${enum.name}  # noqa: F401
% endfor
% for message in messages:

<%
required_fields = [f for f in message.fields if f.required]
repeated_fields = [f for f in message.fields if f.repeated]
optional_fields = [f for f in message.fields if f.optional]

def member_type(field):
    if field.required:
        return field.python_type
    if field.optional and field.default_value is not None:
        return field.python_type
    if field.repeated:
        return f"list[{field.python_type}]"
    return f"{field.python_type} | None"

%>\
    class ${message.name}(protobuf.MessageType):
% if message.fields:
% for field in message.fields:
        ${field.name}: "${member_type(field)}"
% endfor

        def __init__(
            self,
            *,
% for field in required_fields:
            ${field.name}: "${field.python_type}",
% endfor
% for field in repeated_fields:
            ${field.name}: "list[${field.python_type}] | None" = None,
% endfor
% for field in optional_fields:
            ${field.name}: "${field.python_type} | None" = None,
% endfor
        ) -> None:
            pass
% endif  # if fields

        @classmethod
        def is_type_of(cls, msg: Any) -> TypeGuard["${message.name}"]:
            return isinstance(msg, cls)
% endfor
