-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Add GitHub output format #20358
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
Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great!
I just have one minor concern, refer to inline comment.
::error title=Ruff (F401),file=fib.py,line=1,col=8,endLine=1,endColumn=10::fib.py:1:8: F401 `os` imported but unused | ||
::error title=Ruff (F841),file=fib.py,line=6,col=5,endLine=6,endColumn=6::fib.py:6:5: F841 Local variable `x` is assigned to but never used | ||
::error title=Ruff (F821),file=undef.py,line=1,col=4,endLine=1,endColumn=5::undef.py:1:4: F821 Undefined name `a` | ||
::error title=ty (F401),file=fib.py,line=1,col=8,endLine=1,endColumn=10::fib.py:1:8: F401 `os` imported but unused | ||
::error title=ty (F841),file=fib.py,line=6,col=5,endLine=6,endColumn=6::fib.py:6:5: F841 Local variable `x` is assigned to but never used | ||
::error title=ty (F821),file=undef.py,line=1,col=4,endLine=1,endColumn=5::undef.py:1:4: F821 Undefined name `a` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just confirming that this change is basically just a side effect of having ty as the default for Program
and is not because there's someplace where the program is not being set correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's right!
crates/ruff_db/src/diagnostic/mod.rs
Outdated
show_fix_status: false, | ||
show_fix_diff: false, | ||
fix_applicability: Applicability::Safe, | ||
program: Program::Ty, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think at this point, it might be useful to refactor this into a new
method that takes in a program: Program
as the sole parameter because there's no valid default value for this field and the caller should always provide the program for which this rendering is to be done for. What do you think?
crates/ruff_db/src/diagnostic/mod.rs
Outdated
fix_applicability: Applicability, | ||
/// The program for which diagnostics are being rendered (Ruff or ty). | ||
/// | ||
/// This is currently only used by the GitHub output format and defaults to `Program::Ty`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refer to my other comment, I'd probably remove the default value for this field.
crates/ruff_db/src/diagnostic/mod.rs
Outdated
#[derive(Clone, Debug)] | ||
pub enum Program { | ||
Ruff, | ||
Ty, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It would probably be fine to just use a String
here
crates/ruff_db/src/diagnostic/mod.rs
Outdated
/// The program for which diagnostics are being rendered (Ruff or ty). | ||
/// | ||
/// This is currently only used by the GitHub output format and defaults to `Program::Ty`. | ||
program: Program, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I brought this up before and it isn't something that we need to change in this PR but I personally find it a bit difficult to reason about which settings are used for which output format (most settings are only used by one or very few formats).
The main reason I see today why we need a single Config
struct is because DisplayDiagnostics
matches on the output format, which I'm not sure we should keep because we also match over the output format in ruff's rendering code (meaning, we first match to create the generic configuration only to then match on the format again to call into specific renderers). To me, it would feel simpler to just have a single match in ruff's printer that calls the corresponding renderer directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought eventually we would be able to hoist the Config
out of the match in Ruff and have something like DisplayDiagnosticsConfig::from(ruff_output_format)
and a single DisplayDiagnostics
call, but I guess you're right. We may not want to port over all of the output formats, and some other logic is mixed in on the Ruff side, such as the SHOW_FIX_SUMMARY
flag and writing of summary text. I can follow up on this.
write!( | ||
f, | ||
"::error title=Ruff ({code})", | ||
"::error title={program} ({code})", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to use the correct severity for ty diagnostics and not always default to error
Thanks for the reviews! I pushed one commit to use the real severity. I think Dhruv's suggestions about not having a default I briefly started that refactor on this branch, but I think it would help to finally make the |
## Summary This is a follow-up to #20358 to avoid adding a new `DisplayDiagnosticConfig` option (with a surprising default value) just to print the program name for GitHub diagnostics. Initially I expected this to be a larger refactor including making the various `Renderer::render` methods take a `std::io::Write` instead of a `std::fmt::Formatter` to make them easier to call directly in Ruff. However, this was a bit more painful than expected because we use the `Display` implementation of `DisplayDiagnostics` for more than writing directly to stdout. Implementing `Display` on the individual renderers seemed like a smaller step in this direction. At this point it might make sense just to merge this into #20358. Alternatively, I could apply the same transformation to the other affected renderers. For example, the JSON and JSON-lines formats only use their `config` fields for the `preview` setting. Or we could update all of the renderers and call them directly in Ruff instead of assembling a config at all. I just wanted to make sure this approach looked reasonable before applying it to everything. ## Test Plan Existing tests
Summary
This PR wires up the GitHub output format moved to
ruff_db
in #20320 to the ty CLI.It's a bit smaller than the GitLab version (#20155) because some of the helpers were already in place, but I did factor out a few
DisplayDiagnosticConfig
constructor calls in Ruff. I also exposed theGithubRenderer
and a wrapperDisplayGithubDiagnostics
type because we needed a way to configure the program name displayed in the GitHub diagnostics. This was previously hard-coded toRuff
:Another option would be to drop the program name in the output format, but I think it can be helpful in workflows with multiple programs emitting annotations (such as Ruff and ty!)
Test Plan
New CLI test, and a manual test with
--config 'terminal.output-format = "github"'