Skip to content

switch to ruff #5218

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

switch to ruff #5218

wants to merge 1 commit into from

Conversation

gvwilson
Copy link
Contributor

@gvwilson gvwilson commented Jun 5, 2025

This PR converts linting and formatting from black to ruff.

  • Remove requires-optional.txt (no longer used, version conflict was confusing).
  • Convert commands.py to use argparse instead of hand-rolled.
  • Rationalize imports in codegen/__init__.py (all at top level).
  • Update "dev" section of pyproject.toml to using ruff instead of black.
    • And to install inflect and requests.
  • Add uv.lock file used by uv.
  • Modify code reformatting in codegen/__init__.py to use ruff instead of black.
  • Add new function to use ruff to reformat existing code (run python commands.py format).
  • Add new function to use ruff to check code (run python commands.py lint).

TODO:

  1. This PR does not reformat generated code, tests, or utilities: that, and cleaning up all the problems identified by ruff, will be done once this PR is approved.

  2. This PR currently fails the build because Circle CI is expecting requires-optional.txt, which this PR removes. We need to decide if we're leaving build on Circle CI or moving it to GitHub.

@gvwilson gvwilson requested a review from emilykl June 5, 2025 13:48
@emilykl
Copy link
Contributor

emilykl commented Jun 5, 2025

@gvwilson big picture comments --

  • I have a STRONG preference for also committing the ruff-formatted files as part of this PR. (To avoid cluttering up this PR, the formatting changes themselves could be done on a separate branch which branches off of this one.) But if we don't do the actual formatting step as part of this PR, we'll miss issues and then have to fix them later once this is on main.

  • The CONTRIBUTING.md needs to be updated to remove references to requires-optional.txt, and it needs some general updates as well. I'd be happy to work on that.

  • Could we add the uv.lock in a separate PR? I'm not opposed, but CONTRIBUTING.md needs to be updated to explain how to use the lockfile in development, otherwise things will get messy.

@gvwilson gvwilson force-pushed the switch-to-ruff branch 3 times, most recently from 8bb233e to 6999c5d Compare June 6, 2025 12:53
-   Switch to `argparse` and clean up imports in `commands.py`.
-   Remove `requires-optional.txt` and `test_requirements/*`
    (put dependencies in `pyproject.toml` instead).
-   Remove `black` and use `ruff` instead.
-   Add `--noformat` option to `python commands.py codegen` for experimenting.
-   Pass output directory around to control what is formatted and linted.
-   Add `python commands.py format` to only do formatting.
-   Add `python commands.py lint` to check code.
-   Reformat comments in `codegen/*.py`.
-   Use double-quoted strings for (most) code generation.
-   Update various GitHub templates.
-   Update CONTRIBUTING.md to reflect switch from black to ruff:
    -   Reorganize sections.
    -   Rewrite chunks of prose.
    -   Replace mentions of `black` with mentions of `ruff`.
    -   And describe `uv` usage.

Note:

1.  Only the generated code is formatted and linted:
    -   `plotly/validators/**/*.py`
    -   `plotly/graph_objs/**/*.py`
    -   `plotly/graph_objects/__init__.py`

2.  The strings in the data used by code generation are (for example)
    `"'some_name'"` (i.e., have embedded single quotes). This PR does
    not try to fix that: instead, we rely on reformatting to turn all
    the single-quoted strings into double-quoted strings.

3.  `CONTRIBUTING.md` refers to `pre-commit` and the old `requires-optional.txt` installed it,
    but there isn't a `.pre-commit-config.yaml` file in this repository.
    Wuzzup?
"geopandas",
"inflect",
"jupyter",
"kaleido==1.0.0rc15",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"kaleido==1.0.0rc15",
"plotly[kaleido]",

- other pure-Python submodules are: `plotly.io` (low-level interface for
displaying, reading and writing figures), `plotly.subplots` (helper function
for layout of multi-plot figures)
- Tests are found in `plotly/tests`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- Tests are found in `plotly/tests`.
- Tests are found in `tests/`.

Comment on lines +41 to +42
- The documentation is in this repository,
and its structure is described in [its README file](doc/README.md).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- The documentation is in this repository,
and its structure is described in [its README file](doc/README.md).
- Documentation is found in `doc/`, and its structure is described in [its README file](doc/README.md).

https://github.com/plotly/plotly.py/issues/1965. If you have writing skills,
the wording of existing examples can also be improved in places.
Code and documentation are not the only way to contribute:
you can also hepl the project by:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
you can also hepl the project by:
you can also help the project by:


