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

Add typing to the Playlist and Page classes #191

Merged
merged 2 commits into from
Oct 23, 2023

Conversation

arusahni
Copy link
Contributor

@arusahni arusahni commented Aug 31, 2023

This was the hairiest of the type conversions so far. It was complicated by the fact that the Artist class does not have an items collection. As a result, the Page iterator interface would end up raising an AttributeError if iteration was attempted. Rather than add a no-op items method to the the Artist type, I opted to shim it at the Page level to keep the API clean.

Please nitpick this! While I use the API, I've never used this module.

Advances #137

@tehkillerbee
Copy link
Collaborator

Thanks, I will try to test this asap, as I just added a few features to mopidy-tidal that requires Page objects.

@2e0byo
Copy link
Collaborator

2e0byo commented Sep 3, 2023

Hmm. Thanks for this.

I think you've exposed a real weakness in the class hierarchy here. I've not had a massive amount of thought about it, but instinctively I'd prefer making the spec of all the response types the same (even with a no-op .items()), preferably by making them inherit and implement an ABC. For the time being @arusahni could you do it the other way, i.e. change the class?

As a more general point I'd like to have a shot at thinning out the codebase a bit. The api as it stands is quite fragmented, something which lead to some curious mocking tricks when I first wrote the test suite for mopidy-tidal. Getting typing (and testing) working is a good way to avoid nasty surprises, but I'm personally up for changing our api (with careful consideration) to get rid of some of these inconsistencies. As a step in that direction I think it would be good if PRs started proposing api improvements, i.e. don't feel afraid @arusahni to suggest changes.

At some point we need to rewrite the config class at least a bit.

I don't have massive amounts of time before Christmas, but I might manage to get some work done on things in december. If anyone's browsing by and want to have a shot at refactoring, go for it.

Copy link
Collaborator

@2e0byo 2e0byo left a comment

Choose a reason for hiding this comment

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

Thanks for this. A few nits + the inverse shimming option, but otherwise LGTM.

tidalapi/page.py Outdated Show resolved Hide resolved
tidalapi/page.py Outdated Show resolved Hide resolved
self.request = session.request
self.categories = None
self.title = title
self.page_category = PageCategory(session)

def __iter__(self):
def __iter__(self) -> "Page":
Copy link
Collaborator

Choose a reason for hiding this comment

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

(aside: when we move to 3.11 these should become typing.Self to handle children.)

tidalapi/page.py Outdated Show resolved Hide resolved
tidalapi/page.py Outdated Show resolved Hide resolved
tidalapi/page.py Outdated Show resolved Hide resolved
tidalapi/page.py Outdated Show resolved Hide resolved
tidalapi/page.py Show resolved Hide resolved
tidalapi/request.py Show resolved Hide resolved
@arusahni arusahni force-pushed the master branch 2 times, most recently from 7ebead8 to 1505375 Compare September 4, 2023 02:55
@arusahni arusahni changed the title Add typing to the Page classes Add typing to the Playlist and Page classes Sep 4, 2023
@arusahni arusahni requested a review from 2e0byo September 4, 2023 03:25
@arusahni
Copy link
Contributor Author

arusahni commented Sep 4, 2023

@2e0byo thanks for the review! I sprinkled a bunch of reveal_types for every item in a chained call and found two core issues that broke mypy's inference (pyright was fine):

  1. Broken typings on Session
  2. Missing typings for the playlist module.

I've updated this PR to include typings for playlist, and fixed #1. There are now far fewer casts.

@tehkillerbee
Copy link
Collaborator

@arusahni I did some quick tests in Mopidy-Tidal. It looks like everything is working as expected when browsing various pages so at least it looks like these changes didn't break anything.

@2e0byo
Copy link
Collaborator

2e0byo commented Sep 5, 2023

Thanks @arusahni yet again for the work. I'll run the test suite at some point and then merge this in.

@arusahni arusahni mentioned this pull request Sep 18, 2023
@tehkillerbee
Copy link
Collaborator

Any more comments on this PR @2e0byo? Otherwise lets merge it in and prepare for next tidalapi release.

@2e0byo
Copy link
Collaborator

2e0byo commented Sep 27, 2023

@tehkillerbee if the tests pass all is good. Would you check? Thanks for picking this up.

@tehkillerbee tehkillerbee self-assigned this Oct 23, 2023
popularity = -1
promoted_artists = None
last_item_added_at = None
popularity: Optional[int] = -1
Copy link
Collaborator

@tehkillerbee tehkillerbee Oct 23, 2023

Choose a reason for hiding this comment

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

We should probably set this one to None to be consistent with L94

@@ -83,7 +90,8 @@ def parse(self, json_obj):
self.created = dateutil.parser.isoparse(created) if created else None
public = json_obj.get("publicPlaylist")
self.public = None if public is None else bool(public)
self.popularity = json_obj.get("popularity")
popularity = json_obj.get("popularity")
self.popularity = int(popularity) if popularity else None
Copy link
Collaborator

Choose a reason for hiding this comment

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

This breaks the tests, as we expect default value to be 0. However, your changes are more consistent so lets update the tests to expect None

@tehkillerbee
Copy link
Collaborator

tehkillerbee commented Oct 23, 2023

LGTM, will fix remaining review suggestions in a separate PR

@tehkillerbee tehkillerbee merged commit af53fc7 into tamland:master Oct 23, 2023
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