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

Attempts to fix update_change_reason for nullable JSONField fields (issue #1181) #1282

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

martinschwinzerl
Copy link

@martinschwinzerl martinschwinzerl commented Dec 2, 2023

Description

As reported in issue #1181, the implementation of the utility update_change_reason() method does not work for models with a nullable JSONField. This is caused by the slightly more convoluted querying mechanisms that has to be applied for JSONFields, please cf. here: django dev documentation

This attempts to fix this by checking

  • if a field is a JSONField
  • is nullable
  • and then using different query language, depending on the null status of the field in question.

The strategy assumes that

  • a nullable JSONField with a value of None is not set at all
  • a non-nullable JSONField with a value of None is actually a "NULL" dictionary

Note: I am aware that this is not the only way to handle this. But it is the convention I am using in my project and this fixed the issue for me. It may be worthwhile to implement a policy switch to decide how to interpret None in this specific case.

Related Issue

Motivation and Context

I did run into the same issue as the original reporter of the bug report and was stuck. The fix, while admittedly maybe not yet fully universal, depending on the interpretation of a "nullable" JSONField, did solve the issue for me

How Has This Been Tested?

Tested using my local installation. No unit test implemented but this can be done, if required

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have run the pre-commit run command to format and lint.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have added my name and/or github handle to AUTHORS.rst
  • I have added my change to CHANGES.rst
  • All new and existing tests passed.

martinschwinzerl and others added 2 commits December 2, 2023 13:34
As reported in issue jazzband#1181, the implementation of the utility `update_change_reason()`
method does not work for models with a nullable JSONField. This is caused by the
slightly more convoluted querying mechanisms that have to be applied for JSONFields,
please cf. here:
[django dev documentation](https://docs.djangoproject.com/en/dev/topics/db/queries/#querying-jsonfield)

This attempts to fix this by assuming that
- a nullable JSONField with a `value` of `None` is not set at all
- a non-nullable JSONField with a `value` of `None` is actually a
    "NULL" dictionary

I am aware that this is not the only way to handle this. But it is the convention I am
using in my project and this fixed the issue for me. It may be worthwhile to implement a
policy switch to decide how to interpret `None` in this specific case.
@martinschwinzerl
Copy link
Author

Sorry, I had some issues with running the pre-commit hooks on my development machine as I do not have a python 3.8 environment available.

[INFO] Installing environment for https://github.com/psf/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/home/martin/.virtualenvs/ps_sps_users/bin/python', '-mvirtualenv', '/home/martin/.cache/pre-commit/repo5rnoh46q/py_env-python3.8', '-p', 'python3.8')
return code: 1
stdout:
    RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.8'
stderr: (none)
Check the log at /home/martin/.cache/pre-commit/pre-commit.log

I did locally modify the commit hooks to use python 3.11 and everything seemed to work but now my local branch contains this change to the pre-commit hooks and everything is a bit of a mess.

I am in a hurry but may have time to run again later next week on an older machine with python 3.8, any suggestions or pointers on how to handle this in a less awkward fashion would be highly appreciated!

Copy link

codecov bot commented Dec 2, 2023

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (29108a5) 96.87% compared to head (7ea2597) 96.56%.

Files Patch % Lines
simple_history/utils.py 20.00% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1282      +/-   ##
==========================================
- Coverage   96.87%   96.56%   -0.31%     
==========================================
  Files          23       23              
  Lines        1278     1282       +4     
  Branches      211      213       +2     
==========================================
  Hits         1238     1238              
- Misses         21       24       +3     
- Partials       19       20       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

Successfully merging this pull request may close these issues.

1 participant