- reporting bugs (see below).
- Submitting feature request (also at <https://github.com/plotly/plotly.py/issues>).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- Submitting feature request (also at <https://github.com/plotly/plotly.py/issues>).
- Submitting a feature request (also at <https://github.com/plotly/plotly.py/issues>).

## Have a Bug Report?
This section explains how to set up a development environment so that you can contribute code and/or tests.
Note that if you are modifying a single documentation page,
you can do it directly on Github by clicking on the "Edit this page on GitHub" link without cloning the repository.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
you can do it directly on Github by clicking on the "Edit this page on GitHub" link without cloning the repository.
you can do it directly on GitHub by clicking on the "Edit this page on GitHub" link without cloning the repository.

## Have Questions about Plotly?
We use Git and GitHub to manage our project;
if you are not familiar with them,
there are great resources like <http://try.github.io/> to get your started.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
there are great resources like <http://try.github.io/> to get your started.
there are great resources like <http://try.github.io/> to get you started.

```bash
(plotly_dev) $ pip install -e .
```
This command also creates an *editable install* of plotly.py
Copy link
Contributor

@emilykl emilykl Jun 6, 2025

Choose a reason for hiding this comment

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

Suggested change
This command also creates an *editable install* of plotly.py
If using `conda` or `virtualenv`, you can install all packages with:
```bash
pip install -e '.[dev]'
```
These commands creates an *editable install* of plotly.py

Comment on lines +124 to +127
This repo uses [ruff](https://astral.sh/ruff) to format Python code consistently
and the [pre-commit](https://pre-commit.com/) library to manage a Git commit hook to run ruff prior to each commit.
Both pre-commit and ruff are included in `pyproject.toml`,
so you should have them installed already if you've been following along.
Copy link
Contributor

@emilykl emilykl Jun 6, 2025

Choose a reason for hiding this comment

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

We are not using pre-commit hooks currently

Suggested change
This repo uses [ruff](https://astral.sh/ruff) to format Python code consistently
and the [pre-commit](https://pre-commit.com/) library to manage a Git commit hook to run ruff prior to each commit.
Both pre-commit and ruff are included in `pyproject.toml`,
so you should have them installed already if you've been following along.
This repo uses [ruff](https://astral.sh/ruff) to format Python code consistently. Ruff is included in the `plotly[dev]` install, so you should have it installed already if you've been following along.

Comment on lines +129 to 147
To enable the ruff formatting Git hook,
run this command in your virtual environment:

```bash
(plotly_dev) $ pre-commit install
pre-commit install
```

Now, whenever you perform a commit, the Black formatter will run. If the formatter
makes no changes, then the commit will proceed. But if the formatter does make changes,
then the commit will abort. To proceed, stage the files that the formatter
modified and commit again.
ruff will now run automatically whenever you perform a commit.
The commit will proceed if the formatter makes no changes,
but will be aborted if it does.
To proceed,
add the files that the formatter modified and commit again.

If you don't want to use `pre-commit`, then you can run black manually prior to making
a PR as follows.
If you don't want to use `pre-commit`,
you can run ruff manually prior to making a PR with:

```bash
(plotly_dev) $ black .
ruff check .
```
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this section -- we're not currently using pre-commit hooks in the dev process, and the pre-commit install command fails because pre-commit is not in the dev requirements


```bash
$ python commands.py updateplotlyjs
pytest tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pytest tests
python -m pytest tests

```bash
python commands.py updateplotlyjsdev --devrepo reponame --devbranch branchname
pytest tests/test_plotly/test_plot.py
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pytest tests/test_plotly/test_plot.py
python -m pytest tests/test_plotly/test_plot.py


```bash
# In your plotly.js/ directory, prepare the package:
### Generating the JavaScript Bundle
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
### Generating the JavaScript Bundle
### Generating the JavaScript Bundles for Jupyter

Comment on lines -261 to 195
$ npm pack
$ mv plotly.js-*.tgz plotly.js.tgz
To test `go.FigureWidget` locally, you'll need to generate the JavaScript bundle as follows:

# In your plotly.py/ directory:
$ python commands.py updateplotlyjsdev --local /path/to/your/plotly.js/
```
cd js
npm install && npm run build
```
Copy link
Contributor

Choose a reason for hiding this comment

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

delete

```bash
pytest -v tests/
```
### CircleCI Release
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
### CircleCI Release
### Using a development branch of Plotly.js

Comment on lines +62 to +63
"numpy",
"pandas",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"numpy",
"pandas",
"numpy",
"orjson",
"pandas",

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

Successfully merging this pull request may close these issues.

2 participants