Skip to content

Conversation

@rwgk
Copy link
Collaborator

@rwgk rwgk commented Jan 12, 2026

Add # isort: skip comment to the _version import in cuda_pathfinder/cuda/pathfinder/__init__.py to prevent ruff from inconsistently reordering the import based on whether _version.py exists.

Problem

Ruff's import sorting (I001) behavior is inconsistent when imports cannot be resolved. The _version.py file is generated by setuptools-scm during the build process, so its existence depends on build state:

  • When _version.py EXISTS (after a build):
    Ruff can resolve the import and wants to sort it alphabetically. Since _version comes after _headers alphabetically, ruff expects the import order: _dynamic_libs, _headers, _version.

  • When _version.py DOESN'T EXIST (after git clean -fdx or fresh clone):
    Ruff cannot resolve the import and may skip checking it entirely, especially if cached as "unresolvable". This leads to inconsistent behavior where the same code passes or fails depending on build state.

Root Cause

Ruff caches import resolution results. If the cache has stale data where _version.py didn't exist, ruff may skip checking that import. When the cache is refreshed or _version.py exists, ruff resolves it and wants to sort it, triggering I001 errors inconsistently.

Example (before this PR):

smc120-0004.ipp2a2.colossus.nvidia.com:/wrk/forked/cuda-python $ pre-commit run --all-files
ruff check...............................................................Failed
- hook id: ruff-check
- files were modified by this hook

Fixed 1 error:
- cuda_pathfinder/cuda/pathfinder/__init__.py:
    1 × I001 (unsorted-imports)

Found 1 error (1 fixed, 0 remaining).

ruff format..............................................................Passed
Check SPDX Headers.......................................................Passed
...

Solution

Add # isort: skip comment to explicitly tell ruff to skip sorting this import, regardless of file existence or cache state. This ensures consistent behavior in all scenarios:

  • After builds (when _version.py exists)
  • After git clean (when _version.py doesn't exist)
  • With fresh clones
  • With stale ruff cache

The import is placed after the other imports (where it logically belongs alphabetically) with the skip directive to prevent ruff from moving it.

Changes

  • Move _version import from top of file to after _headers imports
  • Add # isort: skip comment to prevent ruff from reordering the import

Note: This PR was extracted from PR #1454

Add # isort: skip comment to the _version import in
cuda_pathfinder/cuda/pathfinder/__init__.py to prevent ruff from
inconsistently reordering the import based on whether _version.py exists.

Problem:
Ruff's import sorting (I001) behavior is inconsistent when imports
cannot be resolved. The _version.py file is generated by setuptools-scm
during the build process, so its existence depends on build state:

- When _version.py EXISTS (after a build):
  Ruff can resolve the import and wants to sort it alphabetically.
  Since _version comes after _headers alphabetically, ruff expects
  the import order: _dynamic_libs, _headers, _version.

- When _version.py DOESN'T EXIST (after git clean -fdx or fresh clone):
  Ruff cannot resolve the import and may skip checking it entirely,
  especially if cached as "unresolvable". This leads to inconsistent
  behavior where the same code passes or fails depending on build state.

Root Cause:
Ruff caches import resolution results. If the cache has stale data
where _version.py didn't exist, ruff may skip checking that import.
When the cache is refreshed or _version.py exists, ruff resolves it
and wants to sort it, triggering I001 errors inconsistently.

Solution:
Add # isort: skip comment to explicitly tell ruff to skip sorting
this import, regardless of file existence or cache state. This ensures
consistent behavior in all scenarios:
- After builds (when _version.py exists)
- After git clean (when _version.py doesn't exist)
- With fresh clones
- With stale ruff cache

The import is placed after the other imports (where it logically
belongs alphabetically) with the skip directive to prevent ruff from
moving it.
@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Jan 12, 2026

Auto-sync is disabled for ready for review pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@rwgk
Copy link
Collaborator Author

rwgk commented Jan 12, 2026

pre-commit.ci run

@rwgk
Copy link
Collaborator Author

rwgk commented Jan 12, 2026

/ok to test

@rwgk rwgk requested a review from mdboom January 12, 2026 18:34
@github-actions
Copy link

Copy link
Contributor

@mdboom mdboom left a comment

Choose a reason for hiding this comment

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

LGTM. This is sure an annoying aspect of setuptools-scm.

@rwgk rwgk merged commit d369900 into NVIDIA:main Jan 12, 2026
80 checks passed
@rwgk rwgk deleted the pathfinder_version_import_resorting branch January 12, 2026 22:11
github-actions bot pushed a commit that referenced this pull request Jan 13, 2026
Removed preview folders for the following PRs:
- PR #1460
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