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

Add support for Ruff as a code-formatter #744

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft

Conversation

akaihola
Copy link
Owner

@akaihola akaihola commented Sep 17, 2024

@akaihola akaihola added the enhancement New feature or request label Sep 17, 2024
@akaihola akaihola added this to the Darker 3.1.0 milestone Sep 17, 2024
@akaihola akaihola self-assigned this Sep 17, 2024
@akaihola akaihola marked this pull request as draft September 17, 2024 07:39
@akaihola
Copy link
Owner Author

FYI @clintonsteiner @mrfroggg @Svenito support for Ruff as a code re-formatter is soon landing. This branch basically works already, we just need to add tests and documentation and clean up a bit. After that, code reviews would be highly appreciated! I'll invite you as collaborators on the repository just in case you might have time to review.

@akaihola akaihola force-pushed the ruff-plugin branch 3 times, most recently from 91c9c4e to 7772f5f Compare September 17, 2024 12:57
@clintonsteiner
Copy link
Collaborator

Sounds great! Let me know what I can do to help

@clintonsteiner
Copy link
Collaborator

I'd be happy to review, add tests, or add docs. Thank you!

@clintonsteiner
Copy link
Collaborator

I have some changes - would like to help.
Would you prefer I make a pull request on top of this branch?

@akaihola
Copy link
Owner Author

Hi @clintonsteiner, thanks for your reply! I just rebased this branch on master, feel free to make your pull requests on top of this branch.

Note that there's a stack of branches sitting on top of each other, and I'm planning to merge them one by one and rebase the latter ones on master after merging:

If some of your fixes are about #568 or #738, it's ok for me if you still push them to a PR on top of #744. I can then do the clean-up work and move them up to the correct branch.

But reviewing #568 and #738 would certainly also help!

@akaihola akaihola mentioned this pull request Sep 25, 2024
2 tasks
@akaihola akaihola force-pushed the ruff-plugin branch 11 times, most recently from cda86e6 to c828762 Compare September 25, 2024 20:05
@akaihola akaihola force-pushed the ruff-plugin branch 3 times, most recently from 9a1847d to ca1f93f Compare October 8, 2024 15:10
Copy link
Collaborator

@clintonsteiner clintonsteiner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left plenty of reviews, sorry just trying to be helpful
Feel free to ignore them as you disagree with them :)

src/darker/help.py Outdated Show resolved Hide resolved
src/darker/help.py Show resolved Hide resolved
src/darker/tests/test_command_line.py Outdated Show resolved Hide resolved
src/darker/tests/test_command_line.py Outdated Show resolved Hide resolved
src/darker/formatters/ruff_formatter.py Show resolved Hide resolved
src/darker/formatters/black_formatter.py Outdated Show resolved Hide resolved
src/darker/formatters/formatter_config.py Show resolved Hide resolved
src/darker/formatters/formatter_config.py Outdated Show resolved Hide resolved
src/darker/formatters/formatter_config.py Show resolved Hide resolved
src/darker/formatters/formatter_config.py Show resolved Hide resolved
This will make it easier to add support for pyupgrade.
This will make it easier to add support for pyupgrade.
This allows formatters to exclude based on file path even if we're
passing file contents on stdin and using --stdin-filename.
r"|\.tox"
r"|\.svn"
r"|\.venv"
r"|\.vscode"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe exclude all dot paths: .* ?

" (e.g. `formatter=none`)"
)
message = "Can't find the Black package"
raise DependencyError(message) from exc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen, if the internals of black will change? e.g.: renaming/moving of parse_pyproject_toml or re_compile_maybe_verbose ? Think the error message is not really good in this case.

So, what's about to import only black package and then do the real imports?

e.g.:

        try:
            import black
        except ImportError as exc:
            logger.warning(
                "To re-format code using Black, install it using e.g."
                " `pip install 'darker[black]'` or"
                " `pip install black`"
            )
            logger.warning(
                "To use a different formatter or no formatter, select it on the"
                " command line (e.g. `--formatter=none`) or configuration"
                " (e.g. `formatter=none`)"
            )
            message = "Can't find the Black package"
            raise DependencyError(message) from exc

        # Black is installed
        from black import (  # pylint: disable=import-outside-toplevel
            parse_pyproject_toml,
            re_compile_maybe_verbose,
        )

)
except ImportError as exc:
logger.warning(
"To re-format code using Black, install it using e.g."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this warning will be always displayed to the user?

# Local import so Darker can be run without Black installed.
# No need for error handling, already done in `BlackFormatter.read_config`.
from black import FileMode as Mode # pylint: disable=import-outside-toplevel
from black import TargetVersion # pylint: disable=import-outside-toplevel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move all import from black into a own package and handle import error there? So it's guaranteed that the own error message from above will always appear?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging this pull request may close these issues.

Support ruff format as a formatter
4 participants