Skip to content

Use new diagnostic functionality #17203

@ntBre

Description

@ntBre

Background

Our new diagnostic system has the ability to attach additional information to the main diagnostic. The two main types of additional information are:

  • Secondary annotations
    These are rendered as additional underlined ranges in the snippet for the main diagnostic:

     PLC0206 Extracting value from dictionary without calling `.items()`
       --> dict_index_missing_items.py:59:5
        |
     58 |
     59 | for instrument in ORCHESTRA:
        |     ^^^^^^^^^^^^^^^^^^^^^^^  <- primary diagnostic range
     60 |     data = json.dumps(
     61 |         {
     62 |             "instrument": instrument,
     63 |             "section": ORCHESTRA[instrument],
        |                        ---------------------  <- secondary annotation
     64 |         }
     65 |     )
     help: Use `for instrument, value in ORCHESTRA.items()` instead
    

    and can be added in most lint rules via the DiagnosticGuard::secondary_annotation helper.

    The example above is from [pylint] Improve diagnostic range for PLC0206 #22312.

  • Sub-diagnostics
    The help: ... line above is actually a simple example of a sub-diagnostic without its own code snippet. For simple messages, you can use the Diagnostic::info or Diagnostic::help methods via the DiagnosticGuard deref implementation.

    Sub-diagnostics can, however, include almost all of the kinds of information as a primary diagnostic. We don't have any examples of this in Ruff yet, but you can find some in ty.

    I think we'll mostly be okay with secondary annotations or simple sub-diagnostics in Ruff, but it's possible to produce these if needed.

Places to use new diagnostics

This is a list of places we've thought about using the new diagnostics. Note that most of these are follow-up comments on merged PRs, so the fact that they're merged doesn't mean that the diagnostics have been updated.

Examples

Here are a couple of example PRs showing how to use secondary annotations:

One thing to watch out for is that changing the primary diagnostic range can easily be a breaking change, so for the most part we only want to add new annotations here, not change the existing ones.

Metadata

Metadata

Assignees

Labels

diagnosticsRelated to reporting of diagnostics.trackingA "meta" issue that tracks completion of a bigger task via a list of smaller scoped issues.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions