Search code examples
pythonpylint

pylint R1720: Unnecessary "elif" after "raise" (no-else-raise)


I have the following code

    if self.download_format == 'mp3':
        raise NotImplementedError
    elif self.download_format == 'wav':
        with NamedTemporaryFile(suffix='.wav') as wavfile:
            self.download_wav_recording(call, wavfile.name)
            convert_wav_to_mp3(wavfile.name, filename)

And pylint reports this error

R1720: Unnecessary "elif" after "raise" (no-else-raise)

What is the motivation for this error? why this code is not ok?


Solution

  •     if self.download_format == 'mp3':
            raise NotImplementedError
        elif self.download_format == 'wav':
            with NamedTemporaryFile(suffix='.wav') as wavfile:
                self.download_wav_recording(call, wavfile.name)
                convert_wav_to_mp3(wavfile.name, filename)
    

    This is equivalent to

        if self.download_format == 'mp3':
            raise NotImplementedError
        if self.download_format == 'wav':
          with NamedTemporaryFile(suffix='.wav') as wavfile:
              self.download_wav_recording(call, wavfile.name)
              convert_wav_to_mp3(wavfile.name, filename)
    

    Hence the warning from pylint

    The raise causes control flow to be disrupted - so you don't need to use elif and can use if instead