googleapis / proto-plus-python

Beautiful, idiomatic protocol buffers in Python
Apache License 2.0
169 stars 35 forks source link

Bug in _package_info.py compile #491

Open aurelien78 opened 1 month ago

aurelien78 commented 1 month ago

On version 1.24.0, in _package_info.py compile() methode line 36 there is :

module_name = module.__name__ if hasattr(module, __name__) else ""

I think it should be :

module_name = module.__name__ if hasattr(module, "__name__") else ""

Furthermore, if we are dealing with packages, I think the whole code should be :

    # Pull a reference to the module where this class is being
    # declared.
    module = sys.modules.get(attrs.get("__module__"))
    module_name = module.__name__ if hasattr(module, "__name__") else ""
    if '.' in module_name:
        parts = module_name.split('.')
        package_name =  '.'.join(parts[:-1])
    else:
        package_name = module_name
    proto_module = getattr(module, "__protobuf__", object())

    # A package should be present; get the marshal from there.
    # TODO: Revert to empty string as a package value after protobuf fix.
    # When package is empty, upb based protobuf fails with an
    # "TypeError: Couldn't build proto file into descriptor pool: invalid name: empty part ()' means"
    # during an attempt to add to descriptor pool.
    package = getattr(
        proto_module, "package", package_name if package_name else "_default_package"
    )
    marshal = Marshal(name=getattr(proto_module, "marshal", package))

    # Done; return the data.
    return (package, marshal)