Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm: Unreachable Code?

PyCharm is showing me that some code is unreachable within a method before the return statement is reached. I cannot help but wonder how is that even remotely possible?

def post(self):
    # get the desired parameters
    username = self.request.get('user')
    password = self.request.get('pass')

    if not self.REGEX.match(username) or not self.REGEX.match(password):
        logging.debug('RegistrationHandler: Bad credentials ->', username, password)
        self.fail('bad username or password')

        print 'Blah' # <---- shows as UNREACHABLE ?
        return # <---- shows as UNREACHABLE ?

self.fail simply calls self.response.write(things).

Update:

Yeah, when I surround it with a try/catch clause, the issue is resolved... Strange. (Note that the method doesn't always raise an exception.

like image 915
Mazyod Avatar asked Feb 22 '14 13:02

Mazyod


People also ask

Why is my code unreachable python?

In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.

What is unreachable code?

Unreachable code is an atom or sequence of atoms which cannot be executed because there is no way for the flow of control to reach that sequence of atoms. For example, in the following atom sequence the MUL, SUB, and ADD atoms will never be executed because of the unconditional jump preceding them.


1 Answers

I actually think this is a bug in PyCharm, thinking that fail refers to TestCase.fail, which would in fact make the code unreachable.

If I use your example, but rename fail to for example failure, the errors disappears. I'd report this bug to the friendly folks at PyCharm to see if this is in fact the case.

like image 110
Menno Avatar answered Sep 28 '22 03:09

Menno