mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:39:12 +00:00
Update E402
to work at cell level for notebooks (#8872)
## Summary This PR updates the `E402` rule to work at cell level for Jupyter notebooks. This is enabled only in preview to gather feedback. The implementation basically resets the import boundary flag on the semantic model when we encounter the first statement in a cell. Another potential solution is to introduce `E403` rule that is specifically for notebooks that works at cell level while `E402` will be disabled for notebooks. ## Test Plan Add a notebook with imports in multiple cells and verify that the rule works as expected. resolves: #8669
This commit is contained in:
parent
4957d94beb
commit
b28556d739
7 changed files with 204 additions and 9 deletions
|
@ -175,7 +175,7 @@ impl Cell {
|
|||
}
|
||||
|
||||
/// Cell offsets are used to keep track of the start and end offsets of each
|
||||
/// cell in the concatenated source code.
|
||||
/// cell in the concatenated source code. These offsets are in sorted order.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct CellOffsets(Vec<TextSize>);
|
||||
|
||||
|
@ -186,7 +186,17 @@ impl CellOffsets {
|
|||
}
|
||||
|
||||
/// Push a new offset to the end of the [`CellOffsets`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the offset is less than the last offset pushed.
|
||||
pub(crate) fn push(&mut self, offset: TextSize) {
|
||||
if let Some(last_offset) = self.0.last() {
|
||||
assert!(
|
||||
*last_offset <= offset,
|
||||
"Offsets must be pushed in sorted order"
|
||||
);
|
||||
}
|
||||
self.0.push(offset);
|
||||
}
|
||||
|
||||
|
@ -200,6 +210,22 @@ impl CellOffsets {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns `true` if the given range contains a cell boundary.
|
||||
pub fn has_cell_boundary(&self, range: TextRange) -> bool {
|
||||
self.binary_search_by(|offset| {
|
||||
if range.start() <= *offset {
|
||||
if range.end() < *offset {
|
||||
std::cmp::Ordering::Greater
|
||||
} else {
|
||||
std::cmp::Ordering::Equal
|
||||
}
|
||||
} else {
|
||||
std::cmp::Ordering::Less
|
||||
}
|
||||
})
|
||||
.is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for CellOffsets {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue