Skip to content

[WIP] Add ruff configuration to pyproject.toml #9586

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

Closed
wants to merge 102 commits into from

Conversation

jlapeyre
Copy link
Contributor

@jlapeyre jlapeyre commented Feb 15, 2023

EDIT I will close this PR as we can still refer to it when closed. But it will continue to cause confusion if it remains open.

EDIT This PR is obsolete, replaced by #10116 . The latter PR is a much smaller, more incremental way forward. I am leaving this open as a draft because the discussion will be useful for possible further ruff PRs.

This PR is an experiment in using ruff, a linting tool, as suggested by @mtreinish (#1179 (comment)). The intent is to allow people to experiment with ruff on qiskit-terra without committing yet to using it in CI.

This PR replaces pylint with ruff for linting qiskit-terra. I have added several more lint tests to the ruff configuration that are not included in this PR. They will be included in a following PR, as this one is already fairly large.

This PR should not be merged before #9689 and #9690 are. I'll also remove rules that reviewers object to or find questionable. Some of these can be reviewed in a subsequent PR.

The main , or perhaps only, advantage that we expect at present is an increase in performance over pylint. The gain is indeed large; two or three orders of magnitude. Using ruff also makes it easy to add pyflake plugins (reimplemented in Rust) that execute very quickly. On my thinkpad ruff check qiskit test finishes in about 350ms with an empty cache 28ms.

The set of lint checks included in this PR is not the same as the previous (pylint) set. And neither is a subset of the other.

Configuration

I have configured ruff in pyproject.toml. Alternatively, we could use a ruff-only config file. tox is configured to use ruff rather than pylint. CI has also been switched from pylint to ruff.

It includes a bit of configuration in pyproject.toml. It also disables some checks on a line-by-line basis with # noqa: XXX.

TODO

  • Make single overrides inline. I disabled some tests in particular files via configuration in pyproject.toml. These ought to done instead via # noqa: comments in the files.
  • Make all python files ruff clean You can run > ruff check test. But you'll get many errors, as I did not yet correct or disable these.
    ~~

Notes

  • I did not see a way to use text representation of the error code when disabling, as you can do with pylint. Rather I have to use the error code, for example E401. I don't prefer this because it may not be obvious what you are disabling. But maybe there is a way to use a semantic tag. [EDIT: It appears there is no way.]
  • I corrected many of the lint errors. Most of them should be non-controversial. (But not all, I suppose) Anything controversial or more a matter of convention, I left for later discussion.

@coveralls

This comment was marked as off-topic.

@@ -114,10 +114,10 @@ def complete_meas_cal(

def tensored_meas_cal(
mit_pattern: List[List[int]] = None,
qr: Union[int, List["QuantumRegister"]] = None,
cr: Union[int, List["ClassicalRegister"]] = None,
qr: Union[int, List["QuantumRegister"]] = None, # noqa: F821
Copy link
Contributor

Choose a reason for hiding this comment

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

The docs say that F821 is an "undefined name error" -- do you think this is that because the type is in quotes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. That's it. I did not track down the reasoning. Of course, the name is in quotes precisely because the symbol is not defined. Maybe some people don't think this should be done, so there is a rule ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Cryoris Just because of this issue, I have reverted the application of this rule in 80b078b . There may be a way to sort this out, make a global exception or something. But for now, it's just in the way here.

@mtreinish mtreinish linked an issue Feb 15, 2023 that may be closed by this pull request
@mtreinish mtreinish added type: qa Issues and PRs that relate to testing and code quality Changelog: None Do not include in changelog labels Feb 15, 2023
@mtreinish
Copy link
Member

mtreinish commented Feb 15, 2023

So the interesting thing is black isn't happy with this PR:

https://dev.azure.com/qiskit-ci/qiskit-terra/_build/results?buildId=43701&view=logs&j=86d13450-b8b4-5d9a-22f1-9f7e71062a71&t=be4bcae7-a989-5a68-9de7-524b8fbabf91&l=12

Can you also update the tox.ini, Makefile, and .azure/lint-linux.yml to use ruff instead of pylint so we can see what using ruff looks like in ci?

Also we probably should add .ruff_cache/ to the .gitignore too.

"qiskit/transpiler/passes/scheduling/calibration_creators.py" = ["F401"]
"qiskit/transpiler/passes/scheduling/rzx_templates.py" = ["F401"]


Copy link
Member

Choose a reason for hiding this comment

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

Per the ruff docs and some of your comments on docstrings in #1179 I'm wondering if we want to add

[tool.ruff.pydocstyle]
convention = "google"

here too

https://github.com/charliermarsh/ruff#does-ruff-support-numpy--or-google-style-docstrings

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's certainly worth trying.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added some doc string checks. But in order to make this more manageable, I plan to instead defer them to a later PR (provided we actually make a switch to ruff) However, several doc string errors/suggestions made by ruff have been merged in recent PRs.

@Qiskit Qiskit deleted a comment from qiskit-bot Feb 15, 2023
@jakelishman
Copy link
Member

I think Black's problems are real, and somewhat unrelated to Ruff - in the couple of places I spot-checked, it's because there's been a suppression comment added that doesn't follow the Black rules. Same-line comments need to be separated from the content by two spaces. Black should be able to autofix them.

@jlapeyre
Copy link
Contributor Author

Same-line comments need to be separated from the content by two spaces

That should be compatible with ruff.

@jakelishman
Copy link
Member

Yeah sorry, I'm just saying that I don't think you've run black is all - no big deal.

@jlapeyre
Copy link
Contributor Author

I'm just saying that I don't think you've run black is all

That's right. I was not clear on that point. I did not yet run black. But I need to do that.

@levbishop
Copy link
Member

Also we probably should add .ruff_cache/ to the .gitignore too.

Ruff generates a .gitignore in its cache directory so it takes care of this automatically, I think.

@jlapeyre jlapeyre changed the title Add ruff configuration to pyproject.toml [WIP] Add ruff configuration to pyproject.toml Mar 1, 2023
@jlapeyre jlapeyre marked this pull request as draft May 16, 2023 17:42
@jlapeyre
Copy link
Contributor Author

The comments here remain useful, but I am closing this PR to avoid confusion. The current good PR is #10116

@jlapeyre jlapeyre closed this May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog type: qa Issues and PRs that relate to testing and code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch from pylint to flake8
7 participants