Skip to content

Commit

Permalink
touch-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Aug 14, 2024
1 parent 600dcc7 commit 051d8bd
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ use super::super::helpers::at_last_top_level_expression_in_cell;
/// ## What it does
/// Checks for useless comparisons.
///
/// For Jupyter Notebooks, this rule ignores to check the last top-level expression for each cell.
/// This is because it's common to have a cell that ends with an expression to display it's value.
///
/// ## Why is this bad?
/// Useless comparisons have no effect on the program, and are often included
/// by mistake. If the comparison is intended to enforce an invariant, prepend
Expand All @@ -28,6 +25,11 @@ use super::super::helpers::at_last_top_level_expression_in_cell;
/// assert foo == bar, "`foo` and `bar` should be equal."
/// ```
///
/// ## Notebook behavior
/// For Jupyter Notebooks, this rule is not applied to the last top-level expression in a cell.
/// This is because it's common to have a notebook cell that ends with an expression,
/// which will result in the `repr` of the evaluated expression being printed as the cell's output.
///
/// ## References
/// - [Python documentation: `assert` statement](https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement)
#[violation]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ use super::super::helpers::at_last_top_level_expression_in_cell;
/// ## What it does
/// Checks for useless expressions.
///
/// For Jupyter Notebooks, this rule ignores to check the last top-level expression for each cell.
/// This is because it's common to have a cell that ends with an expression to display it's value.
///
/// ## Why is this bad?
/// Useless expressions have no effect on the program, and are often included
/// by mistake. Assign a useless expression to a variable, or remove it
Expand All @@ -29,6 +26,11 @@ use super::super::helpers::at_last_top_level_expression_in_cell;
/// foo = 1 + 1
/// ```
///
/// ## Notebook behavior
/// For Jupyter Notebooks, this rule is not applied to the last top-level expression in a cell.
/// This is because it's common to have a notebook cell that ends with an expression,
/// which will result in the `repr` of the evaluated expression being printed as the cell's output.
///
/// ## Known problems
/// This rule ignores expression types that are commonly used for their side
/// effects, such as function calls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::checkers::ast::Checker;
/// ## What it does
/// Checks for imports that are not at the top of the file.
///
/// For Jupyter notebooks, this checks for imports that are not at the top of the cell.
///
/// ## Why is this bad?
/// According to [PEP 8], "imports are always put at the top of the file, just after any
/// module comments and docstrings, and before module globals and constants."
Expand Down Expand Up @@ -37,6 +35,9 @@ use crate::checkers::ast::Checker;
/// a = 1
/// ```
///
/// ## Notebook behavior
/// For Jupyter notebooks, this rule checks for imports that are not at the top of a *cell*.
///
/// [PEP 8]: https://peps.python.org/pep-0008/#imports
#[violation]
pub struct ModuleImportNotAtTopOfFile {
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff_linter/src/rules/pydocstyle/rules/not_missing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use ruff_text_size::TextRange;
use crate::checkers::ast::Checker;
use crate::registry::Rule;

/// This rule is ignored for Jupyter Notebooks.
///
/// ## What it does
/// Checks for undocumented public module definitions.
///
Expand Down Expand Up @@ -55,6 +53,9 @@ use crate::registry::Rule;
/// def calculate_speed(distance: float, time: float) -> float: ...
/// ```
///
/// ## Notebook behavior
/// This rule is ignored for Jupyter Notebooks.
///
/// ## References
/// - [PEP 257 – Docstring Conventions](https://peps.python.org/pep-0257/)
/// - [PEP 287 – reStructuredText Docstring Format](https://peps.python.org/pep-0287/)
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ pub struct Options {
/// included here not for configuration but because we lint whether e.g. the
/// `[project]` matches the schema.
///
/// Starting from Ruff version 0.6.0, the default also includes notebook files (`.ipynb`
/// extension).
/// Notebook files (`.ipynb` extension) are included by default on Ruff 0.6.0+.
/// On older versions, they are included by default only if
/// [preview](https://docs.astral.sh/ruff/preview/) mode is enabled.
///
/// For more information on the glob syntax, refer to the [`globset` documentation](https://docs.rs/globset/latest/globset/#syntax).
#[option(
Expand Down
22 changes: 10 additions & 12 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,11 @@ You can also change the default selection using the [`include`](settings.md#incl

## Jupyter Notebook discovery

Ruff has built-in support for linting and formatting [Jupyter Notebooks](https://jupyter.org/).

!!! info
Jupyter Notebook support is marked as stable from Ruff version `0.6.0` onwards and will
be linted and formatted by default.
Ruff has built-in support for linting and formatting [Jupyter Notebooks](https://jupyter.org/),
which are linted and formatted by default on version `0.6.0` and higher.

If you'd prefer to either only lint or only format Jupyter Notebook files, you can use the
section specific `exclude` option to do so. For example, the following would only lint Jupyter
section-specific `exclude` option to do so. For example, the following would only lint Jupyter
Notebook files and not format them:

=== "pyproject.toml"
Expand Down Expand Up @@ -405,7 +402,7 @@ And, conversely, the following would only format Jupyter Notebook files and not
```

You can completely disable Jupyter Notebook support by updating the
[`extend-exclude`](settings.md#extend-exclude) settings:
[`extend-exclude`](settings.md#extend-exclude) setting:

=== "pyproject.toml"

Expand Down Expand Up @@ -437,11 +434,12 @@ using the [`per-file-ignores`](settings.md#per-file-ignores) setting:
"*.ipynb" = ["T20"]
```

There are certain rules that has different behavior when applied to Jupyter Notebook files. For
example, the [`module-import-not-at-top-of-file` (`E402`)](rules/module-import-not-at-top-of-file.md)
rule works at the cell level where the rule is violated only if an import statement is not at the
top of the **cell**. The rule documentation will specify if it has different behavior for Jupyter
Notebook files.
Some rules have different behavior when applied to Jupyter Notebook files. For
example, when applied to `.py` files the
[`module-import-not-at-top-of-file` (`E402`)](rules/module-import-not-at-top-of-file.md)
rule detect imports at the top of a file, but for notebooks it detects imports at the top of a
**cell**. For a given rule, the rule's documentation will always specify if it has different
behavior when applied to Jupyter Notebook files.

## Command-line interface

Expand Down
2 changes: 1 addition & 1 deletion ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 051d8bd

Please sign in to comment.