Skip to content

Feature request: Catch operations on nothingness #12511

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

Closed
LuminairPrime opened this issue Apr 2, 2022 · 4 comments
Closed

Feature request: Catch operations on nothingness #12511

LuminairPrime opened this issue Apr 2, 2022 · 4 comments
Labels
feature topic-reachability Detecting unreachable code

Comments

@LuminairPrime
Copy link

LuminairPrime commented Apr 2, 2022

Below are four examples of impossible code that operates on nothing. The latter two continue silently, throwing no errors. Cpython says it's out of the scope of the language, but mypy could catch it. (https://bugs.python.org/issue47202)

x =

for x in : print("This code is never reached")

while(None): print("This code is never reached")

emptylist: list[int] = []
for x in emptylist:
    if emptylist[x] == "This code is never reached":
        print("This code is never reached")
    else: print("This code is never reached")

I think the second two cases should throw a warning that they aren't doing anything and attached code will be thrown away. What do you think?

@JelleZijlstra JelleZijlstra added the topic-reachability Detecting unreachable code label Apr 2, 2022
@JelleZijlstra
Copy link
Member

The while(None) case is already caught by the --warn-unreachable option.

Iterating over an empty list isn't. Implementing this check would require significant work, because mypy doesn't currently track whether a list is always empty. It seems doable at least in simple cases like your example, but I'm not sure how valuable it is, since I don't think it's a common problem in real-world code.

@AlexWaygood
Copy link
Member

I think this is basically a duplicate of #8637

@AlexWaygood
Copy link
Member

I think this is basically a duplicate of #8637

Closing as a duplicate of #8637 due to lack of response from OP.

@LuminairPrime
Copy link
Author

I agree that checking for iteration over nothing would be a good feature in the future. I have nothing else to add because I can't build the feature ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-reachability Detecting unreachable code
Projects
None yet
Development

No branches or pull requests

3 participants