Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(python): Potential breaking change in protobuf 5.27.0rc1 where MessageMapContainer cannot be imported #16596

Open
parthea opened this issue Apr 22, 2024 · 1 comment
Assignees

Comments

@parthea
Copy link

parthea commented Apr 22, 2024

What version of protobuf and what language are you using?
Version: main/v3.6.0/v3.5.0 etc. (NOTE: please try updating to the latest version of protoc/runtime possible beforehand to attempt to resolve your problem)
Language: C++/Java/Python/C#/Ruby/PHP/Objective-C/Javascript
Python Protobuf 5.27.0rc1

What operating system (Linux, Windows, ...) and version?
Debian GNU/Linux rodete
6.6.15-2rodete2-amd64

What runtime / compiler are you using (e.g., python version or gcc version)
Python 3.9.16
gcc version 13.2.0 (Debian 13.2.0-10)

What did you do?
Steps to reproduce the behavior:

  1. Create foo.proto
syntax = "proto3";

message Foo {
  int32 bar = 1;
}

message Baz {
    map<string, Foo> foos = 1;
}
  1. Compile proto using protoc --python_out=. foo.proto
  2. Run the following python code and observe that the type is <class 'google._upb._message.MessageMapContainer'>
import foo_pb2
foo = foo_pb2.Foo(bar=5)
type(foo_pb2.Baz(foos = {"i": foo}).foos)
>>> import foo_pb2
>>> foo = foo_pb2.Foo(bar=5)
>>> type(foo_pb2.Baz(foos = {"i": foo}).foos)
<class 'google._upb._message.MessageMapContainer'>
  1. Run the code below which results in ImportError
    from google._upb._message import MessageMapContainer

What did you expect to see
I expected from google._upb._message import MessageMapContainer to succeed as it works in 5.26.1

(py392) partheniou@partheniou-vm-3:$ python3
Python 3.9.16 (main, Mar  2 2023, 17:52:22) 
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from google._upb._message import MessageMapContainer
>>> import google.protobuf
>>> google.protobuf.__version__
'5.26.1'
(py392) partheniou@partheniou-vm-3:~$ python3
Python 3.9.16 (main, Mar  2 2023, 17:52:22) 
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from google._upb._message import MessageMapContainer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'MessageMapContainer' from 'google._upb._message' (/usr/local/google/home/partheniou/.pyenv/versions/3.9.16/envs/py392/lib/python3.9/site-packages/google/_upb/_message.abi3.so)
>>> import google.protobuf
>>> google.protobuf.__version__
'5.27.0rc1'

What did you see instead?

>>> from google._upb._message import MessageMapContainer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'MessageMapContainer' from 'google._upb._message' (/usr/local/google/home/partheniou/.pyenv/versions/3.9.16/envs/py392/lib/python3.9/site-packages/google/_upb/_message.abi3.so)

Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).

Anything else we should know about your project / environment

@parthea parthea added the untriaged auto added to all issues by default when created. label Apr 22, 2024
@anandolee anandolee removed the untriaged auto added to all issues by default when created. label Apr 22, 2024
@anandolee anandolee self-assigned this Apr 22, 2024
@anandolee
Copy link
Contributor

Chatted with Anthonios, and this is the update:

#15999 might the PR that introduced the change

from google._upb._message import MessageMapContainer which import internal type is not a use case we want to support. The reason to import the container is mostly check if a field is repeated, the right way is use field descriptor instead like:

if field_des.label == descriptor.FieldDescriptor.LABEL_REPEATED:
    if field_des.message_type:
        // It is a repeated message
    else:
        // It is a repeated scalar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants