Skip to content

Expose Ruff's public API as a Python library #659

Open
@charliermarsh

Description

Activity

facundobatista

facundobatista commented on Nov 25, 2022

@facundobatista

This would mean that we could do something like the following, right?

import ruff
errors = ruff.check_files(list_of_paths)
...

Thanks!

charliermarsh

charliermarsh commented on Nov 25, 2022

@charliermarsh
MemberAuthor

Yup, that's right!

charliermarsh

charliermarsh commented on Nov 25, 2022

@charliermarsh
MemberAuthor

@messense - I wasn't certain on this last time -- if we bundle a Python API with Ruff, will we need to build separate wheels for every Python version?

messense

messense commented on Nov 25, 2022

@messense
Contributor

If you can use abi3 features, one wheel per platform, otherwise you need to build separate wheels for every Python version.

charliermarsh

charliermarsh commented on Nov 25, 2022

@charliermarsh
MemberAuthor

Awesome thank you. I think we should be able to do that, so maybe this will be really straightforward.

provinzkraut

provinzkraut commented on Dec 8, 2022

@provinzkraut

Hello there, do you happen to have a rough timeline for when (if?) this is going to happen? I'm looking to integrate ruff into a tool I'm developing, which would require an API of some sort. It would be very helpful to know if this is something I can wait on, or look for another solution / workaround!

added this to the Release 0.1.0 milestone on Dec 8, 2022
charliermarsh

charliermarsh commented on Dec 8, 2022

@charliermarsh
MemberAuthor

@provinzkraut - It's definitely going to happen! I could probably ship it within the next week or so. I'd just been punting on it until I had more people asking for it.

Could I hear a bit more about your use-case, if you don't mind sharing?

provinzkraut

provinzkraut commented on Dec 8, 2022

@provinzkraut

@charliermarsh That's good to hear!

Could I hear a bit more about your use-case, if you don't mind sharing?

Sure. I'm working on a markdown extension to automatically generate pymdown tabs for different Python versions from a source version, i.e. generate 3.7, 3.8, 3.10 tabs from a 3.7 source (repo).

Currently I'm using pyupgrade to generate the versions and autoflake to clean imports that have become superfluous. Especially autoflake is quite slow, making up a majority of the extensions runtime. Since ruff is way faster at this, I'd like to use it (also one less dependency). I fiddled around with using the CLI version, but that's messy and a performance degradation.

charliermarsh

charliermarsh commented on Dec 9, 2022

@charliermarsh
MemberAuthor

@provinzkraut - Ok, cool. Let me see what I can do. I don't know if you're comfortable reading Rust, but would the current Rust public API suit your use-case, were it callable from Python with Python objects etc.?

charliermarsh

charliermarsh commented on Dec 9, 2022

@charliermarsh
MemberAuthor

In short: it takes a file path (to find the pyproject.toml), the raw Python source code, and an autofix setting, and returns a list of checks (which themselves include the raw fixes / patches).

I'm guessing that for your use-case, what you actually want is a function that takes source code (plus settings, to enable a list of checks) and returns fixed source code?

provinzkraut

provinzkraut commented on Dec 9, 2022

@provinzkraut

but would the current Rust public API suit your use-case, were it callable from Python with Python objects etc.?

I looked at this yesterday because I though that maybe it could be as simple as adding a tiny wrapper around the rust lib myself, but it seems to be a bit more involved. The current API doesn't really lend itself that well to my usecase.

I'm guessing that for your use-case, what you actually want is a function that takes source code (plus settings, to enable a list of checks) and returns fixed source code?

That would be ideal, yes. Dealing with a list of checks and extracting what I need from it also wouldn't be that big of an issue, but passing in configuration directly and omitting the config file is crucial, both for the needed configurability (I need to run the fixers with varying configuration for every invocation) and performance (I'm running the fixers many times on small snippets, which means the overhead of looking for and parsing a pyproject.toml every time adds up).

58 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreRelated to core functionalityhelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Expose Ruff's public API as a Python library · Issue #659 · astral-sh/ruff