Skip to content

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Mar 24, 2025

Addresses #4863 with isort, as recommended by @maffoo in that issue's discussion. This PR integrates it into format-incremental (and respects the --apply arg) rather than having its own "sort-imports" script, as it seems like better developer ergonomics.

This PR just adds the tool and updates format-incremental. It doesn't change any existing imports, since that can be done separately. Once the PR is merged, it will be possible to run isort . and open a new PR that has all the imports sorted consistently. I've tried this locally and it works (and hits 100+ files).

__init__.py files are excluded for now, because in some of them, reordering the imports breaks things. That should probably be addressed later.

Fixes #4863

@daxfohl daxfohl marked this pull request as ready for review March 24, 2025 21:49
@daxfohl daxfohl requested review from vtomole and a team as code owners March 24, 2025 21:49
Copy link

codecov bot commented Mar 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.14%. Comparing base (ea1679d) to head (e949485).
Report is 11 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7181    +/-   ##
========================================
  Coverage   98.13%   98.14%            
========================================
  Files        1098     1100     +2     
  Lines       95812    96191   +379     
========================================
+ Hits        94029    94409   +380     
+ Misses       1783     1782     -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@daxfohl daxfohl marked this pull request as draft March 24, 2025 22:14
@pavoljuhas
Copy link
Collaborator

For some reason this does not recognize cirq* packages as self imports and groups them with third-party imports. Perhaps some extra option can fix that?

Example after isort run:

diff --git a/cirq-aqt/cirq_aqt/aqt_device_metadata.py b/cirq-aqt/cirq_aqt/aqt_device_metadata.py
index 82137d37..f75b6e85 100644
--- a/cirq-aqt/cirq_aqt/aqt_device_metadata.py
+++ b/cirq-aqt/cirq_aqt/aqt_device_metadata.py
@@ -19,6 +19,5 @@ from typing import Any, Iterable, Mapping
 
-import networkx as nx
-
 import cirq
 from cirq_aqt import aqt_target_gateset
+import networkx as nx
 

@daxfohl daxfohl marked this pull request as ready for review March 24, 2025 23:00
@daxfohl
Copy link
Collaborator Author

daxfohl commented Mar 24, 2025

@pavoljuhas I added a known_first_party config. Does that sort as you'd expect? (I don't work with Python much and don't know the conventions).

Allow redirect the output to file without terminal color codes.
And disregard extra line in `black --version` output.
Also move the `tool.isort` section in pyproject.toml per alphabetic order.
@pavoljuhas
Copy link
Collaborator

pavoljuhas commented Mar 26, 2025

@pavoljuhas I added a known_first_party config. Does that sort as you'd expect? (I don't work with Python much and don't know the conventions).

Yes, looks good. I test ran isort . and skimmed over 50 files or so (overall it changes 640 files).

I took the liberty with adding a few tweaks in 8570d70...e949485, the main one is to ignore the machine-generated *_pb2.py files. PTAL and adjust if necessary.

Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

LGTM, please check if you are OK with a couple of tweaks I pushed to the PR.

Thank you for taking care of this!

@daxfohl daxfohl added this pull request to the merge queue Mar 26, 2025
Merged via the queue into quantumlib:main with commit 3b268c2 Mar 26, 2025
38 checks passed
@daxfohl daxfohl deleted the isort branch March 26, 2025 20:14
pavoljuhas added a commit to pavoljuhas/Cirq that referenced this pull request Mar 26, 2025
The `check/format-incremental` needs more work.
isort invocation should skip `__init__.py` files, but it does not.

Related to quantumlib#7181
github-merge-queue bot pushed a commit that referenced this pull request Mar 26, 2025
* CI - skip sorting of imports with isort

The `check/format-incremental` needs more work.
isort invocation should skip `__init__.py` files, but it does not.

Related to #7181

* Add TODO note for re-enabling isort
pavoljuhas added a commit to pavoljuhas/Cirq that referenced this pull request Mar 27, 2025
Set the `CI` environment variable to get the `--color` option unconditionally.
Also check if `isort` is invoked.

Follow-up to quantumlib#7181
github-merge-queue bot pushed a commit that referenced this pull request Mar 27, 2025
…7196)

Set the `CI` environment variable to get the `--color` option unconditionally.
Also check if `isort` is invoked.

Follow-up to #7181
mhucka added a commit to mhucka/Cirq that referenced this pull request Mar 27, 2025
This adds information about `isort`'s use in Cirq, now that PR quantumlib#7181
has been merged and `isort` is used in the check/ scripts.
github-merge-queue bot pushed a commit that referenced this pull request Mar 28, 2025
* Add citation for Tesseract Decoder

* Add Tesseract Decoder & remove Rigetti

* Remove statement about cirq-rigetti

It seems like the Docker container may not be used solely for the
Rigetti module, so I left the other text about Docker and only removed
the statement involving Rigetti.

* Add mention of Tesseract & fix a couple of tiny niggles

* Remove mention of labels no longer used

This entire section still needs a good re-think and more revisions.
For now, I just removed mention of the labels that have been changed.

* Repeat the statement about vendor modules

It's true that the section on what's covered mentioned that vendor
modules are not, but it seemed strange that it was not mentioned in
the section that is specifically about what's no covered.

* Edit for typos

