Skip to content

Objects lose their custom_attributes during a save() #1395

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

Closed
deveaud-m opened this issue Apr 12, 2021 · 4 comments · Fixed by #1448
Closed

Objects lose their custom_attributes during a save() #1395

deveaud-m opened this issue Apr 12, 2021 · 4 comments · Fixed by #1448
Labels

Comments

@deveaud-m
Copy link

Description of the problem, including code/CLI snippet

When saving an object (e.g. User) with custom_attributes, those attributes get lost an the object needs to be synced again to recover all needed attributes:

import gitlab

gl = gitlab.Gitlab(...)
bob = gl.users.get(42, with_custom_attributes=True)
print(bob.custom_attributes)
bob.save()
print(bob.custom_attributes)

This raises AttributeError:

Traceback (most recent call last):
  File ".../lib/python3.9/site-packages/gitlab/base.py", line 59, in __getattr__
    return self.__dict__["_updated_attrs"][name]
KeyError: 'custom_attributes'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../lib/python3.9/site-packages/gitlab/base.py", line 62, in __getattr__
    value = self.__dict__["_attrs"][name]
KeyError: 'custom_attributes'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../lib/python3.9/site-packages/gitlab/base.py", line 82, in __getattr__
    return self.__dict__["_parent_attrs"][name]
KeyError: 'custom_attributes'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../attr_test.py", line 8, in <module>
    print(f"Bob after {json.dumps(bob.custom_attributes, indent=4)}")
  File ".../lib/python3.9/site-packages/gitlab/base.py", line 84, in __getattr__
    raise AttributeError(name)
AttributeError: custom_attributes

Expected Behavior

Restore objects' custom_attributes after calling save().
This can be reproduced with Projects and Groups.

Actual Behavior

The objects must be synced again with the option with_custom_attributes=True to access custom_attributes.

Specifications

  • python-gitlab version: 2.6.0
  • API version you are using (v3/v4): v4
  • Gitlab server version (or gitlab.com): 13.10.1
@nejch nejch added the bug label Apr 13, 2021
@nejch
Copy link
Member

nejch commented Apr 13, 2021

Thanks @deveaud-m! Note to self:

  • for save()/update() methods the new server data alone shouldn't be used as it will mostly be just a subset of what's returned via GET.
  • for refresh(), the arguments used for the original get() should maybe be stored in the object and reused for subsequent GET requests or there will be missing attributes again.

A similar example is here with save and sudo: #532

@max-wittig
Copy link
Member

Not sure, if this is a "bug" that we can fix. We're just retrieving the info that GitLab sends. I don't think we should change that behavior.

@nejch
Copy link
Member

nejch commented May 2, 2021

Not sure, if this is a "bug" that we can fix. We're just retrieving the info that GitLab sends. I don't think we should change that behavior.

That's true, just that in a way #1213 was a breaking change in behavior from pre-2.6.0 🤔 and can be confusing for users maybe, the way it works.

I'm thinking an extra arg here would help cover both @deveaud-m's use case for save() and @ericfrederich's original idea for refresh() in #1155.

I've got #1443 as an initial lightweight approach without refactoring RESTObjects too much, @deveaud-m would this cover your issue or is it still too verbose?
Reverting this entirely would bring back the previous issue with refresh.

@JohnVillalovos JohnVillalovos changed the title Objects loose their custom_attributes during a save() Objects lose their custom_attributes during a save() May 2, 2021
@deveaud-m
Copy link
Author

I've got #1443 as an initial lightweight approach without refactoring RESTObjects too much, @deveaud-m would this cover your issue or is it still too verbose?
Reverting this entirely would bring back the previous issue with refresh.

I'm ok with this change, at least it would be stated in the documentation that objects can loose their attributes while being refreshed or saved. Users then have the choice between using the "verbose" argument to persist attributes or get a new object using the API (in case concurrent accesses to that object would have been performed on the server side).

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants