{"action":"created","issue":{"url":"https://api.github.com/repos/google/flatbuffers/issues/4389","repository_url":"https://api.github.com/repos/google/flatbuffers","labels_url":"https://api.github.com/repos/google/flatbuffers/issues/4389/labels{/name}","comments_url":"https://api.github.com/repos/google/flatbuffers/issues/4389/comments","events_url":"https://api.github.com/repos/google/flatbuffers/issues/4389/events","html_url":"https://github.com/google/flatbuffers/issues/4389","id":244436910,"number":4389,"title":"Python 3 string field return type differs ","user":{"login":"jlafon","id":236514,"avatar_url":"https://avatars2.githubusercontent.com/u/236514?v=4","gravatar_id":"","url":"https://api.github.com/users/jlafon","html_url":"https://github.com/jlafon","followers_url":"https://api.github.com/users/jlafon/followers","following_url":"https://api.github.com/users/jlafon/following{/other_user}","gists_url":"https://api.github.com/users/jlafon/gists{/gist_id}","starred_url":"https://api.github.com/users/jlafon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jlafon/subscriptions","organizations_url":"https://api.github.com/users/jlafon/orgs","repos_url":"https://api.github.com/users/jlafon/repos","events_url":"https://api.github.com/users/jlafon/events{/privacy}","received_events_url":"https://api.github.com/users/jlafon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-07-20T17:20:02Z","updated_at":"2017-08-02T10:53:49Z","closed_at":null,"body":"If a string field is included during serialization, a bytes object is returned when deserializing, but a string object is returned if the field is omitted.\r\n\r\nThis leads to a situation where every optional string field has to be type-checked (bytes or str) upon deserialization, assuming you need to get a string. There is also the issue that the method named String() returns bytes in Python 3, which some may find surprising.\r\n\r\nBelow are details on how to reproduce the issue.\r\n\r\nThis issue can be reproduced with a very simple schema:\r\n`\r\ntable Example {\r\n  Field:string;\r\n}\r\n`\r\n```bash\r\n$ flatc example.fbs --python\r\n```\r\n\r\nThe generated code:\r\n```python\r\n# automatically generated by the FlatBuffers compiler, do not modify\r\n\r\n# namespace: \r\n\r\nimport flatbuffers\r\n\r\nclass Example(object):\r\n    __slots__ = ['_tab']\r\n\r\n    @classmethod\r\n    def GetRootAsExample(cls, buf, offset):\r\n        n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)\r\n        x = Example()\r\n        x.Init(buf, n + offset)\r\n        return x\r\n\r\n    # Example\r\n    def Init(self, buf, pos):\r\n        self._tab = flatbuffers.table.Table(buf, pos)\r\n\r\n    # Example\r\n    def Field(self):\r\n        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))\r\n        if o != 0:\r\n            return self._tab.String(o + self._tab.Pos)\r\n        return \"\"\r\n\r\ndef ExampleStart(builder): builder.StartObject(1)\r\ndef ExampleAddField(builder, Field): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(Field), 0)\r\ndef ExampleEnd(builder): return builder.EndObject()\r\n```\r\nAn example reproducing the issue:\r\n```python\r\nimport flatbuffers\r\nimport Example\r\n\r\nbuilder = flatbuffers.Builder(32)\r\nfield_value = \"test\"\r\n\r\noff = builder.CreateString(field_value)\r\nExample.ExampleStart(builder)\r\nExample.ExampleAddField(builder, off)\r\nbuilder.Finish(Example.ExampleEnd(builder))\r\n\r\nex = Example.Example.GetRootAsExample(builder.Output(), 0)\r\n\r\nprint(\"Type where field is included\", type(ex.Field()))\r\n\r\nExample.ExampleStart(builder)\r\nbuilder.Finish(Example.ExampleEnd(builder))\r\nex = Example.Example.GetRootAsExample(builder.Output(), 0)\r\nprint(\"Type where field is omitted\", type(ex.Field()))\r\n```\r\nThe output:\r\n```bash\r\n$ python test.py \r\nType where field is included <class 'bytes'>\r\nType where field is omitted <class 'str'>\r\n```\r\n\r\n- flatc version: 1.6.0\r\n- Python library version: flatbuffers==2015.12.22.1\r\n- Python version: 3.5.2\r\n- OS: OSX 10.11.6\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/google/flatbuffers/issues/comments/319638882","html_url":"https://github.com/google/flatbuffers/issues/4389#issuecomment-319638882","issue_url":"https://api.github.com/repos/google/flatbuffers/issues/4389","id":319638882,"user":{"login":"rw","id":21367,"avatar_url":"https://avatars2.githubusercontent.com/u/21367?v=4","gravatar_id":"","url":"https://api.github.com/users/rw","html_url":"https://github.com/rw","followers_url":"https://api.github.com/users/rw/followers","following_url":"https://api.github.com/users/rw/following{/other_user}","gists_url":"https://api.github.com/users/rw/gists{/gist_id}","starred_url":"https://api.github.com/users/rw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rw/subscriptions","organizations_url":"https://api.github.com/users/rw/orgs","repos_url":"https://api.github.com/users/rw/repos","events_url":"https://api.github.com/users/rw/events{/privacy}","received_events_url":"https://api.github.com/users/rw/received_events","type":"User","site_admin":false},"created_at":"2017-08-02T10:53:49Z","updated_at":"2017-08-02T10:53:49Z","body":"@jlafon Thanks for pointing this out. The fix is straightforward: update the code generator to emit `bytes()` instead of `\"\"`. Would you like to make a tiny PR with this change?"}}
{"actor":{"display_login":"rw"}}