Skip to content
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

Make SourceKind a required parameter #7013

Merged
merged 2 commits into from
Sep 4, 2023
Merged

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Aug 31, 2023

Summary

This PR refactors the check_path function to make SourceKind a required parameter i.e., remove the Option<...>. The main blocker for this change was that add_noqa wasn't supported for Jupyter Notebooks but it's still fine to pass in the SourceKind without it. This is ok because the PySourceType is checked before and we don't proceed unless it's either a Python or stub file.

This is also required for the PEP 701 f-string parser changes because we want the source code to be available while parsing to extract the leading and trailing text for debug expressions. Here, SourceKind will provide that but it was an Option<...> before.

Comment on lines +174 to +175
let error_location =
if let Some(jupyter_index) = self.source_kind.as_ipy_notebook().map(Notebook::index) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the main change in this section of the diff otherwise it's just a formatter change.

Comment on lines -13 to -21
/// Return the [`Notebook`] if the source kind is [`SourceKind::IpyNotebook`].
pub fn notebook(&self) -> Option<&Notebook> {
if let Self::IpyNotebook(notebook) = self {
Some(notebook)
} else {
None
}
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Not required because of is_macro::Is usage on the enum.

crates/ruff_cli/src/commands/add_noqa.rs Outdated Show resolved Hide resolved
@github-actions
Copy link
Contributor

github-actions bot commented Aug 31, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

crates/ruff/src/linter.rs Outdated Show resolved Hide resolved
crates/ruff_cli/src/commands/add_noqa.rs Outdated Show resolved Hide resolved
error!("Failed to extract source from {}", path.display());
return None;
};
match add_noqa_to_path(path, package, &source_kind, source_type, settings) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe unify the error handling either by using LintSource::try_from_path(...).and_then(|source| add_noqa_to_path(...)) or extracing into its own function and calling it.

@MichaReiser MichaReiser added the internal An internal refactor or improvement label Aug 31, 2023
@dhruvmanila dhruvmanila changed the base branch from main to charlie/box-error September 1, 2023 05:04
@dhruvmanila dhruvmanila force-pushed the dhruv/source-kind-required branch 2 times, most recently from 7017574 to b369b12 Compare September 1, 2023 05:06
Base automatically changed from charlie/box-error to main September 1, 2023 11:08
charliermarsh added a commit that referenced this pull request Sep 1, 2023
…7035)

## Summary

This PR refactors the error-handling cases around Jupyter notebooks to
use errors rather than `Box<Diagnostics>`, which creates some oddities
in the downstream handling. So, instead of formatting errors as
diagnostics _eagerly_ (in the notebook methods), we now return errors
and convert those errors to diagnostics at the last possible moment (in
`diagnostics.rs`). This is more ergonomic, as errors can be composed and
reported-on in different ways, whereas diagnostics require a `Printer`,
etc.

See, e.g.,
#7013 (comment).

## Test Plan

Ran `cargo run` over a Python file labeled with a `.ipynb` suffix, and
saw:

```
foo.ipynb:1:1: E999 SyntaxError: Expected a Jupyter Notebook, which must be internally stored as JSON, but found a Python source file: expected value at line 1 column 1
```
@dhruvmanila dhruvmanila closed this Sep 4, 2023
@dhruvmanila dhruvmanila reopened this Sep 4, 2023
@dhruvmanila
Copy link
Member Author

(Closing and re-opening the PR to trigger CI)

@dhruvmanila dhruvmanila enabled auto-merge (squash) September 4, 2023 07:36
@dhruvmanila dhruvmanila merged commit 1067261 into main Sep 4, 2023
33 of 54 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/source-kind-required branch September 4, 2023 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants