Open spiqueras opened 1 year ago
AFAICS, Cloud NDB always uses str
for class_
field:
We are considering this change. It will take some time because we need to ensure this will not break existing users. My current plan is to introduce this change in the next Major Version update enabled by a flag. In a subsequent update, we can update the flag to be enabled by default.
This was added as part providing py2->py3 support. b/139757579 cl/276076727
On Tue, Jul 25, 2023 at 11:21 AM shreejad @.***> wrote:
We are considering this change. It will take some time because we need to ensure this will not break existing users. My current plan is to introduce this change in the next Major Version update enabled by a flag. In a subsequent update, we can update the flag to be enabled by default.
— Reply to this email directly, view it on GitHub https://github.com/GoogleCloudPlatform/appengine-python-standard/issues/89#issuecomment-1650323661, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADUIHGYM6WNPGXRPNJIFQELXSAFELANCNFSM6AAAAAAYQMTV7A . You are receiving this because you are subscribed to this thread.Message ID: <GoogleCloudPlatform/appengine-python-standard/issues/89/1650323661@ github.com>
Expected Behavior
Polyclass models have a read-only attribute called
class_
that contains the class hierarchy in form of a list. This attribute should always contain a list of eitherstr
orbytes
, preferably the former.Actual Behavior
On instance creation, the
class_
attribute is filled withbytes
objects. After the object is put and on any subsequent read, this attribute will containstr
objects instead. This makes the attribute hard to rely on, as if you ever want to use it you need to know if the entity has already been written to the database or not or convert between types. It also breaks instance equality checks when one of the instance has just been created and the other has been retrieved from the database.This is similar in Python 2 SDK (the attribute is of type
str
pre-put
andunicode
after), but due to Python 2'sstr
andunicode
being mostly compatible, it wasn't that much of an issue.Steps to Reproduce the Problem
The following test will work fine in Python 2 but fail in Python 3.
The error
FAILED tests_py3/model_test.py::test_class_in_polymodel - AssertionError: assert APolymodel(key=Key('APolymodel', 1), class_=['APolymodel']) == APolymodel(key=Key('APolymodel', 1), class_=[b'APolymodel'])
The offending code is likely the following: https://github.com/GoogleCloudPlatform/appengine-python-standard/blob/ad6e5cc1927557aa16e656bc9a8444da69b8bbb5/src/google/appengine/ext/ndb/polymodel.py#L214-L219 Unless there's a clear reason to enforce
class_
being abytes
list on instance creation, I'd be great to remove theensure_binary
, making it consistently anstr
list.Goes without saying, but this issue does not happen to classes that inherit from
ndb.Model
.Specifications