Mostly fix capitalization and occasional missing hyphens.

* Add notices that cirq-rigetti is deprecated

* Small edits for grammar and clarity

* Move paragraph about hardware modules higher in the list

Putting the bullet item about hardware modules seems higher in the
list seems like it's better, so that the flow of concepts goes
somewhare more general to somewhat more specific.

* Trivial edits to rewrite a phrase & add formatting

* Fix trivial typos

* Partially rewrite support.md

Changes:

* There were 3 H1 headings, and at the same time, the first heading
  did not match what is in the TOC. I changed this to have one H1
  heading that matches the TOC text, made the remaining headings H2,
  and added a lead-in paragaraph

* Rewrote the section about using the issue tracker in attempt to be
  more specific and clear.

* Added a section about using the Q.C. Stack Exchange.

* Added a section about using email to contact us.

* Fix minor inconsistent heading content style

A trivial change to match the way other headings were written.

* Update triage description

Removed more no-longer-existing issue labels, and rewrote section on
stale issue/PR heading to be more succinct.

* Fix trivial typos

Capitalization and placement of colon characters.

* Run spell checker

* Fix a few more typos

* Fix a few more tiny typos

* Take out Tesseract for now

It has no Cirq interface, and it's a bit too early stage still.

* Update list of software

* Spell out LBL and ORNL

* Mildly edit another description

* Fix misspelled name

* Remove cirq-rigetti from list of example modules

It's not essential, and removing it now will help reduce the number of
places we have to update when we remove Rigetti altogether.

* Make note of deprecation status in more places

* Update the section on Python imports

This adds information about `isort`'s use in Cirq, now that PR #7181
has been merged and `isort` is used in the check/ scripts.

* Fix typo

* Shorten the import ordering section

---------

Co-authored-by: Pavol Juhas <juhas@google.com>
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
* Add isort for import linting

* Fix isort color output

* Fix np2 test

* Add cirq as known first-party

* Add "cirq*" as known first-party

* Colorize formatters output only if going to terminal or GHA log

Allow redirect the output to file without terminal color codes.

* Use long-form isort option for verbosity

And disregard extra line in `black --version` output.

* isort - skip generated Python protobuf sources

Also move the `tool.isort` section in pyproject.toml per alphabetic order.

---------

Co-authored-by: Pavol Juhas <juhas@google.com>
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
* CI - skip sorting of imports with isort

The `check/format-incremental` needs more work.
isort invocation should skip `__init__.py` files, but it does not.

Related to quantumlib#7181

* Add TODO note for re-enabling isort
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
…uantumlib#7196)

Set the `CI` environment variable to get the `--color` option unconditionally.
Also check if `isort` is invoked.

Follow-up to quantumlib#7181
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
…mlib#7175)

* Add citation for Tesseract Decoder

* Add Tesseract Decoder & remove Rigetti

* Remove statement about cirq-rigetti

It seems like the Docker container may not be used solely for the
Rigetti module, so I left the other text about Docker and only removed
the statement involving Rigetti.

* Add mention of Tesseract & fix a couple of tiny niggles

* Remove mention of labels no longer used

This entire section still needs a good re-think and more revisions.
For now, I just removed mention of the labels that have been changed.

* Repeat the statement about vendor modules

It's true that the section on what's covered mentioned that vendor
modules are not, but it seemed strange that it was not mentioned in
the section that is specifically about what's no covered.

* Edit for typos

Mostly fix capitalization and occasional missing hyphens.

* Add notices that cirq-rigetti is deprecated

* Small edits for grammar and clarity

* Move paragraph about hardware modules higher in the list

Putting the bullet item about hardware modules seems higher in the
list seems like it's better, so that the flow of concepts goes
somewhare more general to somewhat more specific.

* Trivial edits to rewrite a phrase & add formatting

* Fix trivial typos

* Partially rewrite support.md

Changes:

* There were 3 H1 headings, and at the same time, the first heading
  did not match what is in the TOC. I changed this to have one H1
  heading that matches the TOC text, made the remaining headings H2,
  and added a lead-in paragaraph

* Rewrote the section about using the issue tracker in attempt to be
  more specific and clear.

* Added a section about using the Q.C. Stack Exchange.

* Added a section about using email to contact us.

* Fix minor inconsistent heading content style

A trivial change to match the way other headings were written.

* Update triage description

Removed more no-longer-existing issue labels, and rewrote section on
stale issue/PR heading to be more succinct.

* Fix trivial typos

Capitalization and placement of colon characters.

* Run spell checker

* Fix a few more typos

* Fix a few more tiny typos

* Take out Tesseract for now

It has no Cirq interface, and it's a bit too early stage still.

* Update list of software

* Spell out LBL and ORNL

* Mildly edit another description

* Fix misspelled name

* Remove cirq-rigetti from list of example modules

It's not essential, and removing it now will help reduce the number of
places we have to update when we remove Rigetti altogether.

* Make note of deprecation status in more places

* Update the section on Python imports

This adds information about `isort`'s use in Cirq, now that PR quantumlib#7181
has been merged and `isort` is used in the check/ scripts.

* Fix typo

* Shorten the import ordering section

---------

Co-authored-by: Pavol Juhas <juhas@google.com>
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.

Add module import linter in codebase
2 participants