Skip to content

[ruff] Add sub-diagnostics with permissions (RUF064)#22972

Merged
ntBre merged 9 commits intoastral-sh:mainfrom
11happy:add_info_non_octal_permission
Feb 2, 2026
Merged

[ruff] Add sub-diagnostics with permissions (RUF064)#22972
ntBre merged 9 commits intoastral-sh:mainfrom
11happy:add_info_non_octal_permission

Conversation

@11happy
Copy link
Contributor

@11happy 11happy commented Jan 30, 2026

Summary

Part of #17203, comment: #18541 (comment)

Test Plan

updated snapshot

Signed-off-by: Bhuminjay <bhuminjaysoni@gmail.com>
Signed-off-by: Bhuminjay <bhuminjaysoni@gmail.com>
@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 30, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you! This looks great overall. I just had two very small nits and a couple of suggestions about the info messages.

}
}

fn get_permissions(mode: u16) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: let's move this to the end of the file to keep the main rule implementation at the top.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 15 to 16
info: current value 444 will be interpreted as 0o674, permissions: u=rw-, g=rwx, o=r--
info: suggested value: 0o444, permissions: u=r--, g=r--, o=r--
Copy link
Contributor

Choose a reason for hiding this comment

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

I think something like this might sound slightly better:

Suggested change
info: current value 444 will be interpreted as 0o674, permissions: u=rw-, g=rwx, o=r--
info: suggested value: 0o444, permissions: u=r--, g=r--, o=r--
info: Current value of 444 (0o674) sets permissions: u=rw-, g=rwx, o=r--
info: Suggested value of 0o444 sets permissions: u=r--, g=r--, o=r--

I actually don't mind Micha's very short example from his earlier comment, but he said it needed better wording 😆

Copy link
Contributor Author

@11happy 11happy Jan 31, 2026

Choose a reason for hiding this comment

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

done, I also here particularly like those suggested ones . Should we move with that ?

info: 0oxxx (256) is u=rw, g=w, o=
info: 0o256 is u=....

these look particularly very concise & clean.

@ntBre ntBre added the diagnostics Related to reporting of diagnostics. label Jan 30, 2026

diagnostic.info(format!(
"current value {mode_literal} will be interpreted as 0o{:o}, permissions: {}",
mode & 0o777,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
mode & 0o777,
mode & 0o7777,

os.chmod() supports setting sticky/setuid/setgid bits as well as permissions, so it could be helpful to correctly display those bits as well. Not sure it's worth displaying them in the permissions part though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done changes to 0o7777 , should I add an extra info like special bits: <>, I am not very sure if we should add this ?

Signed-off-by: Bhuminjay <bhuminjaysoni@gmail.com>
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks! I pushed a few commits addressing the remaining nits I found, and I left one comment flagging something we should follow up on in the future, but this looks good to me.

Comment on lines 161 to 172
help: Replace with octal literal
info: Current value of mode 644 (0o1204) sets permissions: u=-w-, g=---, o=r--)
info: Suggested value of 420 sets permissions: u=rw-, g=r--, o=r--
21 | os.mkdir("foo", 600) # Error
22 | os.mkdir("foo", 0o600) # OK
23 |
- os.makedirs("foo", 644) # Error
24 + os.makedirs("foo", 0o644) # Error
25 | os.makedirs("foo", 0o644) # OK
26 |
27 | os.mkfifo("foo", 640) # Error
note: This is an unsafe fix and may change runtime behavior
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this needs to block this PR, I'm just flagging this for a follow-up, but I think this is our first usage of a sub-diagnostic in Ruff, and you can see that the info messages are emitted between the help message and the diff that was intended to come right after it.

@ntBre ntBre merged commit f1f21f7 into astral-sh:main Feb 2, 2026
41 checks passed
ntBre added a commit that referenced this pull request Feb 2, 2026
Summary
--

As noted in #22972 (comment),
attaching sub-diagnostics to Ruff's diagnostics can interact a bit awkwardly
with how we attach the `Violation::fix_title` and then render a fix diff. In
particular, the fix title is currently attached as a `help` sub-diagnostic when
the diagnostic is originally created, meaning that any additional
sub-diagnostics appear between the fix title and the rendered fix:

```
RUF064 [*] Non-octal mode
 --> RUF064.py:6:17
  |
4 | from pathlib import Path
5 |
6 | os.chmod("foo", 444)  # Error
  |                 ^^^
7 | os.chmod("foo", 0o444)  # OK
8 | os.chmod("foo", 7777)  # Error
  |
help: Replace with octal literal
info: Current value of mode 444 (0o674) sets permissions: u=rw-, g=rwx, o=r--)
info: Suggested value of 292 sets permissions: u=r--, g=r--, o=r--
3 | import os
4 | from pathlib import Path
5 |
  - os.chmod("foo", 444)  # Error
6 + os.chmod("foo", 0o444)  # Error
7 | os.chmod("foo", 0o444)  # OK
8 | os.chmod("foo", 7777)  # Error
9 | os.chmod("foo", 10000)  # Error
note: This is an unsafe fix and may change runtime behavior
```

Instead of adding the fix title immediately, this PR stores it on our
`DiagnosticGuard` type to be added just before the guard is dropped and the
diagnostic is stored.

I think a better long-term fix would be to attach the diff to the sub-diagnostic
with the `help` message/fix title somehow and render these `info`
sub-diagnostics _after_ the diff (or let the diagnostic author choose the
order), but this seemed like an easy improvement over the current approach, at
least.

Test Plan
--

Existing tests for ISC004 and from #22972 showing the `help` message at the end.
@ntBre ntBre changed the title add info for non_octal permissions [ruff] Add sub-diagnostics with permissions (RUF064) Feb 2, 2026
carljm added a commit that referenced this pull request Feb 2, 2026
* main: (48 commits)
  add info for non_octal permissions (#22972)
  Fix empty body rule rendering (#23039)
  [ty] Infer `ParamSpec` from class constructors for callable protocols (#22853)
  Update NPM Development dependencies (#23030)
  Update CodSpeedHQ/action action to v4.8.2 (#23029)
  [ty] remove special handling for `Any()` in match class patterns (#23011)
  Update Rust crate get-size2 to v0.7.4 (#23022)
  Update Rust crate insta to v1.46.1 (#23023)
  Update taiki-e/install-action action to v2.67.11 (#23033)
  Update Rust crate colored to v3.1.1 (#23031)
  Update cargo-bins/cargo-binstall action to v1.17.3 (#23028)
  Update Rust crate uuid to v1.20.0 (#23032)
  [ty] Avoid using `.node()` for detecting `Self` (#23000)
  Update Rust crate proc-macro2 to v1.0.106 (#23024)
  Update actions/setup-python action to v6.2.0 (#23027)
  [ty] fix query cycles in decorated function with parameter defaults (#23014)
  Update Rust crate quote to v1.0.44 (#23025)
  Update Rust crate thiserror to v2.0.18 (#23026)
  Update Rust crate filetime to v0.2.27 (#23021)
  Update Rust crate clearscreen to v4.0.3 (#23020)
  ...
@ntBre ntBre mentioned this pull request Feb 5, 2026
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Related to reporting of diagnostics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants