Compare commits

...

9 Commits

Author SHA1 Message Date
Charlie Marsh
bb6f207e0b Run: brew install --build-from-source --verbose --debug ruff
File: /opt/homebrew/Library/Taps/homebrew/homebrew-core/Formula/ruff.rb

Docs: https://docs.brew.sh/Formula-Cookbook

Test: brew test ruff
2022-11-23 22:36:32 -05:00
Charlie Marsh
68668a584b Bump version to 0.0.137 2022-11-23 20:28:45 -05:00
Charlie Marsh
6cd8655d29 Treat withitem variables as bindings (#897) 2022-11-23 20:28:37 -05:00
Charlie Marsh
72a9bd3cfb Revert "Upload wheels back to GitHub Releases (#884)"
This reverts commit bd08fc359d.
2022-11-23 20:27:33 -05:00
Charlie Marsh
58aac21a36 Bump version to 0.0.136 2022-11-23 17:41:17 -05:00
Charlie Marsh
77e0be3464 Visit iter prior to target in comprehensions (#895) 2022-11-23 10:13:21 -05:00
Charlie Marsh
bd08fc359d Upload wheels back to GitHub Releases (#884) 2022-11-23 00:06:36 -05:00
Harutaka Kawamura
19ad6ab4f5 Add --explain (#887) 2022-11-23 00:06:25 -05:00
Charlie Marsh
4b2df99e78 Set rust-version in Cargo.toml (#886) 2022-11-22 23:33:39 -05:00
13 changed files with 105 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.135
rev: v0.0.137
hooks:
- id: ruff

6
Cargo.lock generated
View File

@@ -670,7 +670,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8-to-ruff"
version = "0.0.135-dev.0"
version = "0.0.137-dev.0"
dependencies = [
"anyhow",
"clap 4.0.22",
@@ -1768,7 +1768,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.135"
version = "0.0.137"
dependencies = [
"annotate-snippets 0.9.1",
"anyhow",
@@ -1818,7 +1818,7 @@ dependencies = [
[[package]]
name = "ruff_dev"
version = "0.0.135"
version = "0.0.137"
dependencies = [
"anyhow",
"clap 4.0.22",

View File

@@ -6,8 +6,9 @@ members = [
[package]
name = "ruff"
version = "0.0.135"
version = "0.0.137"
edition = "2021"
rust-version = "1.65.0"
[lib]
name = "ruff"

View File

@@ -107,7 +107,7 @@ Ruff also works with [pre-commit](https://pre-commit.com):
```yaml
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.135
rev: v0.0.137
hooks:
- id: ruff
```

View File

@@ -771,7 +771,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8_to_ruff"
version = "0.0.135"
version = "0.0.137"
dependencies = [
"anyhow",
"clap",
@@ -1975,7 +1975,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.135"
version = "0.0.137"
dependencies = [
"anyhow",
"bincode",

View File

@@ -1,6 +1,6 @@
[package]
name = "flake8-to-ruff"
version = "0.0.135-dev.0"
version = "0.0.137-dev.0"
edition = "2021"
[lib]

View File

@@ -52,3 +52,16 @@ def f5():
def f7():
nonlocal b
def f6():
annotations = []
assert len([annotations for annotations in annotations])
def f7():
def connect():
return None, None
with connect() as (connection, cursor):
cursor.execute("SELECT * FROM users")

27
ruff.rb Normal file
View File

@@ -0,0 +1,27 @@
class Ruff < Formula
desc "An extremely fast Python linter, written in Rust."
homepage "https://github.com/charliermarsh/ruff"
url "https://github.com/charliermarsh/ruff/archive/refs/tags/v0.0.137.tar.gz"
sha256 "d5521f2ad9ee87ca8018bd23ea731fc05abd0fa4a01878880a8cc5119596f837"
license "MIT"
head "https://github.com/charliermarsh/ruff.git", branch: "main"
# STOPSHIP(charlie): Requires Rust 1.65.0.
# See: https://github.com/Homebrew/homebrew-core/pull/116480
depends_on "rust" => :build
def install
system "cargo", "install", "--no-default-features", *std_cargo_args
bin.install "target/release/ruff" => "ruff"
end
test do
(testpath/"test.py").write <<~EOS
import os
EOS
expected = <<~EOS
test.py:1:1: F401 `os` imported but unused
EOS
assert_equal expected, shell_output("#{bin}/ruff -- --quiet #{testpath}/test.py")
end
end

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff_dev"
version = "0.0.135"
version = "0.0.137"
edition = "2021"
[dependencies]

View File

@@ -456,8 +456,8 @@ pub fn walk_comprehension<'a, V: Visitor<'a> + ?Sized>(
visitor: &mut V,
comprehension: &'a Comprehension,
) {
visitor.visit_expr(&comprehension.target);
visitor.visit_expr(&comprehension.iter);
visitor.visit_expr(&comprehension.target);
for expr in &comprehension.ifs {
visitor.visit_expr(expr);
}

View File

@@ -6,6 +6,7 @@ use std::path::Path;
use itertools::Itertools;
use log::error;
use rustc_hash::{FxHashMap, FxHashSet};
use rustpython_ast::Withitem;
use rustpython_parser::ast::{
Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind,
KeywordData, Operator, Stmt, StmtKind, Suite,
@@ -21,7 +22,7 @@ use crate::ast::types::{
Binding, BindingContext, BindingKind, ClassScope, FunctionScope, ImportKind, Range, Scope,
ScopeKind,
};
use crate::ast::visitor::{walk_excepthandler, Visitor};
use crate::ast::visitor::{walk_excepthandler, walk_withitem, Visitor};
use crate::ast::{helpers, operations, visitor};
use crate::checks::{Check, CheckCode, CheckKind};
use crate::docstrings::definition::{Definition, DefinitionKind, Documentable};
@@ -77,6 +78,7 @@ pub struct Checker<'a> {
in_deferred_string_annotation: bool,
in_literal: bool,
in_subscript: bool,
in_withitem: bool,
seen_import_boundary: bool,
futures_allowed: bool,
annotations_future_enabled: bool,
@@ -120,6 +122,7 @@ impl<'a> Checker<'a> {
in_deferred_string_annotation: false,
in_literal: false,
in_subscript: false,
in_withitem: false,
seen_import_boundary: false,
futures_allowed: true,
annotations_future_enabled: false,
@@ -2080,6 +2083,13 @@ where
self.check_builtin_arg_shadowing(&arg.node.arg, Range::from_located(arg));
}
fn visit_withitem(&mut self, withitem: &'b Withitem) {
let prev_in_withitem = self.in_withitem;
self.in_withitem = true;
walk_withitem(self, withitem);
self.in_withitem = prev_in_withitem;
}
}
fn try_mark_used(scope: &mut Scope, scope_id: usize, id: &str, expr: &Expr) -> bool {
@@ -2386,7 +2396,7 @@ impl<'a> Checker<'a> {
return;
}
if operations::is_unpacking_assignment(parent) {
if self.in_withitem || operations::is_unpacking_assignment(parent) {
self.add_binding(
id,
Binding {

View File

@@ -5,6 +5,7 @@ use clap::{command, Parser};
use regex::Regex;
use rustc_hash::FxHashMap;
use crate::checks::CheckCode;
use crate::checks_gen::CheckCodePrefix;
use crate::logging::LogLevel;
use crate::printer::SerializationFormat;
@@ -111,6 +112,9 @@ pub struct Cli {
/// The name of the file when passing it through stdin.
#[arg(long)]
pub stdin_filename: Option<String>,
/// Explain a rule.
#[arg(long)]
pub explain: Option<CheckCode>,
}
impl Cli {

View File

@@ -40,6 +40,7 @@ use notify::{raw_watcher, RecursiveMode, Watcher};
use rayon::prelude::*;
use ruff::linter::Diagnostics;
use rustpython_ast::Location;
use serde::Serialize;
use walkdir::DirEntry;
/// Shim that calls `par_iter` except for wasm because there's no wasm support
@@ -67,6 +68,37 @@ fn show_settings(
);
}
#[derive(Serialize)]
struct Explanation<'a> {
code: &'a str,
category: &'a str,
summary: &'a str,
}
fn explain(code: &CheckCode, format: SerializationFormat) -> Result<()> {
match format {
SerializationFormat::Text => {
println!(
"{} ({}): {}",
code.as_ref(),
code.category().title(),
code.kind().summary()
);
}
SerializationFormat::Json => {
println!(
"{}",
serde_json::to_string_pretty(&Explanation {
code: code.as_ref(),
category: code.category().title(),
summary: &code.kind().summary(),
})?
);
}
};
Ok(())
}
fn show_files(files: &[PathBuf], settings: &Settings) {
let mut entries: Vec<DirEntry> = files
.iter()
@@ -294,6 +326,11 @@ fn inner_main() -> Result<ExitCode> {
configuration.show_source = true;
}
if let Some(code) = cli.explain {
explain(&code, cli.format)?;
return Ok(ExitCode::SUCCESS);
}
if cli.show_settings && cli.show_files {
eprintln!("Error: specify --show-settings or show-files (not both).");
return Ok(ExitCode::FAILURE);