Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

else with try is not detected #1866

Closed
sobolevn opened this issue Feb 11, 2021 · 3 comments
Closed

else with try is not detected #1866

sobolevn opened this issue Feb 11, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@sobolevn
Copy link
Member

We have this function:

def is_literal(node) -> bool:
    try:
        ast.literal_eval(node)
    except ValueError:
        return False
    else:
        return True

It should raise:

  5:9      WPS503 Found useless returning `else` statement
  return True

Because it should be consistent with if rule. So, the desired version is:

def is_literal(node) -> bool:
    try:
        ast.literal_eval(node)
    except ValueError:
        return False
    return True
@sobolevn sobolevn added the bug Something isn't working label Feb 11, 2021
@sobolevn sobolevn added this to the Version 0.15.x milestone Feb 11, 2021
@sobolevn
Copy link
Member Author

What about for? It has different else semantics.

@sobolevn
Copy link
Member Author

Ok, here's the deal with for:

def wrapper():
    for x in ...:
        if ...:
            break
    else:
        return 1
    return 2

This is not the same as if, but still can be checked. How?

  • else triggers if break was not called and it will return 1
  • if break was called it will return 2

Here's the refactored version:

def wrapper():
    for x in ...:
        if ...:
            return 2
    return 1

@sobolevn
Copy link
Member Author

The same about while

sobolevn added a commit that referenced this issue Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant