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

Feature idea/question: cleanup conditionals on sys.version_info #488

Closed
neutrinoceros opened this issue Jul 15, 2021 · 3 comments · Fixed by #530
Closed

Feature idea/question: cleanup conditionals on sys.version_info #488

neutrinoceros opened this issue Jul 15, 2021 · 3 comments · Fixed by #530

Comments

@neutrinoceros
Copy link
Contributor

neutrinoceros commented Jul 15, 2021

This is more of a question at this point: I wonder if it would be possible to do this with pyupgrade.
If it is, I'll attempt to write a patch, otherwise you can just close this !

input

assuming we run pyupgrade with the --py39 flag

import sys

if sys.version_info < (3, 9):
    print("this should be cleaned up")
else:
    print("this is the message we should keep (dedent)")

if sys.version_info > (3, 8):
    print("this is the message we should keep (dedent)")
else:
    print("this should be cleaned up")

if sys.version_info >= (3, 8):
    print("this is the message we should keep (dedent)")
else:
    print("this should be cleaned up")

if sys.version_info <= (3, 9):
    print("nothing to do here")
else:
    print("both branches are still valid")

output

import sys

print("this is the message we should keep (dedent)")

print("this is the message we should keep (dedent)")

print("this is the message we should keep (dedent)")

if sys.version_info <= (3, 9):
    print("nothing to do here")
else:
    print("both branches are still valid")
@asottile
Copy link
Owner

there already is some amount of this that's done for --py3-plus but it gets complicated quickly! I think it's possible so I'll leave this open but I seem to remember it being very tricky

@neutrinoceros
Copy link
Contributor Author

So I've started looking into this, until I stumbled upon pyupgrade/_plugins/versioned_branches.py which looks a lot like a what I think I'm trying to write ? Why was it moved to a plugin and btw how can I use plugins in pyupgrade ? I see no mention of them in the documentation. In any case, do you confirm that it's a good place to start ? should I try to expand it or start a different plugin ?

@asottile
Copy link
Owner

asottile commented Sep 8, 2021

the plugins are entirely internal -- but a way to speed up and split up the code to manageable pieces

and yeah I would recommend starting with what's happening in that file (probably just extending it to support the additional use-cases)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants