Skip to content
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

Use Ruff to do import sorting #340

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

Conversation

shwina
Copy link

@shwina shwina commented Jan 2, 2025

Sort imports using Ruff (invoking ruff linter will now sort imports in addition to existing linting).

It looks like so far, imports have been kept sorted by manual invocations of ruff (or isort). This PR makes it so that the existing pre-commit hook will also take care of sorting imports.

This PR is based on the feedback/discussion in NVIDIA/cccl#3230.

Copy link

copy-pr-bot bot commented Jan 2, 2025

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ksimpson-work
Copy link
Contributor

ksimpson-work commented Jan 2, 2025

I do not experience this bahaviour (the automatic pre-commit ruff linting does sort my imports).

(cuda_py) ksimpson@NV-3KWHSV3:~/dev/cuda-python/cuda_core$ git commit -m 'test'
ruff.....................................................................Failed

  • hook id: ruff
  • files were modified by this hook

Fixed 1 error:

  • cuda_core/tests/test_linker.py:
    1 × I001 (unsorted-imports)

Found 1 error (1 fixed, 0 remaining).

ruff-format..............................................................Passed

@shwina
Copy link
Author

shwina commented Jan 2, 2025

I do not experience this bahaviour (the automatic pre-commit ruff linting does sort my imports).

To clarify, what was the expected behaviour here? (i.e., which file had unsorted imports?)

@ksimpson-work
Copy link
Contributor

I just moved an "import pytest" after the cuda core experiemental imports in test_linker.py to observe that ruff would resort it after the commit

@shwina
Copy link
Author

shwina commented Jan 2, 2025

I just moved an "import pytest" after the cuda core experiemental imports in test_linker.py to observe that ruff would resort it after the commit

From the output, it looks like indeed ruff did sort it back to correct order:

Fixed 1 error:

cuda_core/tests/test_linker.py:
1 × I001 (unsorted-imports)

was that not actually the case?

@ksimpson-work
Copy link
Contributor

I might not correctly follow what you described above. My understanding is that when you make a commit, you are finding that ruff does not sort imports unless ytou trigger it manually. Is that correct?

@shwina
Copy link
Author

shwina commented Jan 2, 2025

Oh, I see - you are doing all this on main?

@ksimpson-work
Copy link
Contributor

yes this is on main

@shwina
Copy link
Author

shwina commented Jan 2, 2025

Understood - yeah you're absolutely right. It looks like ruff has already been configured to sort imports on main.

Apologies, I assumed this wasn't the case based on Leo's comment here.

--

I think perhaps I misunderstood Leo's ask, which may be about using a top-level pyproject.toml specifying the ruff config, that both cuda.core and cuda.bindings can inherit (same as we are doing in CCCL). @leofang could you confirm?

@ksimpson-work
Copy link
Contributor

No need to apologize! Thanks for looking into it. Let's see what leo says.

@leofang leofang self-requested a review January 3, 2025 02:30
@leofang leofang added support All things related to the project that can't be categorized P1 Medium priority - Should do labels Jan 3, 2025
@leofang leofang added this to the cuda.core beta 3 milestone Jan 3, 2025
Copy link
Member

@leofang leofang left a comment

Choose a reason for hiding this comment

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

Thanks, guys! Sorry for my late reply...

I think perhaps I misunderstood Leo's ask, which may be about using a top-level pyproject.toml specifying the ruff config, that both cuda.core and cuda.bindings can inherit (same as we are doing in CCCL). @leofang could you confirm?

Yes. What I had in mind was:

  1. Have a root pyproject.toml that's inherited by all of our namespace packages
  2. Gradually sync up all Python configs across the two repos (cuda-python & cccl), starting with Ruff but eventually we want to cover everything (ex: mypy CI: Enforce type checks #312).

WDYT?

# Copyright (c) 2025, NVIDIA CORPORATION.

[tool.ruff]
target-version = "py310"
Copy link
Member

Choose a reason for hiding this comment

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

copied from CCCL root toml:

Suggested change
target-version = "py310"
target-version = "py310"
fix = true
show-fixes = true

@@ -98,3 +99,6 @@ exclude = ["cuda/bindings/_version.py"]
"UP022",
"E402", # module level import not at top of file
"F841"] # F841 complains about unused variables, but some assignments have side-effects that could be useful for tests (func calls for example)

[tool.ruff.lint.isort]
known-first-party = ["cuda.parallel"]
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if this is how it works? My intention is to spell out the old and new layouts:

Suggested change
known-first-party = ["cuda.parallel"]
known-first-party = ["cuda.cuda", "cuda.cudart", "cuda.nvrtc", "cuda.bindings"]

@leofang
Copy link
Member

leofang commented Jan 4, 2025

Another question: This PR does not generate any diff that touches the code? Asking because previously @vzhurba01 had some requirements for how cuda.bindings should be formatted (for example, we should not format code that's autogenerated), and we'd like to preserve the requirements during code review (or, even better, through automated tools).

@leofang leofang requested a review from ksimpson-work January 4, 2025 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 Medium priority - Should do support All things related to the project that can't be categorized
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants