Compare commits

..

2 Commits

Author SHA1 Message Date
Charlie Marsh
f84c1f1fa1 Bump version to 0.0.91 2022-10-29 18:49:26 -04:00
Charlie Marsh
3aa9528229 Avoid flake8-comprehensions errors for dicts with kwargs (#512) 2022-10-29 18:49:06 -04:00
10 changed files with 75 additions and 41 deletions

View File

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

2
Cargo.lock generated
View File

@@ -2054,7 +2054,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.90"
version = "0.0.91"
dependencies = [
"anyhow",
"assert_cmd",

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff"
version = "0.0.90"
version = "0.0.91"
edition = "2021"
[lib]

View File

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

View File

@@ -1 +1,2 @@
d = dict((x, x) for x in range(3))
dict((x, x) for x in range(3))
dict(((x, x) for x in range(3)), z=3)

View File

@@ -1 +1,2 @@
d = dict([(i, i) for i in range(3)])
dict([(i, i) for i in range(3)])
dict([(i, i) for i in range(3)], z=4)

View File

@@ -923,7 +923,6 @@ where
func,
args,
keywords,
..
} => {
if self.settings.enabled.contains(&CheckCode::U005) {
pyupgrade::plugins::deprecated_unittest_alias(self, func);
@@ -943,25 +942,25 @@ where
// flake8-comprehensions
if self.settings.enabled.contains(&CheckCode::C400) {
if let Some(check) =
flake8_comprehensions::checks::unnecessary_generator_list(expr, func, args)
{
if let Some(check) = flake8_comprehensions::checks::unnecessary_generator_list(
expr, func, args, keywords,
) {
self.checks.push(check);
};
}
if self.settings.enabled.contains(&CheckCode::C401) {
if let Some(check) =
flake8_comprehensions::checks::unnecessary_generator_set(expr, func, args)
{
if let Some(check) = flake8_comprehensions::checks::unnecessary_generator_set(
expr, func, args, keywords,
) {
self.checks.push(check);
};
}
if self.settings.enabled.contains(&CheckCode::C402) {
if let Some(check) =
flake8_comprehensions::checks::unnecessary_generator_dict(expr, func, args)
{
if let Some(check) = flake8_comprehensions::checks::unnecessary_generator_dict(
expr, func, args, keywords,
) {
self.checks.push(check);
};
}
@@ -969,7 +968,7 @@ where
if self.settings.enabled.contains(&CheckCode::C403) {
if let Some(check) =
flake8_comprehensions::checks::unnecessary_list_comprehension_set(
expr, func, args,
expr, func, args, keywords,
)
{
self.checks.push(check);
@@ -979,7 +978,7 @@ where
if self.settings.enabled.contains(&CheckCode::C404) {
if let Some(check) =
flake8_comprehensions::checks::unnecessary_list_comprehension_dict(
expr, func, args,
expr, func, args, keywords,
)
{
self.checks.push(check);
@@ -987,17 +986,17 @@ where
}
if self.settings.enabled.contains(&CheckCode::C405) {
if let Some(check) =
flake8_comprehensions::checks::unnecessary_literal_set(expr, func, args)
{
if let Some(check) = flake8_comprehensions::checks::unnecessary_literal_set(
expr, func, args, keywords,
) {
self.checks.push(check);
};
}
if self.settings.enabled.contains(&CheckCode::C406) {
if let Some(check) =
flake8_comprehensions::checks::unnecessary_literal_dict(expr, func, args)
{
if let Some(check) = flake8_comprehensions::checks::unnecessary_literal_dict(
expr, func, args, keywords,
) {
self.checks.push(check);
};
}

View File

@@ -1,5 +1,7 @@
use num_bigint::BigInt;
use rustpython_ast::{Comprehension, Constant, Expr, ExprKind, KeywordData, Located, Unaryop};
use rustpython_ast::{
Comprehension, Constant, Expr, ExprKind, Keyword, KeywordData, Located, Unaryop,
};
use crate::ast::types::Range;
use crate::checks::{Check, CheckKind};
@@ -16,7 +18,11 @@ fn exactly_one_argument_with_matching_function<'a>(
name: &str,
func: &Expr,
args: &'a [Expr],
keywords: &[Keyword],
) -> Option<&'a ExprKind> {
if !keywords.is_empty() {
return None;
}
if args.len() != 1 {
return None;
}
@@ -38,8 +44,13 @@ fn first_argument_with_matching_function<'a>(
}
/// C400 (`list(generator)`)
pub fn unnecessary_generator_list(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("list", func, args)?;
pub fn unnecessary_generator_list(
expr: &Expr,
func: &Expr,
args: &[Expr],
keywords: &[Keyword],
) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("list", func, args, keywords)?;
if let ExprKind::GeneratorExp { .. } = argument {
return Some(Check::new(
CheckKind::UnnecessaryGeneratorList,
@@ -50,8 +61,13 @@ pub fn unnecessary_generator_list(expr: &Expr, func: &Expr, args: &[Expr]) -> Op
}
/// C401 (`set(generator)`)
pub fn unnecessary_generator_set(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("set", func, args)?;
pub fn unnecessary_generator_set(
expr: &Expr,
func: &Expr,
args: &[Expr],
keywords: &[Keyword],
) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("set", func, args, keywords)?;
if let ExprKind::GeneratorExp { .. } = argument {
return Some(Check::new(
CheckKind::UnnecessaryGeneratorSet,
@@ -62,8 +78,13 @@ pub fn unnecessary_generator_set(expr: &Expr, func: &Expr, args: &[Expr]) -> Opt
}
/// C402 (`dict((x, y) for x, y in iterable)`)
pub fn unnecessary_generator_dict(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("dict", func, args)?;
pub fn unnecessary_generator_dict(
expr: &Expr,
func: &Expr,
args: &[Expr],
keywords: &[Keyword],
) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("dict", func, args, keywords)?;
if let ExprKind::GeneratorExp { elt, .. } = argument {
match &elt.node {
ExprKind::Tuple { elts, .. } if elts.len() == 2 => {
@@ -83,8 +104,9 @@ pub fn unnecessary_list_comprehension_set(
expr: &Expr,
func: &Expr,
args: &[Expr],
keywords: &[Keyword],
) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("set", func, args)?;
let argument = exactly_one_argument_with_matching_function("set", func, args, keywords)?;
if let ExprKind::ListComp { .. } = &argument {
return Some(Check::new(
CheckKind::UnnecessaryListComprehensionSet,
@@ -99,8 +121,9 @@ pub fn unnecessary_list_comprehension_dict(
expr: &Expr,
func: &Expr,
args: &[Expr],
keywords: &[Keyword],
) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("dict", func, args)?;
let argument = exactly_one_argument_with_matching_function("dict", func, args, keywords)?;
if let ExprKind::ListComp { elt, .. } = &argument {
match &elt.node {
ExprKind::Tuple { elts, .. } if elts.len() == 2 => {
@@ -116,8 +139,13 @@ pub fn unnecessary_list_comprehension_dict(
}
/// C405 (`set([1, 2])`)
pub fn unnecessary_literal_set(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("set", func, args)?;
pub fn unnecessary_literal_set(
expr: &Expr,
func: &Expr,
args: &[Expr],
keywords: &[Keyword],
) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("set", func, args, keywords)?;
let kind = match argument {
ExprKind::List { .. } => "list",
ExprKind::Tuple { .. } => "tuple",
@@ -130,8 +158,13 @@ pub fn unnecessary_literal_set(expr: &Expr, func: &Expr, args: &[Expr]) -> Optio
}
/// C406 (`dict([(1, 2)])`)
pub fn unnecessary_literal_dict(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("dict", func, args)?;
pub fn unnecessary_literal_dict(
expr: &Expr,
func: &Expr,
args: &[Expr],
keywords: &[Keyword],
) -> Option<Check> {
let argument = exactly_one_argument_with_matching_function("dict", func, args, keywords)?;
let (kind, elts) = match argument {
ExprKind::Tuple { elts, .. } => ("tuple", elts),
ExprKind::List { elts, .. } => ("list", elts),

View File

@@ -5,9 +5,9 @@ expression: checks
- kind: UnnecessaryGeneratorDict
location:
row: 1
column: 5
column: 1
end_location:
row: 1
column: 35
column: 31
fix: ~

View File

@@ -5,9 +5,9 @@ expression: checks
- kind: UnnecessaryListComprehensionDict
location:
row: 1
column: 5
column: 1
end_location:
row: 1
column: 37
column: 33
fix: ~