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

Ruff fix 'formatter' no longer works since 0.1.0 #331

Open
felix-cw opened this issue Nov 30, 2023 · 3 comments
Open

Ruff fix 'formatter' no longer works since 0.1.0 #331

felix-cw opened this issue Nov 30, 2023 · 3 comments

Comments

@felix-cw
Copy link
Contributor

This concerns using ruff check --fix to fix code (#296) instead of the new ruff format command.

Since ruff 0.1.0, the functionality no longer works. This is because since astral-sh/ruff#7838, ruff emits to stderr, which jupyterlab_code_formatter interprets as an error.

A potential fix is to add the -e and -q flags to the ruff call, which sets the exit code to 0 and suppresses stderr respectively.

class RuffFixFormatter(CommandLineFormatter):
    @property
    def label(self) -> str:
        return f"Apply ruff Formatter"

    def __init__(self):
        try:
            from ruff.__main__ import find_ruff_bin

            ruff_command = find_ruff_bin()
        except (ImportError, FileNotFoundError):
            ruff_command = "ruff"
        self.command = [ruff_command, "check", "-eq", "--fix-only", "-"]

I edited my installation of jupyterlab_code_formatter to the above and it works, but I haven't had a chance to test more systematically.

@axeltlarsson
Copy link

axeltlarsson commented Mar 19, 2024

@felix-cw You might be able to shed some light with an issue I'm having 🙏.

I'm using this workaround: https://gist.github.com/jbwhit/eecdd1cac2756df85ad165f437445b0b

I'm having some issues with the fact that formatting through jyputerlab_code_formatter doesn't format the same way as if running ruff format from the command line... Mildly infuriating to say the least, and makes it really difficult to use in CI. Any ideas why? It should use the same binary and version, it's just one goes through jyputerlab_code_formatter and the other doesn't as far as I can tell.
I'm also using jupytext, so maybe that's got something to do with it 🤔.

@felix-cw
Copy link
Contributor Author

I'm afraid not. I know that this extension formats cell by cell, wheras using ruff directly can potentially see the whole notebook file during formatting. Whether or not this makes a difference I don't know.

Another idea could be an environment thing? I tend to have jupyter lab and extensions installed in a "global" conda environment as a host and use kernels from per-project environments. I think that this extension uses the ruff in the jupyter lab environment own environment which could lead to mismatches if you have a similar setup. I don't recall having this problem though, maybe because any mismatches got caught by pre-commit before CI.

I've never used jupytext so not sure what the impact could be.

@axeltlarsson
Copy link

It's probably as you say, that this extension formats cell by cell. The diff I see most often is that ruff likes to remove empty cells, which this extension does not. Thanks for taking the time to respond!

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

No branches or pull requests

2 participants