-
Notifications
You must be signed in to change notification settings - Fork 2k
[pyflakes] Add secondary annotation showing previous definition (F811)
#19900
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
Changes from 4 commits
5d52b1d
ae24f3f
5f3aebe
ab685c1
683ae20
22c284a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,9 @@ use itertools::Itertools; | |
| use log::debug; | ||
| use rustc_hash::{FxHashMap, FxHashSet}; | ||
|
|
||
| use ruff_db::diagnostic::Diagnostic; | ||
| use ruff_db::diagnostic::{ | ||
| Annotation, Diagnostic, IntoDiagnosticMessage, Span, SubDiagnostic, SubDiagnosticSeverity, | ||
| }; | ||
| use ruff_diagnostics::{Applicability, Fix, IsolationLevel}; | ||
| use ruff_notebook::{CellOffsets, NotebookIndex}; | ||
| use ruff_python_ast::helpers::{collect_import_from_member, is_docstring_stmt, to_module_path}; | ||
|
|
@@ -3305,6 +3307,22 @@ impl DiagnosticGuard<'_, '_> { | |
| Err(err) => log::debug!("Failed to create fix for {}: {}", self.name(), err), | ||
| } | ||
| } | ||
|
|
||
| /// Add an "info" sub-diagnostic with the given message and range. | ||
| /// | ||
| /// Note that this shadows `Diagnostic::info` from the `Deref` implementation because we'll | ||
| /// usually want to attach a range here. | ||
| pub(crate) fn info<'a>( | ||
| &mut self, | ||
| message: impl IntoDiagnosticMessage + 'a, | ||
| range: impl Ranged, | ||
| ) { | ||
| let mut sub = SubDiagnostic::new(SubDiagnosticSeverity::Info, message); | ||
| let span = Span::from(self.context.source_file.clone()).with_range(range.range()); | ||
| let ann = Annotation::primary(span); | ||
| sub.annotate(ann); | ||
| self.diagnostic.as_mut().unwrap().sub(sub); | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Deref for DiagnosticGuard<'_, '_> { | ||
|
|
@@ -3331,7 +3349,8 @@ impl Drop for DiagnosticGuard<'_, '_> { | |
| return; | ||
| } | ||
|
|
||
| if let Some(diagnostic) = self.diagnostic.take() { | ||
| if let Some(mut diagnostic) = self.diagnostic.take() { | ||
| diagnostic.sort_sub_diagnostics(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this. I think it's important that a lint rule author is in full control about how a diagnostic is rendered. If they decide that a sub diagnostic with lower severity should come first, than so be it. I'm sure there's a reason for it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know what you mean, I was just trying to work around the automatic
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A more strimlined version of |
||
| self.context.diagnostics.borrow_mut().push(diagnostic); | ||
| } | ||
| } | ||
|
|
||
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.
Out of curiosity, what is the motivation for this change?
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 to match the path in
from_diagnosticabove. This isn't used in this PR now that we're using a secondary annotation, so I can revert it!I'll double-check if it's actually needed for full diagnostics too.
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.
Using
pathinstead ofrelative_pathchanges some of our CLI snapshots for the main diagnostic, so I think we'll need this for sub-diagnostics too, once we use any sub-diagnostics with paths in Ruff.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'll go ahead and leave this instead of reverting, since it seems like it will be useful in the future)