Skip to content

Commit

Permalink
Merge branch 'main' into line-suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Aug 25, 2023
2 parents 9385e63 + 15b7525 commit 983257e
Show file tree
Hide file tree
Showing 144 changed files with 18,228 additions and 21,143 deletions.
25 changes: 23 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ tracing = "0.1.37"
tracing-indicatif = "0.3.4"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
unicode-width = "0.1.10"
uuid = { version = "1.4.1", features = ["v4", "fast-rng", "macro-diagnostics", "js"] }
wsl = { version = "0.1.0" }

# v1.0.1
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ toml = { workspace = true }
typed-arena = { version = "2.0.2" }
unicode-width = { workspace = true }
unicode_names2 = { version = "0.6.0", git = "https://github.com/youknowone/unicode_names2.git", rev = "4ce16aa85cbcdd9cc830410f1a72ef9a235f2fde" }
uuid = { workspace = true, features = ["v4", "fast-rng", "macro-diagnostics", "js"] }
wsl = { version = "0.1.0" }

[dev-dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ def timedelta_okay(value=dt.timedelta(hours=1)):
def path_okay(value=Path(".")):
pass

# B008 allow arbitrary call with immutable annotation
def immutable_annotation_call(value: Sequence[int] = foo()):
pass

# B006 and B008
# We should handle arbitrary nesting of these B008.
def nested_combo(a=[float(3), dt.datetime.now()]):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List

import fastapi
import custom
from fastapi import Query


Expand All @@ -16,5 +17,9 @@ def okay(data: List[str] = Query(None)):
...


def okay(data: custom.ImmutableTypeA = foo()):
...


def error_due_to_missing_import(data: List[str] = Depends(None)):
...
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ def test_error():
assert something # OK
assert something and something_else # Error
assert something and something_else and something_third # Error


def test_multiline():
assert something and something_else; x = 1

x = 1; assert something and something_else

x = 1; \
assert something and something_else
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os
import pandas
import foo.baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.ruff]
line-length = 88
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import os\n",
"\n",
"math.pi"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (ruff)",
"language": "python",
"name": "ruff"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
37 changes: 37 additions & 0 deletions crates/ruff/resources/test/fixtures/jupyter/no_cell_id.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import os\n",
"\n",
"math.pi"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (ruff)",
"language": "python",
"name": "ruff"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
# coding=utf8""" # empty comment

"""
Invalid coding declaration since it is nested inside a docstring
The following empty comment tests for false positives as our implementation visits comments
"""
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# coding=utf8
print("Hello world")

"""
Regression test for https://github.com/astral-sh/ruff/issues/6756
The leading space must be removed to prevent invalid syntax.
"""
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# coding=utf8
print("Hello world")

"""
Regression test for https://github.com/astral-sh/ruff/issues/6756
The leading tab must be removed to prevent invalid syntax.
"""
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
print("foo") # coding=utf8
print("Hello world")

"""
Invalid coding declaration due to a statement before the comment
"""
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP009_9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x = 1 \
# coding=utf8
x = 2

"""
Invalid coding declaration due to continuation on preceding line
"""
26 changes: 13 additions & 13 deletions crates/ruff/src/autofix/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,25 +226,25 @@ fn trailing_semicolon(offset: TextSize, locator: &Locator) -> Option<TextSize> {
fn next_stmt_break(semicolon: TextSize, locator: &Locator) -> TextSize {
let start_location = semicolon + TextSize::from(1);

let contents = &locator.contents()[usize::from(start_location)..];
for line in NewlineWithTrailingNewline::from(contents) {
for line in
NewlineWithTrailingNewline::with_offset(locator.after(start_location), start_location)
{
let trimmed = line.trim_whitespace();
// Skip past any continuations.
if trimmed.starts_with('\\') {
continue;
}

return start_location
+ if trimmed.is_empty() {
// If the line is empty, then despite the previous statement ending in a
// semicolon, we know that it's not a multi-statement line.
line.start()
} else {
// Otherwise, find the start of the next statement. (Or, anything that isn't
// whitespace.)
let relative_offset = line.find(|c: char| !is_python_whitespace(c)).unwrap();
line.start() + TextSize::try_from(relative_offset).unwrap()
};
return if trimmed.is_empty() {
// If the line is empty, then despite the previous statement ending in a
// semicolon, we know that it's not a multi-statement line.
line.start()
} else {
// Otherwise, find the start of the next statement. (Or, anything that isn't
// whitespace.)
let relative_offset = line.find(|c: char| !is_python_whitespace(c)).unwrap();
line.start() + TextSize::try_from(relative_offset).unwrap()
};
}

locator.line_end(start_location)
Expand Down
14 changes: 7 additions & 7 deletions crates/ruff/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ pub(crate) fn check_noqa(
let mut diagnostic =
Diagnostic::new(UnusedNOQA { codes: None }, directive.range());
if settings.rules.should_fix(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix_from_edit(delete_noqa(directive.range(), locator));
diagnostic
.set_fix(Fix::automatic(delete_noqa(directive.range(), locator)));
}
diagnostics.push(diagnostic);
}
Expand Down Expand Up @@ -175,12 +175,12 @@ pub(crate) fn check_noqa(
);
if settings.rules.should_fix(diagnostic.kind.rule()) {
if valid_codes.is_empty() {
#[allow(deprecated)]
diagnostic
.set_fix_from_edit(delete_noqa(directive.range(), locator));
diagnostic.set_fix(Fix::automatic(delete_noqa(
directive.range(),
locator,
)));
} else {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
format!("# noqa: {}", valid_codes.join(", ")),
directive.range(),
)));
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/jupyter/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/// When we lint a jupyter notebook, we have to translate the row/column based on
/// [`ruff_text_size::TextSize`] to jupyter notebook cell/row/column.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct JupyterIndex {
pub struct NotebookIndex {
/// Enter a row (1-based), get back the cell (1-based)
pub(super) row_to_cell: Vec<u32>,
/// Enter a row (1-based), get back the row in cell (1-based)
pub(super) row_to_row_in_cell: Vec<u32>,
}

impl JupyterIndex {
impl NotebookIndex {
/// Returns the cell number (1-based) for the given row (1-based).
pub fn cell(&self, row: usize) -> Option<u32> {
self.row_to_cell.get(row).copied()
Expand Down
Loading

0 comments on commit 983257e

Please sign in to comment.