Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Jun 25, 2025

Summary

I think this should be the last step before combining OldDiagnostic and ruff_db::Diagnostic. We can't store a NoqaCode on ruff_db::Diagnostic, so I converted the noqa_code field to an Option<String> and then propagated this change to all of the callers.

I tried to use &str everywhere it was possible, so I think the remaining to_string calls are necessary. I spent some time trying to convert everything to &str but ran into lifetime issues, especially in the FixTable. Maybe we can take another look at that if it causes a performance regression, but hopefully these paths aren't too hot. We also avoid some to_string calls, so it might even out a bit too.

Test Plan

Existing tests

@ntBre ntBre added internal An internal refactor or improvement diagnostics Related to reporting of diagnostics. labels Jun 25, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jun 25, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre
Copy link
Contributor Author

ntBre commented Jun 25, 2025

The performance is a bit worse (-2% in the worst case) on the all-rules benchmarks, but it seems widely dispersed, so I think it may just be from the up-front NoqaCode::to_string calls for storing them in OldDiagnostic, not the later conversions.

@ntBre ntBre marked this pull request as ready for review June 25, 2025 21:38
@ntBre ntBre requested a review from MichaReiser June 25, 2025 21:38
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

This mostly looks good. I think we want to keep using Rule in noqa.rs. But I'm not sure if that's better done in a separate PR

I also suggest introducing a SecondaryCode struct. It's hard to tell what all the &str and String usages are (especially because there are so few comments)

.fold(
vec![],
|mut acc: Vec<((Option<NoqaCode>, &OldDiagnostic), usize)>, (code, message)| {
|mut acc: Vec<((Option<&str>, &OldDiagnostic), usize)>, (code, message)| {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should add a SecondaryCode struct which wraps a String I do find the &str makes the code harder to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I think if it wrapped a String we'd have to clone in OldDiagnostic::secondary_code right? I'll try it with a &str and see if we can use that everywhere. I definitely agree that it would be more readable, 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.

I think I was responding to these too early in the morning 🤦 I can store the SecondaryCode(String) on the OldDiagnostic and then use &SecondaryCode where I'm currently using &str. I'm working on that, and then I'll try the Rule changes on a separate branch!

@github-actions
Copy link
Contributor

github-actions bot commented Jun 26, 2025

mypy_primer results

No ecosystem changes detected ✅

ntBre added a commit that referenced this pull request Jun 26, 2025
Summary
--

See #18946 (comment).

Test Plan
--

Existing tests
ntBre and others added 2 commits June 26, 2025 17:01
@ntBre ntBre merged commit 96f3c8d into main Jun 27, 2025
36 checks passed
@ntBre ntBre deleted the brent/string-noqa-code branch June 27, 2025 15:36
dcreager added a commit that referenced this pull request Jun 27, 2025
* main:
  [ty] Make tuple instantiations sound (#18987)
  [`flake8-pyi`] Expand `Optional[A]` to `A | None` (`PYI016`) (#18572)
  Convert `OldDiagnostic::noqa_code` to an `Option<String>` (#18946)
  [ty] Fix playground (#18986)
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. internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants