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

Type issues with RadarrAPI.get_movie() #119

Closed
2 tasks done
wthueb opened this issue Jun 30, 2022 · 3 comments · Fixed by #116
Closed
2 tasks done

Type issues with RadarrAPI.get_movie() #119

wthueb opened this issue Jun 30, 2022 · 3 comments · Fixed by #116
Labels
type/bug Something isn't working
Milestone

Comments

@wthueb
Copy link
Contributor

wthueb commented Jun 30, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current Behaviour

RadarrAPI.get_movie() has quite odd functionality. This isn't pyarr's fault, the v3 Radarr API simply gives you a lot of options for getting a movie in your library, and it makes sense to encapsulate them into one function.

If you request using a TMDB ID, the API responds with a singleton list if it finds the movie, else it responds with an empty list. Meanwhile, if you request using a Radarr database ID, the API responds with the JSON of the movie, or it returns the resource not found status code.

It was discussed in #105 how the type hints should be constructed, however narrowing the types through the assert_return function appears to not be working very well for me when Unions are involved. Pylance and mypy for that matter get quite upset about me trying to call __getitem__ on lists ("error: No overload variant of "getitem" of "list" matches argument type "str"") and indexing Unions with ints ("error: Invalid index type "int" for "Union[List[Dict[str, Any]], Dict[str, Any]]"").

Also, #113 was meant to fix #111, however the SonarrAPI.get_series() part was missed.

Expected behaviour

I was generally unaware of how to tackle the problem the proper way as types in Python are relatively new to me, however I stumbled across this article explaining, what appears to be after further research, the standard (typing.overload decorated function signatures).

For instance, RadarrAPI.get_movie() would have the following signatures (side note: should tmdb be keyword-only?):

from typing import Any, Literal, Optional, Union, overload

@overload
def get_movie(id: None = ..., tmdb: bool = ...) -> list[dict[str, Any]]:
    ...

@overload
def get_movie(id: int, tmdb: Literal[False] = ...) -> dict[str, Any]:
    ...

@overload
def get_movie(id: int, tmdb: Literal[True]) -> list[dict[str, Any]]:
    ...

def get_movie(
    id: Optional[int] = None, tmdb: bool = False
) -> Union[dict[str, Any], list[dict[str, Any]]]:
    # implementation here
    ...

This could be put right in the source files, or in stub files. With how many signatures needed though, I think stub files would make the most sense.

I can try my hand at a PR for this, however my non-Radarr API knowledge is pretty lacking.

Steps To Reproduce

No response

Pyarr Version

4.1.0

Python Version

3.10.0

Example Code

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@wthueb wthueb added the type/bug Something isn't working label Jun 30, 2022
@marksie1988
Copy link
Collaborator

Yes I have started adding tests, initially with Sonarr which has picked up some issues with the type hints, as I write the tests I am resolving the issues. However its taking me time to write them due to there being so many functions.

I plan to work on the Radarr tests second as I think people mainly use Sonarr & Radarr.

@Archmonger
Copy link
Contributor

Archmonger commented Jun 30, 2022

I would recommend basing the tests off of aiopyarr's configuration.

We probably don't need to be as strict on the type checks as his test config though.

@marksie1988
Copy link
Collaborator

marksie1988 commented Jun 30, 2022

That's what I have been looking at, however as he is using objects for the responses the tests are far more substantial.

Really all we need to test for is that the expected type is returned e.g. dict / list on the JSON, the correct errors are raised which I'm covering in the current PR.

Unless there are any obvious tests that I'm missing? I'm new to setting up tests so I may well be missing something obvious.

I almost have all of the Sonar API setup with tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants