Skip to content

propose fix for issue 61 #62

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

Merged
merged 7 commits into from
Apr 5, 2025
Merged

Conversation

zh3nl
Copy link
Contributor

@zh3nl zh3nl commented Apr 1, 2025

Fix: For fget, fset, and fdel, I suggest binding the relevant methods to the class. Also use a descriptor protocol for class members.

Tests: I have created the following tests to test my implementation against read-only class property, deletable class property, class properties with default values, and class property with custom getter/setter methods.

Please provide feedback as I am a beginner to this!

@VigneshVSV
Copy link
Collaborator

I will also do a step-by-step debug and get back in about a day! Thanks a lot.

@VigneshVSV
Copy link
Collaborator

Hi, I think we have to rever the latest commit because some other sections of the code are breaking by changing getter/setter decorator to return self.

I have made a separate issue here: #63

@VigneshVSV VigneshVSV moved this to In review in hololinked Apr 2, 2025
@VigneshVSV
Copy link
Collaborator

Also the version in here and here can be changed to 0.2.10 so that once this is merged, I can release it to pip and anaconda.

@zh3nl
Copy link
Contributor Author

zh3nl commented Apr 3, 2025

@VigneshVSV Just pushed the following changes:

  1. Reverted changes to decorators to the original commit
  2. Asserted instance-based access as you suggested.
  3. Added self.assertEqual(instance.deletable_class_prop, 100)

@VigneshVSV
Copy link
Collaborator

To run the full unit tests, you can do

cd tests
python -m unittest

It needs to pass before I can merge the branch. Unfortunately , for non-admin, its not automatically triggered.

@zh3nl
Copy link
Contributor Author

zh3nl commented Apr 5, 2025

I have attempted to change my code to pass the unit tests but I observe that the set and del methods are not working because they are trying to change the class which will result in "TypeError: 'mappingproxy' object does not support item assignment". This may or may not be in the scope of the issue and I do not want to change too much of the code.

@VigneshVSV
Copy link
Collaborator

VigneshVSV commented Apr 5, 2025

I have attempted to change my code to pass the unit tests but I observe that the set and del methods are not working because they are trying to change the class which will result in "TypeError: 'mappingproxy' object does not support item assignment". This may or may not be in the scope of the issue and I do not want to change too much of the code.

Not a problem!

This snippet should fix that

else: 
    if self.class_member:
        setattr(obj, self._internal_name, value)
    else:
        obj.__dict__[self._internal_name] = value

However, I can take it from here. Should I do so? I think other parts are working when I checked also in the debugger.

If you interested in an explanation, I think this is due class-factory or metaclass behaviour where the __dict__ seems to be implemented as a mappingproxy. It needs a setattr operation instead of using it like a dictionary.

@VigneshVSV
Copy link
Collaborator

Also, thanks a lot for the contribution!

@VigneshVSV VigneshVSV changed the base branch from main to issue-61 April 5, 2025 08:23
@zh3nl
Copy link
Contributor Author

zh3nl commented Apr 5, 2025

I have implemented the changes you suggested. If you want to take it from here, please do so and merge my additions as needed.

@VigneshVSV
Copy link
Collaborator

I have implemented the changes you suggested. If you want to take it from here, please do so and merge my additions as needed.

Thanks again! Its looking pretty good.

@VigneshVSV VigneshVSV self-assigned this Apr 5, 2025
@VigneshVSV VigneshVSV merged commit f277536 into hololinked-dev:issue-61 Apr 5, 2025
@github-project-automation github-project-automation bot moved this from In review to Done in hololinked Apr 5, 2025
@zh3nl
Copy link
Contributor Author

zh3nl commented Apr 5, 2025

I want to note that in #62 in the chunk between 413 and 420 on parametrized.py I did use getattr rather than obj.dict.get. Not sure if will affect anything.

@VigneshVSV
Copy link
Collaborator

VigneshVSV commented Apr 5, 2025

I want to note that in #62 in the chunk between 413 and 420 on parametrized.py I did use getattr rather than obj.dict.get. Not sure if will affect anything.

Logically its fine! Its probably just a mere few nanoseconds faster to just use the __dict__. The getattr will ultimately look into the __dict__ if it does not find it anywhere else.

@zh3nl
Copy link
Contributor Author

zh3nl commented Apr 5, 2025

Sounds good! I thought there was a slight difference between the two, but wanted to make sure that I wouldn't break anything :) Have a great rest of your day, and thanks for letting me contribute to the project!

@VigneshVSV VigneshVSV mentioned this pull request Apr 5, 2025
@VigneshVSV
Copy link
Collaborator

Hi, I published it to pip. Thanks a lot!

I had to make one change which was, I reset self.fget.__get__(None, obj)(obj) to self.fget(obj) & similar for setters and deleters because when you try to bind it using None as the first argument, it results in the same object.
In other words self.fget.__get__(None, obj) is exactly equal to self.fget(obj).

You can have a look here: https://pypi.org/project/hololinked/

@zh3nl
Copy link
Contributor Author

zh3nl commented Apr 5, 2025

Okay got it. So this change has the same functionality but is more readable?

@VigneshVSV
Copy link
Collaborator

Okay got it. So this change has the same functionality but is more readable?

It just avoids one extra function call. It does not make it more or less readable as if you try self.fget.__get__(None, cls) == self.fget, you will get True.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants