flake8-executable: allow global flags in uv shebangs (EXE003)#22582
flake8-executable: allow global flags in uv shebangs (EXE003)#22582amyreese merged 4 commits intoastral-sh:mainfrom
Conversation
|
| if shebang.contains("python") | ||
| || shebang.contains("pytest") | ||
| || shebang.contains("uv run") | ||
| || (shebang.contains("uv") && shebang.contains("run")) |
There was a problem hiding this comment.
I'd prefer a stricter regex as mentioned in the original issue. We should also make the same change for uvx and uv tool run.
There was a problem hiding this comment.
I used switched out contains() for a regex check. It's 'stricter' than the contains() check but I didn't want to make it too strict. I initially got caught up in trying to account for every possible flag a user might use.... that turned into a monster. I chopped it down to this. Added some tests and included uvx and uv tool run.
8cfe319 to
71c144b
Compare
| use crate::comments::shebang::ShebangDirective; | ||
|
|
||
| static UV_RUN_REGEX: LazyLock<Regex> = | ||
| LazyLock::new(|| Regex::new(r"(?x) \b(?:uv|uvx|uv\s+tool)\b .* \s run \b").unwrap()); |
There was a problem hiding this comment.
uvx doesn't require run. So I don't thinkw e need to change anything for uvx.
For uv and uv tool, would a regex like this work uv\s+(?:--?[a-zA-Z][\w-]*(?:[=\s]\S+)?\s+)*run(?:\s+.*)?
There was a problem hiding this comment.
My bad on the uvx run thing. I took the run out of the test cases with uvx. I tried your suggested regex. When using uv tool, the regex didn't recognize tool, and run is still mandatory.
So, I updated the regex to use your flag logic the uv cases (requiring run) and uvx can stand alone. I thought about just making run optional, but that would cause problems.
There was a problem hiding this comment.
Oh, and I left comments in the regex, as you can see. I can take those out, if you think it looks cleaner without them.
71c144b to
8d049ba
Compare
amyreese
left a comment
There was a problem hiding this comment.
Probably needs a rebase onto latest.
| EXE005 Shebang should be at the beginning of the file | ||
| --> EXE003_uv.py:4:1 | ||
| | | ||
| 2 | print("hello world") | ||
| 3 | | ||
| 4 | #!/usr/bin/env uv --offline run | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 5 | print("offline") |
There was a problem hiding this comment.
Can you update the config for this snapshot test to exclude EXE005 since the test fixture is only testing EXE003?
There was a problem hiding this comment.
Yeah, sure thing. I can do this a little later.
There was a problem hiding this comment.
rebased, and got rid of that EXE005 noise.
8d049ba to
324737c
Compare
324737c to
1e552c3
Compare
|
@ntBre I updated the PR to split the EXE00x test cases to clean up the EXE005 errors from snapshots without needing |
ntBre
left a comment
There was a problem hiding this comment.
Looks good to me, just a couple of testing suggestions.
crates/ruff_linter/src/rules/flake8_executable/rules/shebang_missing_python.rs
Show resolved
Hide resolved
1e552c3 to
b39c2d9
Compare
* main: (209 commits) [ty] Defer base inference for functional `type(...)` classes (#22792) flake8-executable: allow global flags in uv shebangs (EXE003) (#22582) [ty] Add `replace-imports-with-any` option (#23122) Update html comments in mdtests (#23269) Apply ruff formatting to mdtests (#22935) [ty] Exclude test-related symbols from non-first-party packages in auto-import completions [ty] Refactor `CursorTest` helper to support site-packages [ty] Qualify inlay hint edit symbol when possibly referencing another variable (#23265) [ty] Avoid `UnionBuilder` overhead when creating a new union from the filtered elements of an existing union (#22352) [ty] Refactor TypedDict key assignment validation (#23262) [ty] Improve Python environment path documentation (#23256) [ty] loop control flow analysis using loop header definitions Prepare for 0.15.1 (#23253) Remove docker-run-action (#23254) [ty] Allow discovering dependencies in system Python environments (#22994) Ensure pending suppression diagnostics are reported (#23242) [`isort`] support for configurable import section heading comments (#23151) [ty] Fix method calls on subclasses of `Any` (#23248) [ty] Fix bound method access on `None` (#23246) Make range suppression test snapshot actually useful (#23251) ...
Summary
This PR allows the use of global flags (e.g.,
--quiet,--offline) inuvshebangs for ruleEXE003. Previously, flags placed betweenuvandruncaused a false positive "missing python" error. The logic has been updated to independently check for the presence ofuvandrunwithin the shebang directive.Fixes #21753
Test Plan
crates/ruff_linter/resources/test/fixtures/flake8_executable/EXE003_uv.pycoveringuv --quiet runanduv --offline run.