sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.5k stars 65 forks source link

Unwrap a constant iterable constructor -> Syntax Errors #388

Closed amogorkon closed 9 months ago

amogorkon commented 9 months ago

Description

In my fuzzylogic library, I use a "factory pattern" for functions, delegating checks and pre-computing constants to the outer function, then returning the inner function that only takes a single value. One such function that is used to combine different functions is simple_disjoint_sum, which looks like this

def simple_disjoint_sum(*funcs):
    def F(z):
        M = {
            f(z) for f in funcs
        }  # a set of all membership values over all given functions to be iterated over
        return max(min((x, *({1 - y for y in M - set([x])} or (1 - x,)))) for x in M)
    return F

Sourcery detects "Unwrap a constant iterable constructor" as a problem and suggests to correct it to

return max(min((x, *{1 - y for y in M - {x}} or (1 - x,))) for x in M)

which results in multiple syntax errors - an unclosed (, unexpected token "or" and removing the ( would result in further syntax errors and other problems. I realize this is a pretty exotic problem, but you should be aware of it.

Debug Information

IDE Version: Version: 1.83.0-insider (user setup) Commit: bccfade64adb249f57c8fcf03cba41609f76ce5c Date: 2023-09-15T05:35:16.508Z Electron: 25.8.1 ElectronBuildId: 23779380 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Windows_NT x64 10.0.19045

Sourcery Version 1.9.0

bm424 commented 9 months ago

Hey @amogorkon, thanks for reporting this. I've reproduced it, and it looks to me not exactly like a bug in the refactoring (which as far as I can tell is just set([x]) -> {x}), but rather a problem with our handling of the parenthesis wrapping. We should be able to fix this fairly quickly, I'll get back to you soon.