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:
Dhruv Manilawala 2023-11-28 18:32:35 -06:00 committed by GitHub
parent 4957d94beb
commit b28556d739
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 204 additions and 9 deletions

View file

@ -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 {