From bd2a63653b7022e58cd03c1e029192a50d563ccd Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Fri, 9 Apr 2021 23:30:53 +0000 Subject: [PATCH] Refactor unnecessary `else` / `elif` when `if` block has a `return` statement --- librespot/Version.py | 5 ++--- librespot/audio/AbsChunkedInputStream.py | 2 +- librespot/audio/AudioKeyManager.py | 9 ++++---- librespot/audio/PlayableContentFeeder.py | 5 ++--- librespot/audio/cdn/CdnManager.py | 8 +++---- librespot/audio/format/SuperAudioFormat.py | 23 ++++++++++----------- librespot/common/Base62.py | 5 ++--- librespot/common/Utils.py | 3 +-- librespot/core/EventService.py | 2 +- librespot/mercury/MercuryClient.py | 3 +-- librespot/metadata/AlbumId.py | 3 +-- librespot/metadata/ArtistId.py | 3 +-- librespot/metadata/EpisodeId.py | 3 +-- librespot/metadata/ShowId.py | 3 +-- librespot/metadata/TrackId.py | 3 +-- librespot/player/codecs/AudioQuality.py | 23 ++++++++++----------- librespot/standard/ByteArrayOutputStream.py | 2 +- librespot/standard/DataInputStream.py | 5 ++--- librespot/standard/InputStream.py | 12 +++++------ 19 files changed, 52 insertions(+), 70 deletions(-) diff --git a/librespot/Version.py b/librespot/Version.py index b97e908..11f24e3 100644 --- a/librespot/Version.py +++ b/librespot/Version.py @@ -9,10 +9,9 @@ class Version: def platform() -> Platform: if platform.system() == "Windows": return Platform.PLATFORM_WIN32_X86 - elif platform.system() == "Darwin": + if platform.system() == "Darwin": return Platform.PLATFORM_OSX_X86 - else: - return Platform.PLATFORM_LINUX_X86 + return Platform.PLATFORM_LINUX_X86 pass @staticmethod diff --git a/librespot/audio/AbsChunkedInputStream.py b/librespot/audio/AbsChunkedInputStream.py index 4c398ea..af493d5 100644 --- a/librespot/audio/AbsChunkedInputStream.py +++ b/librespot/audio/AbsChunkedInputStream.py @@ -153,7 +153,7 @@ class AbsChunkedInputStream(InputStream, HaltListener): length: int = None) -> int: if b is None and offset is None and length is None: return self.internal_read() - elif not (b is not None and offset is not None and length is not None): + if not (b is not None and offset is not None and length is not None): raise TypeError() if self.closed: diff --git a/librespot/audio/AudioKeyManager.py b/librespot/audio/AudioKeyManager.py index 31537ba..32e5861 100644 --- a/librespot/audio/AudioKeyManager.py +++ b/librespot/audio/AudioKeyManager.py @@ -45,11 +45,10 @@ class AudioKeyManager(PacketsReceiver): if key is None: if retry: return self.get_audio_key(gid, file_id, False) - else: - raise RuntimeError( - "Failed fetching audio key! gid: {}, fileId: {}".format( - Utils.Utils.bytes_to_hex(gid), - Utils.Utils.bytes_to_hex(file_id))) + raise RuntimeError( + "Failed fetching audio key! gid: {}, fileId: {}".format( + Utils.Utils.bytes_to_hex(gid), + Utils.Utils.bytes_to_hex(file_id))) return key diff --git a/librespot/audio/PlayableContentFeeder.py b/librespot/audio/PlayableContentFeeder.py index 853523c..e99b893 100644 --- a/librespot/audio/PlayableContentFeeder.py +++ b/librespot/audio/PlayableContentFeeder.py @@ -87,9 +87,8 @@ class PlayableContentFeeder: if track is not None: return CdnFeedHelper.load_track(self.session, track, file, resp, preload, halt_lister) - else: - return CdnFeedHelper.load_episode(self.session, episode, file, - resp, preload, halt_lister) + return CdnFeedHelper.load_episode(self.session, episode, file, + resp, preload, halt_lister) elif resp.result == StorageResolve.StorageResolveResponse.Result.STORAGE: if track is None: # return StorageFeedHelper diff --git a/librespot/audio/cdn/CdnManager.py b/librespot/audio/cdn/CdnManager.py index a5bbf35..2b8b3c8 100644 --- a/librespot/audio/cdn/CdnManager.py +++ b/librespot/audio/cdn/CdnManager.py @@ -79,9 +79,8 @@ class CdnManager: self._LOGGER.debug("Fetched CDN url for {}: {}".format( Utils.bytes_to_hex(file_id), url)) return url - else: - raise CdnManager.CdnException( - "Could not retrieve CDN url! result: {}".format(proto.result)) + raise CdnManager.CdnException( + "Could not retrieve CDN url! result: {}".format(proto.result)) class CdnException(Exception): def __init__(self, ex): @@ -228,8 +227,7 @@ class CdnManager: if self._streamId.is_episode(): return "episode_gid: {}".format( self._streamId.get_episode_gid()) - else: - return "file_id: {}".format(self._streamId.get_file_id()) + return "file_id: {}".format(self._streamId.get_file_id()) def decrypt_time_ms(self) -> int: return self._audioDecrypt.decrypt_time_ms() diff --git a/librespot/audio/format/SuperAudioFormat.py b/librespot/audio/format/SuperAudioFormat.py index d672bf8..82986d4 100644 --- a/librespot/audio/format/SuperAudioFormat.py +++ b/librespot/audio/format/SuperAudioFormat.py @@ -10,18 +10,17 @@ class SuperAudioFormat(enum.Enum): @staticmethod def get(audio_format: Metadata.AudioFile.Format): if audio_format == Metadata.AudioFile.Format.OGG_VORBIS_96 or \ - audio_format == Metadata.AudioFile.Format.OGG_VORBIS_160 or \ - audio_format == Metadata.AudioFile.Format.OGG_VORBIS_320: + audio_format == Metadata.AudioFile.Format.OGG_VORBIS_160 or \ + audio_format == Metadata.AudioFile.Format.OGG_VORBIS_320: return SuperAudioFormat.VORBIS - elif audio_format == Metadata.AudioFile.Format.MP3_256 or \ - audio_format == Metadata.AudioFile.Format.MP3_320 or \ - audio_format == Metadata.AudioFile.Format.MP3_160 or \ - audio_format == Metadata.AudioFile.Format.MP3_96 or \ - audio_format == Metadata.AudioFile.Format.MP3_160_ENC: + if audio_format == Metadata.AudioFile.Format.MP3_256 or \ + audio_format == Metadata.AudioFile.Format.MP3_320 or \ + audio_format == Metadata.AudioFile.Format.MP3_160 or \ + audio_format == Metadata.AudioFile.Format.MP3_96 or \ + audio_format == Metadata.AudioFile.Format.MP3_160_ENC: return SuperAudioFormat.MP3 - elif audio_format == Metadata.AudioFile.Format.AAC_24 or \ - audio_format == Metadata.AudioFile.Format.AAC_48 or \ - audio_format == Metadata.AudioFile.Format.AAC_24_NORM: + if audio_format == Metadata.AudioFile.Format.AAC_24 or \ + audio_format == Metadata.AudioFile.Format.AAC_48 or \ + audio_format == Metadata.AudioFile.Format.AAC_24_NORM: return SuperAudioFormat.AAC - else: - raise RuntimeError("Unknown audio format: {}".format(audio_format)) + raise RuntimeError("Unknown audio format: {}".format(audio_format)) diff --git a/librespot/common/Base62.py b/librespot/common/Base62.py index 9663822..5ab0c71 100644 --- a/librespot/common/Base62.py +++ b/librespot/common/Base62.py @@ -59,10 +59,9 @@ class Base62: out += bytes([0]) return self.reverse(out) - elif len(out) > estimated_length: + if len(out) > estimated_length: return self.reverse(out[:estimated_length]) - else: - return self.reverse(out) + return self.reverse(out) def estimate_output_length(self, input_length: int, source_base: int, target_base: int): diff --git a/librespot/common/Utils.py b/librespot/common/Utils.py index 2a9b18c..9602689 100644 --- a/librespot/common/Utils.py +++ b/librespot/common/Utils.py @@ -30,8 +30,7 @@ class Utils: fmt = '%%0%dx' % (width // 4) if i == 0: return bytes([0]) - else: - return binascii.unhexlify(fmt % i) + return binascii.unhexlify(fmt % i) @staticmethod def bytes_to_hex(buffer: bytes) -> str: diff --git a/librespot/core/EventService.py b/librespot/core/EventService.py index 6e119ff..24f6e08 100644 --- a/librespot/core/EventService.py +++ b/librespot/core/EventService.py @@ -94,7 +94,7 @@ class EventService: self.body.write(byte=0x09) self.body.write(byte=c) return self - elif s is not None: + if s is not None: self.body.write(byte=0x09) self.append_no_delimiter(s) return self diff --git a/librespot/mercury/MercuryClient.py b/librespot/mercury/MercuryClient.py index 253a4c1..0ce128f 100644 --- a/librespot/mercury/MercuryClient.py +++ b/librespot/mercury/MercuryClient.py @@ -73,8 +73,7 @@ class MercuryClient(PacketsReceiver.PacketsReceiver, Closeable): resp = self.send_sync(request.request) if 200 <= resp.status_code < 300: return json.loads(resp.payload[0]) - else: - raise MercuryClient.MercuryException(resp) + raise MercuryClient.MercuryException(resp) def send(self, request: RawMercuryRequest, callback) -> int: buffer = BytesOutputStream() diff --git a/librespot/metadata/AlbumId.py b/librespot/metadata/AlbumId.py index 894b9b6..0a3007e 100644 --- a/librespot/metadata/AlbumId.py +++ b/librespot/metadata/AlbumId.py @@ -22,8 +22,7 @@ class AlbumId(SpotifyId.SpotifyId): album_id = matcher.group(1) return AlbumId( Utils.bytes_to_hex(AlbumId._BASE62.decode(album_id, 16))) - else: - raise TypeError("Not a Spotify album ID: {}.f".format(uri)) + raise TypeError("Not a Spotify album ID: {}.f".format(uri)) @staticmethod def from_base62(base62: str) -> AlbumId: diff --git a/librespot/metadata/ArtistId.py b/librespot/metadata/ArtistId.py index 0eb0513..2729d72 100644 --- a/librespot/metadata/ArtistId.py +++ b/librespot/metadata/ArtistId.py @@ -22,8 +22,7 @@ class ArtistId(SpotifyId.SpotifyId): artist_id = matcher.group(1) return ArtistId( Utils.bytes_to_hex(ArtistId._BASE62.decode(artist_id, 16))) - else: - raise TypeError("Not a Spotify artist ID: {}".format(uri)) + raise TypeError("Not a Spotify artist ID: {}".format(uri)) @staticmethod def from_base62(base62: str) -> ArtistId: diff --git a/librespot/metadata/EpisodeId.py b/librespot/metadata/EpisodeId.py index c6542ad..44d6c56 100644 --- a/librespot/metadata/EpisodeId.py +++ b/librespot/metadata/EpisodeId.py @@ -20,8 +20,7 @@ class EpisodeId(SpotifyId.SpotifyId, PlayableId): return EpisodeId( Utils.Utils.bytes_to_hex( PlayableId.BASE62.decode(episode_id, 16))) - else: - TypeError("Not a Spotify episode ID: {}".format(uri)) + TypeError("Not a Spotify episode ID: {}".format(uri)) @staticmethod def from_base62(base62: str) -> EpisodeId: diff --git a/librespot/metadata/ShowId.py b/librespot/metadata/ShowId.py index 3678926..41a823f 100644 --- a/librespot/metadata/ShowId.py +++ b/librespot/metadata/ShowId.py @@ -22,8 +22,7 @@ class ShowId(SpotifyId.SpotifyId): show_id = matcher.group(1) return ShowId( Utils.bytes_to_hex(ShowId._BASE62.decode(show_id, 16))) - else: - raise TypeError("Not a Spotify show ID: {}".format(uri)) + raise TypeError("Not a Spotify show ID: {}".format(uri)) @staticmethod def from_base62(base62: str) -> ShowId: diff --git a/librespot/metadata/TrackId.py b/librespot/metadata/TrackId.py index fa29b2d..aae37cf 100644 --- a/librespot/metadata/TrackId.py +++ b/librespot/metadata/TrackId.py @@ -21,8 +21,7 @@ class TrackId(PlayableId, SpotifyId): track_id = search.group(1) return TrackId( Utils.bytes_to_hex(PlayableId.BASE62.decode(track_id, 16))) - else: - raise RuntimeError("Not a Spotify track ID: {}".format(uri)) + raise RuntimeError("Not a Spotify track ID: {}".format(uri)) @staticmethod def from_base62(base62: str) -> TrackId: diff --git a/librespot/player/codecs/AudioQuality.py b/librespot/player/codecs/AudioQuality.py index 495af88..80b7706 100644 --- a/librespot/player/codecs/AudioQuality.py +++ b/librespot/player/codecs/AudioQuality.py @@ -11,21 +11,20 @@ class AudioQuality(enum.Enum): @staticmethod def get_quality(audio_format: AudioFile.Format) -> AudioQuality: if audio_format == AudioFile.MP3_96 or \ - audio_format == AudioFile.OGG_VORBIS_96 or \ - audio_format == AudioFile.AAC_24_NORM: + audio_format == AudioFile.OGG_VORBIS_96 or \ + audio_format == AudioFile.AAC_24_NORM: return AudioQuality.NORMAL - elif audio_format == AudioFile.MP3_160 or \ - audio_format == AudioFile.MP3_160_ENC or \ - audio_format == AudioFile.OGG_VORBIS_160 or \ - audio_format == AudioFile.AAC_24: + if audio_format == AudioFile.MP3_160 or \ + audio_format == AudioFile.MP3_160_ENC or \ + audio_format == AudioFile.OGG_VORBIS_160 or \ + audio_format == AudioFile.AAC_24: return AudioQuality.HIGH - elif audio_format == AudioFile.MP3_320 or \ - audio_format == AudioFile.MP3_256 or \ - audio_format == AudioFile.OGG_VORBIS_320 or \ - audio_format == AudioFile.AAC_48: + if audio_format == AudioFile.MP3_320 or \ + audio_format == AudioFile.MP3_256 or \ + audio_format == AudioFile.OGG_VORBIS_320 or \ + audio_format == AudioFile.AAC_48: return AudioQuality.VERY_HIGH - else: - raise RuntimeError("Unknown format: {}".format(format)) + raise RuntimeError("Unknown format: {}".format(format)) def get_matches(self, files: list[AudioFile]) -> list[AudioFile]: file_list = [] diff --git a/librespot/standard/ByteArrayOutputStream.py b/librespot/standard/ByteArrayOutputStream.py index 0b290ff..25ed5c7 100644 --- a/librespot/standard/ByteArrayOutputStream.py +++ b/librespot/standard/ByteArrayOutputStream.py @@ -31,7 +31,7 @@ class ByteArrayOutputStream(OutputStream): if byte is not None and buffer is None and offset is None and length is None: self.internal_write(byte) return - elif byte is None and buffer is not None and offset is None and length is None: + if byte is None and buffer is not None and offset is None and length is None: offset = 0 length = len(buffer) elif not (byte is None and buffer is not None and offset is not None diff --git a/librespot/standard/DataInputStream.py b/librespot/standard/DataInputStream.py index 9267c35..0ce84ed 100644 --- a/librespot/standard/DataInputStream.py +++ b/librespot/standard/DataInputStream.py @@ -13,10 +13,9 @@ class DataInputStream(FilterInputStream, DataInput): length: int = None) -> int: if b is not None and offset is None and length is None: return self.input_stream.read(b, 0, len(b)) - elif b is not None and offset is not None and length is not None: + if b is not None and offset is not None and length is not None: return self.input_stream.read(b, offset, length) - else: - raise TypeError() + raise TypeError() def read_fully(self, b: bytearray = None, diff --git a/librespot/standard/InputStream.py b/librespot/standard/InputStream.py index ed265cd..242ff51 100644 --- a/librespot/standard/InputStream.py +++ b/librespot/standard/InputStream.py @@ -35,11 +35,10 @@ class InputStream(Closeable): return 0 self.ensure_open() return -1 - elif b is None and offset is None and length is None: + if b is None and offset is None and length is None: self.ensure_open() return -1 - else: - raise TypeError() + raise TypeError() def read_all_bytes(self): self.ensure_open() @@ -83,7 +82,7 @@ class InputStream(Closeable): length: int = None) -> int: if b is None and offset is None and length is None: return self.internal_read() - elif b is not None and offset is None and length is None: + if b is not None and offset is None and length is None: offset = 0 length = len(b) elif not (b is not None and offset is not None and length is not None): @@ -165,7 +164,7 @@ class InputStream(Closeable): remaining -= count return result - elif b is not None and offset is not None and length is not None: + if b is not None and offset is not None and length is not None: if len(b) < (offset + length): raise IndexError() @@ -176,8 +175,7 @@ class InputStream(Closeable): break n += count return n - else: - raise TypeError() + raise TypeError() def skip(self, n: int) -> int: remaining = n