Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Jul 3, 2025

Summary

This PR adds a dummy FileResolver to Ruff to enable using the ruff_db::Diagnostic rendering code and ports one of the simplest output formats (azure) to a ruff_db::DiagnosticFormat. This also made it very easy to add support for this output format to ty.

I think this will eventually allow us to rework, or even remove, the Emitter infrastructure in Ruff, but my plan for now is just to port the internals of each Emitter.

We didn't have any tests for this, but I removed this check:

let location = if context.is_notebook(&filename) {
// We can't give a reasonable location for the structured formats,
// so we show one that's clearly a fallback
LineColumn::default()
} else {
diagnostic.expect_ruff_start_location()
};

because the line numbers in notebooks look sensible to me. This matches the current behavior of ty too.

Test Plan

Existing Ruff tests and a new ty CLI test

@ntBre ntBre added internal An internal refactor or improvement diagnostics Related to reporting of diagnostics. ty Multi-file analysis & type inference and removed internal An internal refactor or improvement labels Jul 3, 2025
@ntBre
Copy link
Contributor Author

ntBre commented Jul 3, 2025

Not sure about the labels here 😆 it should be internal for Ruff, but I guess it's a new feature for ty.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 3, 2025

mypy_primer results

Changes were detected when running on open source projects
mypy_primer (https://github.com/hauntsaninja/mypy_primer)
-     memo fields = ~45MB
+     memo fields = ~41MB

Expression (https://github.com/cognitedata/Expression)
-     memo fields = ~54MB
+     memo fields = ~49MB

trio (https://github.com/python-trio/trio)
-     memo fields = ~156MB
+     memo fields = ~142MB

discord.py (https://github.com/Rapptz/discord.py)
-     memo fields = ~189MB
+     memo fields = ~207MB

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- TOTAL MEMORY USAGE: ~80MB
+ TOTAL MEMORY USAGE: ~88MB

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
-     memo fields = ~171MB
+     memo fields = ~156MB

bandersnatch (https://github.com/pypa/bandersnatch)
-     memo fields = ~66MB
+     memo fields = ~72MB

mkdocs (https://github.com/mkdocs/mkdocs)
-     memo fields = ~97MB
+     memo fields = ~106MB

psycopg (https://github.com/psycopg/psycopg)
- TOTAL MEMORY USAGE: ~228MB
+ TOTAL MEMORY USAGE: ~207MB

django-stubs (https://github.com/typeddjango/django-stubs)
- TOTAL MEMORY USAGE: ~171MB
+ TOTAL MEMORY USAGE: ~189MB

openlibrary (https://github.com/internetarchive/openlibrary)
- TOTAL MEMORY USAGE: ~228MB
+ TOTAL MEMORY USAGE: ~207MB

scipy (https://github.com/scipy/scipy)
- TOTAL MEMORY USAGE: ~1156MB
+ TOTAL MEMORY USAGE: ~1271MB

@github-actions
Copy link
Contributor

github-actions bot commented Jul 3, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre marked this pull request as ready for review July 3, 2025 19:14
@ntBre ntBre requested review from BurntSushi and dhruvmanila and removed request for carljm, dcreager and sharkdp July 3, 2025 19:14
@ntBre
Copy link
Contributor Author

ntBre commented Jul 3, 2025

I started working on JSON output after this, and I think we may end up needing the NotebookIndex anyway, so I should probably not delete that section of code. I'll convert this back to a draft while I figure it out.

@ntBre ntBre marked this pull request as draft July 3, 2025 21:48
@ntBre ntBre closed this Jul 4, 2025
ntBre added a commit that referenced this pull request Jul 11, 2025
…19133)

## Summary

This was originally stacked on #19129, but some of the changes I made
for JSON also impacted the Azure format, so I went ahead and combined
them. The main changes here are:

- Implementing `FileResolver` for Ruff's `EmitterContext`
- Adding `FileResolver::notebook_index` and `FileResolver::is_notebook`
methods
- Adding a `DisplayDiagnostics` (with an "s") type for rendering a group
of diagnostics at once
- Adding `Azure`, `Json`, and `JsonLines` as new `DiagnosticFormat`s

I tried a couple of alternatives to the `FileResolver::notebook` methods
like passing down the `NotebookIndex` separately and trying to reparse a
`Notebook` from Ruff's `SourceFile`. The latter seemed promising, but
the `SourceFile` only stores the concatenated plain text of the
notebook, not the re-parsable JSON. I guess the current version is just
a variation on passing the `NotebookIndex`, but at least we can reuse
the existing `resolver` argument. I think a lot of this can be cleaned
up once Ruff has its own actual file resolver.

As suggested, I also tried deleting the corresponding `Emitter` files in
`ruff_linter`, but it doesn't look like git was able to follow this as a
rename. It did, however, track that the tests were moved, so the
snapshots should be easy to review.

## Test Plan

Existing Ruff tests ported to tests in `ruff_db`. I think some other
existing ruff tests also cover parts of this refactor.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
@ntBre ntBre deleted the brent/diagnostic-rendering branch September 3, 2025 19:07
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. ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants