Skip to content

Question about the MessageMeta.to_dict() function return type annotated as Message #489

@brendan-corrigan

Description

@brendan-corrigan

Currently calling Message.to_dict(my_message) and it is correctly returning a dictionary. However the my type checker is throwing errors downstream because it thinks it is a Message object instead of a python dict.

After looking through the source i found that the to_dict return type annotation is a Message:

def to_dict(
    cls,
    instance,
    *,
    use_integers_for_enums=True,
    preserving_proto_field_name=True,
    including_default_value_fields=None,
    float_precision=None,
    always_print_fields_with_no_presence=None,
) -> "Message":
        """Given a message instance, return its representation as a python dict.

However, the to_json method it is correctly typed as a str:

def to_json(
    cls,
    instance,
    *,
    use_integers_for_enums=True,
    including_default_value_fields=None,
    preserving_proto_field_name=False,
    sort_keys=False,
    indent=2,
    float_precision=None,
    always_print_fields_with_no_presence=None,
) -> str:

Wondering if this is the intended implementation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p3Desirable enhancement or fix. May not be included in next release.type: questionRequest for information or clarification. Not an issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions