bwhmather / ssort

Tool for automatically sorting python statements within a module
MIT License
359 stars 10 forks source link

Fix comprehension bindings and requirements when target conflicts with outer scope #98

Closed jgberry closed 1 year ago

jgberry commented 1 year ago

The scoping around comprehensions is a bit confusing and our logic doesn't capture it correctly. Consider this case which we currently sort incorrectly:

def f():
    g()
    return [g for g in range(10)]

def g():
    print("Called g")

In the above snippet, the g() invocation within f and the g reference within the list comprehension exist within two separate scopes. Therefore, the invocation of g() should NOT be considered a call to a variable within the local scope. That is, function f does indeed have a requirement on the global function g.

This PR fixes this scoping issue and introduces additional tests to assert correctness of these cases.