Skip to content

Conversation

hwalinga
Copy link
Contributor

@hwalinga hwalinga commented Apr 24, 2021

Description

Return self if called on the class instead of instance to not error when used with djangorestframework

Some metaprogramming magic in djangorestframework performs a check on getting all the property object attributes on a django model. As this loops over all attributes it will also arrive at the history_object which is implemented with a descriptor. However, this implementation does not account for being accessed on the class instead of the model. (So instance is None.)

This PR just returns self when that happens. This does not interfere with this metaprogramming done, and makes django-simple-history usable again in combination with djangorestframework.

Returning self in __get__ when instance is None is the canonical way of doing this, as explained here:

https://stackoverflow.com/questions/29436716/why-do-you-need-if-instance-is-none-in-get-of-a-descriptor-class

Related Issue

Fixes #821

Motivation and Context

Fixed #821

How Has This Been Tested?

I have created the fix and installed it in a project that suffered from this bug and it worked. I don't think we should be pulling in djangorestframework for this?

Types of changes

Descriptor now accounts for being accessed on the class not the instance.

Checklist:

  • I have run the make format command to format my code
  • My change requires a change to the documentation. (They don't)
  • I have updated the documentation accordingly. (No docs need updating)
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes. (Manually confirmed fix)
  • 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.

…ut in metaprogramming in internals of djangorestframework.
@hwalinga hwalinga force-pushed the account-for-get-historical-object-on-class branch from 7144152 to a8b7cb1 Compare April 25, 2021 10:15
@hwalinga hwalinga changed the title Return self if called on the class instead of instance to not error when used with djangorestframework Fix bug where descriptor class missed instance=None case May 20, 2021
@hwalinga
Copy link
Contributor Author

@barm Would you be able to take a look at this to merge it? It is just a very minor two lines fix in a wrongly implemented descriptor class.

@hwalinga
Copy link
Contributor Author

@rossmechanic Would you be able to see if this fix can be merged? It are just two lines solving an annoying issue with a wrongly implemented descriptor class. I saw you had previous commits affecting the surrounding lines.

@@ -579,6 +579,8 @@ def __init__(self, model, fields_included):
self.fields_included = fields_included

def __get__(self, instance, owner):
if instance is None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hwalinga Why would you want to return the descriptor?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see your stackoverflow post. Got it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is so that any metaprogramming done on the class knows what kind of attribute this is. (ie a descriptor class) You can also return None, but this way you allow anyone inspecting the class (programmatically) to know what is in here.

That is also the bug I got in DRF. There was some metaprogramming that failed to continue when it got to this attribute.

@codecov
Copy link

codecov bot commented May 31, 2021

Codecov Report

Merging #822 (11f1579) into master (df66df4) will decrease coverage by 0.19%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #822      +/-   ##
==========================================
- Coverage   97.87%   97.67%   -0.20%     
==========================================
  Files          18       18              
  Lines         988      990       +2     
  Branches      150      151       +1     
==========================================
  Hits          967      967              
- Misses          9       10       +1     
- Partials       12       13       +1     
Impacted Files Coverage Δ
simple_history/models.py 97.86% <0.00%> (-0.61%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update df66df4...11f1579. Read the comment docs.

@rossmechanic rossmechanic merged commit 666b6a6 into django-commons:master May 31, 2021
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.

djangorestframework 3.12.3 introduced bug where HistoricalModel cannot be used with OrderingFilter
2 participants