1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to bulk remove trivial if statements

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working on refactoring a big codebase and part of the refactor includes removing some checks. I couldn't figure out a way to do this efficiently. For example if the old code was something like:

    if A:
    print("branch 1")
    else:
    print("branch 2")


    I want to delete A and make it always True. So my new code would look something like:

    if True:
    print("branch 1")
    else:
    print("branch 2") # unreachable


    Now I want to simplify the code into:

    print("branch 1")


    A few different scenarios that complicate this: Scenario 1:

    if A and B:
    print("branch 1")
    else:
    print("branch 2")


    would become:

    if B:
    print("branch 1")
    else:
    print("branch 2")


    Scenario 2:

    if A:
    print("branch 1")
    return
    print("branch 2")


    would become:

    print("branch 1")
    return


    I couldn't figure out a way to do it efficiently. I tried running a few different linters to see if any of them would suggest the simplification, but got no luck. I am also not good enough doing codemods to write my own script

    Continue reading...

Compartilhe esta Página