sourcery-ai / sourcery-vscode

VS Code extension for Sourcery
MIT License
86 stars 10 forks source link

Sourcery mistakes function after a class in a script as member of the class - bug report #234

Closed Estrangeling closed 1 year ago

Estrangeling commented 1 year ago

I am using Visual Studio Code with Sourcery extension.

And I have encountered the bug multiple times, the bug is as follows:

Inside a script, if there is a class defined above a function, the function isn't a member of the class, Sourcery will mistake the function as a member of the class and show the following message:

Sourcery - The first argument to instance methods should be `self` sourcery(refactoring:instance-method-first-arg-name)

This is a bug, the function isn't part of the class definition. This can happen for various reasons, but the bug most frequently occurs when a class is added before a function in a script.

As an example:

class Button_Animation(QObject):
    stopped = pyqtSignal()
    change = pyqtSignal()
    update = pyqtSignal()

    def run(self):
        while GLOBALS["run"]:
            self.animate()

        self.reset()

    def animate(self):
        while GLOBALS["pause"]:
            QTest.qWait(42)

        choices = list(range(12))
        for name in TRINITY:
            choice = random.choice(choices)
            choices.remove(choice)
            BUTTONS[choice] = name

        texts = random.choices(TRINITY, k=9)
        for name, i in zip(texts, choices):
            BUTTONS[i] = name

        self.change.emit()
        self.update.emit()

    def reset(self):
        global BUTTONS
        BUTTONS = BUTTONS_COPY.copy()
        self.change.emit()
        self.update.emit()

def init_connection(obj, worker, key):
    thread = QThread()
    worker.moveToThread(thread)
    thread.started.connect(worker.run)
    worker.stopped.connect(thread.quit)
    GLOBALS[key] = worker
    obj.qthread = thread

2023-10-15_014057

The message goes away if I close the editor and reopen it. But it will easily resurface if I add a new class or edit a class defined before a function.

How can I fix this?

bm424 commented 1 year ago

Hi @Estrangeling, ~this looks very much like a bug with our pattern matching system. I'll investigate and we'll hopefully have a fix in the next release.~

This is the same as sourcery-ai/sourcery#328, so I'm going to close this issue in favour of the older one.