11

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.

1
  • 2
    When I change my method name from fail, the issue is actually resolved... Maybe PyCharm trying to do something interesting with fail? It think fail raises an exception?
    – Mazyod
    Commented Feb 22, 2014 at 13:33

3 Answers 3

15

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.

1
  • I used failure when I mentioned that I changed it.. Interesting. I was really curious where that came from, thanks.
    – Mazyod
    Commented Feb 22, 2014 at 14:02
0

This code is unreachable less...

Inspection info: This inspection detects code which can not be normally reached. import random from typing import List, Any

while True:
x: List[Any] = list(str(random.sample(range(1001, 10000), 1)))
x.remove("[")
x.remove("]")

print(x)
0

check the indentation of your function and the statement/line which is unreachable.

I used it like the second function is included in the first functions' indentation

How the code should be when indentation removed

then I removed the indentation before if statement and I got the result. . . . . . . . . You are Welcome!

Not the answer you're looking for? Browse other questions tagged or ask your own question.