Skip to content

Fix W391 fixes for consecutive empty notebook cells#24236

Merged
charliermarsh merged 2 commits intomainfrom
charlie/w
Mar 27, 2026
Merged

Fix W391 fixes for consecutive empty notebook cells#24236
charliermarsh merged 2 commits intomainfrom
charlie/w

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

Summary

We now compute trailing newline diagnostics per-cell instead of across the concatenated file, which avoids panics for consecutive empty code cells.

Closes #22797.

Closes #20789.

@charliermarsh charliermarsh added the bug Something isn't working label Mar 27, 2026
@charliermarsh charliermarsh marked this pull request as ready for review March 27, 2026 02:51
@astral-sh-bot astral-sh-bot bot requested a review from ntBre March 27, 2026 02:51
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 27, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -2 violations, +0 -0 fixes in 1 projects; 55 projects unchanged)

bokeh/bokeh (+0 -2 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL

- examples/output/jupyter/push_notebook/Basic Usage.ipynb:cell 14:1:1: W391 [*] Extra newline at end of cell
- examples/server/api/notebook_embed.ipynb:cell 7:1:1: W391 [*] Extra newline at end of cell

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
W391 2 0 2 0 0

@charliermarsh charliermarsh marked this pull request as draft March 27, 2026 03:05
@charliermarsh charliermarsh marked this pull request as ready for review March 27, 2026 13:44
@charliermarsh
Copy link
Copy Markdown
Member Author

I believe those diagnostics were false positives.

self.ranges().map(|range| {
let end = range
.end()
.checked_sub(TextSize::new(1))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it guaranteed that this last character always has a UTF8 length of 1?

.for_each(drop);

for range in cell_offsets.content_ranges() {
let mut tokens_iter = tokens.in_range(range).iter().rev().peekable();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Using in_range performs a binary search. We should be able to do better by doing the slicing ourselves (with a linear scan to find the cell end).

@charliermarsh charliermarsh merged commit c4c6c22 into main Mar 27, 2026
42 checks passed
@charliermarsh charliermarsh deleted the charlie/w branch March 27, 2026 15:25
carljm added a commit that referenced this pull request Mar 31, 2026
* main: (40 commits)
  [ty] resolve union-likes in emitting union attribute errors (#24263)
  [ty] Improve support for `Callable` type context (#23888)
  [ty] Propagate type context through `await` expressions (#24256)
  [`pyflakes`] Flag annotated variable redeclarations as `F811` in preview mode (#24244)
  [ty] Preserve `Divergent` when materializing recursive aliases (#24245)
  Fix W391 fixes for consecutive empty notebook cells (#24236)
  [flake8-bugbear] Clarify RUF071 fix safety for non-path string comparisons (#24149)
  [ty] Ban type qualifiers in PEP-695 type aliases (#24242)
  [ty] Include keyword-prefixed symbols in completions for attributes (#24232)
  [ty] Add tests for TypedDict method overloads on unions (#24230)
  [ty] report unused bindings as unnecessary hint diagnostics (#23305)
  Remove unused `non_root` variable (#24238)
  Extend F507 to flag %-format strings with zero placeholders (#24215)
  [`flake8-simplify`] Suppress `SIM105` for `except*` before Python 3.12 (#23869)
  Ignore pre-initialization references in SIM113 (#24235)
  Parenthesize expression in RUF050 fix (#24234)
  Publish playgrounds using the `release-playground` environment (#24223)
  [ty] Fix instance-attribute lookup in methods of protocol classes (#24213)
  [ty] Used shared expression cache during generic call inference (#24219)
  [ty] make `Type::BoundMethod` include instances of same-named methods bound to a subclass (#24039)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix for W391 does not Work for Notebooks Fatal crash in ipynb file

3 participants