Skip to content

Fix: Do not set deleted items to UserList._items in UserList.remove_items() - #30

Draft
glensc wants to merge 1 commit into
mainfrom
userlist-remove_items-bug
Draft

glensc wants to merge 1 commit into
mainfrom
userlist-remove_items-bug

Conversation

@glensc

@glensc glensc commented Apr 18, 2023

Copy link
Copy Markdown
Owner

Seems like a bug. Does not make sense to set _items to the value of deleted items.

Git blame points to 51b1b7b commit:

51b1b7b0 (moogar0880      2015-02-14 22:05:10 -0500 178)         self._items = items
51b1b7b0 (moogar0880      2015-02-14 22:05:10 -0500 205)         self._items = items

Seems to me the code is copy-paste from def add_items(self, *items)

@glensc
glensc requested a review from simonc56 April 18, 2023 20:37
@glensc glensc self-assigned this Apr 18, 2023
@simonc56

simonc56 commented Apr 19, 2023

Copy link
Copy Markdown
Collaborator

Also in def add_items(self, *items) it doesn't make sense to set _items to the value of added items.

What about items already in list ?

@glensc glensc changed the title Fix: Do not set deleted items to UserList._items Fix: Do not set deleted items to UserList._items in UserList.remove_items() Apr 19, 2023
@glensc

glensc commented Apr 19, 2023

Copy link
Copy Markdown
Owner Author

And get_items appends to the list, if called multiple times with different list value the result would be pointless, i.e it's collection of random things, not a definition of a UserList.

I think get_items was not intended to be used outside the class and it should just return the data rather updating self._items.
the self._items update could be moved to _get.

and addition to that add_items should merge the _items, and remove_items should remove items from _items.

the _items is used for the iter outside the class.

@glensc
glensc marked this pull request as draft August 30, 2023 20:11
@rawsun007

Copy link
Copy Markdown
Contributor

The line this draft deletes is still on main at trakt/users.py:333, and add_items carries the same assignment at :304. __len__ from #131 makes both observable:

>>> len(ulist)               # three items loaded by _get()
3
>>> ulist.add_items(movie)   # one item added to a three-item list
>>> len(ulist)
1

Two consequences beyond the value being wrong:

  • items is a tuple, so _items stops being a list, and the next get_items() fails with 'tuple' object has no attribute 'append'.
  • The assignment runs before the generator yields the request, so it lands whether or not the POST succeeds.

test_user_list calls both methods with no arguments, so self._items = () replaces an empty list with an empty tuple and nothing in the suite notices.

The shape described in the comments above — get_items returning data rather than appending, the _items update moving to _get, add_items merging and remove_items subtracting — is larger than this draft's one-line deletion, and it is a behaviour change. Happy to implement it, against this branch or a fresh one, with tests that call both methods with real arguments. Waiting to be asked rather than starting.

Written with Claude Code (Claude Opus 5) on rawsun007's account.

This branch has not been deployed

No deployments
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.

3 participants