edelooff / sqlalchemy-json

Full-featured JSON type with mutation tracking for SQLAlchemy
http://variable-scope.com/posts/mutation-tracking-in-nested-json-structures-using-sqlalchemy
BSD 2-Clause "Simplified" License
189 stars 34 forks source link

`AttributeError: 'super' object has no attribute 'coerce'` when setting value as `None` #10

Closed idavidmcdonald closed 6 years ago

idavidmcdonald commented 6 years ago

If I have a JSON field, say a_json that has been associated with NestedMutable I cannot set it's value as None without triggering an AttributeError as None does not have the coerce attribute.

For example:

File "example.py", line 83, in <module>
    item1.a_json = None
  File "/Users/davidmcdonald/sqlalchemy-bug-example/venv/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 229, in __set__
    instance_dict(instance), value, None)
  File "/Users/davidmcdonald/sqlalchemy-bug-example/venv/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 708, in set
    value, old, initiator)
  File "/Users/davidmcdonald/sqlalchemy-bug-example/venv/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 715, in fire_replace_event
    state, value, previous, initiator or self._replace_token)
  File "/Users/davidmcdonald/sqlalchemy-bug-example/venv/lib/python3.6/site-packages/sqlalchemy/ext/mutable.py", line 495, in set
    value = cls.coerce(key, value)
  File "/Users/davidmcdonald/sqlalchemy-bug-example/venv/lib/python3.6/site-packages/sqlalchemy_json/__init__.py", line 44, in coerce
    return super(cls).coerce(key, value)

I believe setting the value as None should be allowed and should be quite an easy one to fix. I'm happy to put in a PR if you agree this is sensible, just let me know.

Thanks :)

edelooff commented 6 years ago

Yeah this seems like a bug. Nulling a field should certainly be possible.

Adding a check for None as a value in NestedMutable.coerce seems like a decent solution, and follows the base pattern laid out in SQLAlchemy's Mutable base type. A PR would be more than welcome.

idavidmcdonald commented 6 years ago

Great, I'll put in a PR.

Would you like me to take care of a patch version bump or is this something you'd like to do separately?

idavidmcdonald commented 6 years ago

Very simple PR up - https://github.com/edelooff/sqlalchemy-json/pull/11. Feel free to take a look and I'll be happy to add any further changes.

edelooff commented 6 years ago

Thanks again.

I'll take care of the change log entry, version bump and new PyPI release when I have a bit of spare time.

benvand commented 5 years ago

@edelooff Would love to see a pypi release for this. Let me know if I can do anything to help.

edelooff commented 5 years ago

@benvand https://pypi.org/project/sqlalchemy-json/ done, thanks for the request.

casualuser commented 1 year ago

@edelooff it looks like this is still actual with https://pypi.org/project/sqlalchemy-json/0.5.0