spotcast icon indicating copy to clipboard operation
spotcast copied to clipboard

HA 2024.2 with Python 3.12 raise an TypeError

Open difrost opened this issue 1 year ago • 2 comments

Bug Ticket

Describe the bug

Due to the randrange() changes introduced in Python 3.12 Spotcast raise an TypeError:

 File "/config/custom_components/spotcast/__init__.py", line 285, in start_casting
    spotcast_controller.play(
  File "/config/custom_components/spotcast/spotcast_controller.py", line 370, in play
    position = random.randint(0, results["total"] - 1)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/random.py", line 336, in randint
    return self.randrange(a, b+1)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/random.py", line 312, in randrange
    istop = _index(stop)
            ^^^^^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer

See following note:

Remove randrange() functionality deprecated since Python 3.10. Formerly, randrange(10.0) losslessly converted to randrange(10). Now, it raises a [TypeError](https://docs.python.org/3/library/exceptions.html#TypeError). Also, the exception raised for non-integer values such as randrange(10.5) or randrange('10') has been changed from [ValueError](https://docs.python.org/3/library/exceptions.html#ValueError) to [TypeError](https://docs.python.org/3/library/exceptions.html#TypeError). This also prevents bugs where randrange(1e25) would silently select from a larger range than randrange(10**25).

Troubleshooting

Make sure to validate all the elements before submitting the ticket (Exception to the steps marked as optional)

  • [x] Using latest version of spotcast
  • [x] Using latest stable version of Home Assistant
  • [x] I have setup the Spotify integration in Home Assistant
  • [x] I have renewed my sp_dc and sp_key values and restarted Home Assistant (see README)
  • [x] (optional) I have Spotify Premium
  • [ ] (optional) I am using multiple accounts
  • [ ] (optional) I'm attaching relevant logs with level debug for component spotcast (see README)
  • [ ] (optional) I'm using entity_id in the service call and have tried device_name but the issue remains

Environment

  • Installation type: Container (in k3s)
  • HA version: 2024.2.x
  • spotcast version: 3.7.2

difrost avatar Mar 02 '24 10:03 difrost

As a quick check I've done a cast to int and that "fixes" the issue:

           if random_song:
                if uri.find("album") > 0:
                    results = client.album_tracks(uri, market=country_code)
                    position = random.randint(0, int(results["total"]) - 1)
                elif uri.find("playlist") > 0:
                    results = client.playlist_tracks(uri)
                    position = random.randint(0, int(results["total"]) - 1)
                elif uri.find("collection") > 0:
                    results = client.current_user_saved_tracks()
                    position = random.randint(0, int(results["total"]) - 1)
                _LOGGER.debug(
                    "Start playback at random position: %s", position)

I'm not sure if that's correct tho can't find a reason why direct cast might cause some future errors. I can make a PR for review.

NOTE: I'm not a Python dev :smile:

difrost avatar Mar 02 '24 10:03 difrost

Thanks for the info. If you submit a pr can you just add a comment saying

# TODO: review random position algo

I want to revisit it later, but at least we could fix the dependency issue quick.

fcusson avatar Mar 02 '24 13:03 fcusson