From 8bfcd46d0f81b7d6441dcff1cb9e6ee8103e5450 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 17 Jan 2023 15:06:13 -0500 Subject: [PATCH] Move @functools.cache rewrites to their own rule --- BREAKING_CHANGES.md | 8 + README.md | 3 +- resources/test/fixtures/pyupgrade/UP011.py | 67 +++++++ resources/test/fixtures/pyupgrade/UP011_0.py | 103 ----------- resources/test/fixtures/pyupgrade/UP011_1.py | 15 -- resources/test/fixtures/pyupgrade/UP033.py | 67 +++++++ ruff.schema.json | 1 + src/checkers/ast.rs | 7 +- src/registry.rs | 3 +- src/rules/pyupgrade/mod.rs | 4 +- src/rules/pyupgrade/rules/functools_cache.rs | 63 +++++++ .../rules/lru_cache_without_parameters.rs | 43 +++++ src/rules/pyupgrade/rules/mod.rs | 6 +- .../rules/unnecessary_lru_cache_params.rs | 88 --------- ...les__pyupgrade__tests__UP011_UP011.py.snap | 73 ++++++++ ...s__pyupgrade__tests__UP011_UP011_0.py.snap | 168 ------------------ ...s__pyupgrade__tests__UP011_UP011_1.py.snap | 6 - ...les__pyupgrade__tests__UP033_UP033.py.snap | 66 +++++++ src/violations.rs | 23 ++- 19 files changed, 424 insertions(+), 390 deletions(-) create mode 100644 resources/test/fixtures/pyupgrade/UP011.py delete mode 100644 resources/test/fixtures/pyupgrade/UP011_0.py delete mode 100644 resources/test/fixtures/pyupgrade/UP011_1.py create mode 100644 resources/test/fixtures/pyupgrade/UP033.py create mode 100644 src/rules/pyupgrade/rules/functools_cache.rs create mode 100644 src/rules/pyupgrade/rules/lru_cache_without_parameters.rs delete mode 100644 src/rules/pyupgrade/rules/unnecessary_lru_cache_params.rs create mode 100644 src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011.py.snap delete mode 100644 src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011_0.py.snap delete mode 100644 src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011_1.py.snap create mode 100644 src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP033_UP033.py.snap diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md index 3569d0cf377faf..a902c2d61ff3c8 100644 --- a/BREAKING_CHANGES.md +++ b/BREAKING_CHANGES.md @@ -1,5 +1,13 @@ # Breaking Changes +## 0.0.224 + +### `@functools.cache` rewrites have been moved to a standalone rule (`UP033`) (#[]()) + +Previously, `UP011` handled both `@functools.lru_cache()`-to-`@functools.lru_cache` conversions, +_and_ `@functools.lru_cache(maxsize=None)`-to-`@functools.cache` conversions. The latter has been +moved out to its own rule (`UP033`). + ## 0.0.222 ### `--max-complexity` has been removed from the CLI ([#1877](https://github.com/charliermarsh/ruff/pull/1877)) diff --git a/README.md b/README.md index 0619c4e062d912..cc0d66952eb5ba 100644 --- a/README.md +++ b/README.md @@ -705,7 +705,7 @@ For more, see [pyupgrade](https://pypi.org/project/pyupgrade/3.2.0/) on PyPI. | UP008 | SuperCallWithParameters | Use `super()` instead of `super(__class__, self)` | 🛠 | | UP009 | PEP3120UnnecessaryCodingComment | UTF-8 encoding declaration is unnecessary | 🛠 | | UP010 | UnnecessaryFutureImport | Unnecessary `__future__` import `...` for target Python version | 🛠 | -| UP011 | UnnecessaryLRUCacheParams | Unnecessary parameters to `functools.lru_cache` | 🛠 | +| UP011 | LRUCacheWithoutParameters | Unnecessary parameters to `functools.lru_cache` | 🛠 | | UP012 | UnnecessaryEncodeUTF8 | Unnecessary call to `encode` as UTF-8 | 🛠 | | UP013 | ConvertTypedDictFunctionalToClass | Convert `...` from `TypedDict` functional to class syntax | 🛠 | | UP014 | ConvertNamedTupleFunctionalToClass | Convert `...` from `NamedTuple` functional to class syntax | 🛠 | @@ -726,6 +726,7 @@ For more, see [pyupgrade](https://pypi.org/project/pyupgrade/3.2.0/) on PyPI. | UP029 | UnnecessaryBuiltinImport | Unnecessary builtin import: `...` | 🛠 | | UP030 | FormatLiterals | Use implicit references for positional format fields | 🛠 | | UP032 | FString | Use f-string instead of `format` call | 🛠 | +| UP033 | FunctoolsCache | Use `@functools.cache` instead of `@functools.lru_cache(maxsize=None)` | 🛠 | ### pep8-naming (N) diff --git a/resources/test/fixtures/pyupgrade/UP011.py b/resources/test/fixtures/pyupgrade/UP011.py new file mode 100644 index 00000000000000..97a0337c6a4ceb --- /dev/null +++ b/resources/test/fixtures/pyupgrade/UP011.py @@ -0,0 +1,67 @@ +import functools +from functools import lru_cache + + +@functools.lru_cache() +def fixme(): + pass + + +@lru_cache() +def fixme(): + pass + + +@other_decorator +@functools.lru_cache() +def fixme(): + pass + + +@functools.lru_cache() +@other_decorator +def fixme(): + pass + + +@functools.lru_cache(maxsize=None) +def ok(): + pass + + +@lru_cache(maxsize=None) +def ok(): + pass + + +@functools.lru_cache(maxsize=64) +def ok(): + pass + + +@lru_cache(maxsize=64) +def ok(): + pass + + +def user_func(): + pass + + +@lru_cache(user_func) +def ok(): + pass + + +@lru_cache(user_func, maxsize=None) +def ok(): + pass + + +def lru_cache(maxsize=None): + pass + + +@lru_cache(maxsize=None) +def ok(): + pass diff --git a/resources/test/fixtures/pyupgrade/UP011_0.py b/resources/test/fixtures/pyupgrade/UP011_0.py deleted file mode 100644 index 3d54f51284f00a..00000000000000 --- a/resources/test/fixtures/pyupgrade/UP011_0.py +++ /dev/null @@ -1,103 +0,0 @@ -import functools -from functools import lru_cache - - -@lru_cache() -def fixme1(): - pass - - -@other_deco_after -@functools.lru_cache() -def fixme2(): - pass - - -@lru_cache(maxsize=None) -def fixme3(): - pass - - -@functools.lru_cache(maxsize=None) -@other_deco_before -def fixme4(): - pass - - -@lru_cache( # A -) # B -def fixme5(): - pass - - -@lru_cache( - # A -) # B -def fixme6(): - pass - - -@functools.lru_cache( - # A - maxsize = None) # B -def fixme7(): - pass - - -@functools.lru_cache( - # A1 - maxsize = None - # A2 -) # B -def fixme8(): - pass - - -@functools.lru_cache( - # A1 - maxsize = - None - # A2 - -) -def fixme9(): - pass - - -@functools.lru_cache( - # A1 - maxsize = - None - # A2 -) -def fixme10(): - pass - - -@lru_cache -def correct1(): - pass - - -@functools.lru_cache -def correct2(): - pass - - -@functoools.lru_cache(maxsize=64) -def correct3(): - pass - - -def user_func(): - pass - - -@lru_cache(user_func) -def correct4(): - pass - - -@lru_cache(user_func, maxsize=None) -def correct5(): - pass diff --git a/resources/test/fixtures/pyupgrade/UP011_1.py b/resources/test/fixtures/pyupgrade/UP011_1.py deleted file mode 100644 index b837fa30f8e341..00000000000000 --- a/resources/test/fixtures/pyupgrade/UP011_1.py +++ /dev/null @@ -1,15 +0,0 @@ -import functools - - -def lru_cache(maxsize=None): - pass - - -@lru_cache() -def dont_fixme(): - pass - - -@lru_cache(maxsize=None) -def dont_fixme(): - pass diff --git a/resources/test/fixtures/pyupgrade/UP033.py b/resources/test/fixtures/pyupgrade/UP033.py new file mode 100644 index 00000000000000..a14c7ff7139f83 --- /dev/null +++ b/resources/test/fixtures/pyupgrade/UP033.py @@ -0,0 +1,67 @@ +import functools +from functools import lru_cache + + +@functools.lru_cache(maxsize=None) +def fixme(): + pass + + +@lru_cache(maxsize=None) +def fixme(): + pass + + +@other_decorator +@functools.lru_cache(maxsize=None) +def fixme(): + pass + + +@functools.lru_cache(maxsize=None) +@other_decorator +def fixme(): + pass + + +@functools.lru_cache() +def ok(): + pass + + +@lru_cache() +def ok(): + pass + + +@functools.lru_cache(maxsize=64) +def ok(): + pass + + +@lru_cache(maxsize=64) +def ok(): + pass + + +def user_func(): + pass + + +@lru_cache(user_func) +def ok(): + pass + + +@lru_cache(user_func, maxsize=None) +def ok(): + pass + + +def lru_cache(maxsize=None): + pass + + +@lru_cache(maxsize=None) +def ok(): + pass diff --git a/ruff.schema.json b/ruff.schema.json index b1bcc896a35c5b..beef56bd417781 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -1688,6 +1688,7 @@ "UP03", "UP030", "UP032", + "UP033", "W", "W2", "W29", diff --git a/src/checkers/ast.rs b/src/checkers/ast.rs index 7dddb3cc50aa47..6cf3aa684c89cb 100644 --- a/src/checkers/ast.rs +++ b/src/checkers/ast.rs @@ -499,7 +499,12 @@ where if self.settings.rules.enabled(&RuleCode::UP011) && self.settings.target_version >= PythonVersion::Py38 { - pyupgrade::rules::unnecessary_lru_cache_params(self, decorator_list); + pyupgrade::rules::lru_cache_without_parameters(self, decorator_list); + } + if self.settings.rules.enabled(&RuleCode::UP033) + && self.settings.target_version >= PythonVersion::Py39 + { + pyupgrade::rules::functools_cache(self, decorator_list); } if self.settings.rules.enabled(&RuleCode::B018) { diff --git a/src/registry.rs b/src/registry.rs index 3a70c4d9926103..93d6db273ee906 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -232,7 +232,7 @@ ruff_macros::define_rule_mapping!( UP008 => violations::SuperCallWithParameters, UP009 => violations::PEP3120UnnecessaryCodingComment, UP010 => violations::UnnecessaryFutureImport, - UP011 => violations::UnnecessaryLRUCacheParams, + UP011 => violations::LRUCacheWithoutParameters, UP012 => violations::UnnecessaryEncodeUTF8, UP013 => violations::ConvertTypedDictFunctionalToClass, UP014 => violations::ConvertNamedTupleFunctionalToClass, @@ -253,6 +253,7 @@ ruff_macros::define_rule_mapping!( UP029 => violations::UnnecessaryBuiltinImport, UP030 => violations::FormatLiterals, UP032 => violations::FString, + UP033 => violations::FunctoolsCache, // pydocstyle D100 => violations::PublicModule, D101 => violations::PublicClass, diff --git a/src/rules/pyupgrade/mod.rs b/src/rules/pyupgrade/mod.rs index 0dc209b22a6599..fe0d4e051f8137 100644 --- a/src/rules/pyupgrade/mod.rs +++ b/src/rules/pyupgrade/mod.rs @@ -30,8 +30,7 @@ mod tests { #[test_case(RuleCode::UP009, Path::new("UP009_3.py"); "UP009_3")] #[test_case(RuleCode::UP009, Path::new("UP009_4.py"); "UP009_4")] #[test_case(RuleCode::UP010, Path::new("UP010.py"); "UP010")] - #[test_case(RuleCode::UP011, Path::new("UP011_0.py"); "UP011_0")] - #[test_case(RuleCode::UP011, Path::new("UP011_1.py"); "UP011_1")] + #[test_case(RuleCode::UP011, Path::new("UP011.py"); "UP011")] #[test_case(RuleCode::UP012, Path::new("UP012.py"); "UP012")] #[test_case(RuleCode::UP013, Path::new("UP013.py"); "UP013")] #[test_case(RuleCode::UP014, Path::new("UP014.py"); "UP014")] @@ -55,6 +54,7 @@ mod tests { #[test_case(RuleCode::UP030, Path::new("UP030_0.py"); "UP030_0")] #[test_case(RuleCode::UP030, Path::new("UP030_1.py"); "UP030_1")] #[test_case(RuleCode::UP032, Path::new("UP032.py"); "UP032")] + #[test_case(RuleCode::UP033, Path::new("UP033.py"); "UP033")] fn rules(rule_code: RuleCode, path: &Path) -> Result<()> { let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy()); let diagnostics = test_path( diff --git a/src/rules/pyupgrade/rules/functools_cache.rs b/src/rules/pyupgrade/rules/functools_cache.rs new file mode 100644 index 00000000000000..5c5f290ed8010e --- /dev/null +++ b/src/rules/pyupgrade/rules/functools_cache.rs @@ -0,0 +1,63 @@ +use rustpython_ast::{Constant, ExprKind, KeywordData}; +use rustpython_parser::ast::Expr; + +use crate::ast::helpers::{create_expr, unparse_expr}; +use crate::ast::types::Range; +use crate::checkers::ast::Checker; +use crate::fix::Fix; +use crate::registry::{Diagnostic, RuleCode}; +use crate::violations; + +/// UP033 +pub fn functools_cache(checker: &mut Checker, decorator_list: &[Expr]) { + for expr in decorator_list.iter() { + let ExprKind::Call { + func, + args, + keywords, + } = &expr.node else { + continue; + }; + + // Look for, e.g., `import functools; @functools.lru_cache(maxsize=None)`. + if args.is_empty() + && keywords.len() == 1 + && checker + .resolve_call_path(func) + .map_or(false, |call_path| call_path == ["functools", "lru_cache"]) + { + let KeywordData { arg, value } = &keywords[0].node; + if arg.as_ref().map_or(false, |arg| arg == "maxsize") + && matches!( + value.node, + ExprKind::Constant { + value: Constant::None, + kind: None, + } + ) + { + let mut diagnostic = Diagnostic::new( + violations::FunctoolsCache, + Range::new(func.end_location.unwrap(), expr.end_location.unwrap()), + ); + if checker.patch(&RuleCode::UP033) { + if let ExprKind::Attribute { value, ctx, .. } = &func.node { + diagnostic.amend(Fix::replacement( + unparse_expr( + &create_expr(ExprKind::Attribute { + value: value.clone(), + attr: "cache".to_string(), + ctx: ctx.clone(), + }), + checker.stylist, + ), + expr.location, + expr.end_location.unwrap(), + )); + } + } + checker.diagnostics.push(diagnostic); + } + } + } +} diff --git a/src/rules/pyupgrade/rules/lru_cache_without_parameters.rs b/src/rules/pyupgrade/rules/lru_cache_without_parameters.rs new file mode 100644 index 00000000000000..b967f99d6fcf96 --- /dev/null +++ b/src/rules/pyupgrade/rules/lru_cache_without_parameters.rs @@ -0,0 +1,43 @@ +use rustpython_ast::ExprKind; +use rustpython_parser::ast::Expr; + +use crate::ast::helpers::unparse_expr; +use crate::ast::types::Range; +use crate::checkers::ast::Checker; +use crate::fix::Fix; +use crate::registry::{Diagnostic, RuleCode}; +use crate::violations; + +/// UP011 +pub fn lru_cache_without_parameters(checker: &mut Checker, decorator_list: &[Expr]) { + for expr in decorator_list.iter() { + let ExprKind::Call { + func, + args, + keywords, + } = &expr.node else { + continue; + }; + + // Look for, e.g., `import functools; @functools.lru_cache()`. + if args.is_empty() + && keywords.is_empty() + && checker + .resolve_call_path(func) + .map_or(false, |call_path| call_path == ["functools", "lru_cache"]) + { + let mut diagnostic = Diagnostic::new( + violations::LRUCacheWithoutParameters, + Range::new(func.end_location.unwrap(), expr.end_location.unwrap()), + ); + if checker.patch(&RuleCode::UP011) { + diagnostic.amend(Fix::replacement( + unparse_expr(func, checker.stylist), + expr.location, + expr.end_location.unwrap(), + )); + } + checker.diagnostics.push(diagnostic); + } + } +} diff --git a/src/rules/pyupgrade/rules/mod.rs b/src/rules/pyupgrade/rules/mod.rs index 7c9576d720b43e..af4c33d93b5881 100644 --- a/src/rules/pyupgrade/rules/mod.rs +++ b/src/rules/pyupgrade/rules/mod.rs @@ -4,6 +4,8 @@ pub(crate) use datetime_utc_alias::datetime_utc_alias; pub(crate) use deprecated_unittest_alias::deprecated_unittest_alias; pub(crate) use f_strings::f_strings; pub(crate) use format_literals::format_literals; +pub(crate) use functools_cache::functools_cache; +pub(crate) use lru_cache_without_parameters::lru_cache_without_parameters; pub(crate) use native_literals::native_literals; use once_cell::sync::Lazy; pub(crate) use open_alias::open_alias; @@ -25,7 +27,6 @@ pub(crate) use typing_text_str_alias::typing_text_str_alias; pub(crate) use unnecessary_builtin_import::unnecessary_builtin_import; pub(crate) use unnecessary_encode_utf8::unnecessary_encode_utf8; pub(crate) use unnecessary_future_import::unnecessary_future_import; -pub(crate) use unnecessary_lru_cache_params::unnecessary_lru_cache_params; pub(crate) use unpack_list_comprehension::unpack_list_comprehension; pub(crate) use use_pep585_annotation::use_pep585_annotation; pub(crate) use use_pep604_annotation::use_pep604_annotation; @@ -44,6 +45,8 @@ mod datetime_utc_alias; mod deprecated_unittest_alias; mod f_strings; mod format_literals; +mod functools_cache; +mod lru_cache_without_parameters; mod native_literals; mod open_alias; mod os_error_alias; @@ -61,7 +64,6 @@ mod typing_text_str_alias; mod unnecessary_builtin_import; mod unnecessary_encode_utf8; mod unnecessary_future_import; -mod unnecessary_lru_cache_params; mod unpack_list_comprehension; mod use_pep585_annotation; mod use_pep604_annotation; diff --git a/src/rules/pyupgrade/rules/unnecessary_lru_cache_params.rs b/src/rules/pyupgrade/rules/unnecessary_lru_cache_params.rs deleted file mode 100644 index e75434395dd00f..00000000000000 --- a/src/rules/pyupgrade/rules/unnecessary_lru_cache_params.rs +++ /dev/null @@ -1,88 +0,0 @@ -use rustpython_ast::{Constant, ExprKind, KeywordData}; -use rustpython_parser::ast::Expr; - -use crate::ast::helpers::{create_expr, unparse_expr}; -use crate::ast::types::Range; -use crate::checkers::ast::Checker; -use crate::fix::Fix; -use crate::registry::{Diagnostic, RuleCode}; -use crate::settings::types::PythonVersion; -use crate::violations; - -/// UP011 -pub fn unnecessary_lru_cache_params(checker: &mut Checker, decorator_list: &[Expr]) { - for expr in decorator_list.iter() { - let ExprKind::Call { - func, - args, - keywords, - } = &expr.node else { - continue; - }; - - // Look for, e.g., `import functools; @functools.lru_cache`. - if !(args.is_empty() - && checker - .resolve_call_path(func) - .map_or(false, |call_path| call_path == ["functools", "lru_cache"])) - { - continue; - } - - // Ex) `functools.lru_cache()` - if keywords.is_empty() { - let mut diagnostic = Diagnostic::new( - violations::UnnecessaryLRUCacheParams, - Range::new(func.end_location.unwrap(), expr.end_location.unwrap()), - ); - if checker.patch(&RuleCode::UP011) { - diagnostic.amend(Fix::replacement( - unparse_expr(func, checker.stylist), - expr.location, - expr.end_location.unwrap(), - )); - } - checker.diagnostics.push(diagnostic); - } - - // Ex) `functools.lru_cache(maxsize=None)` - if !(checker.settings.target_version >= PythonVersion::Py39 && keywords.len() == 1) { - continue; - } - - let KeywordData { arg, value } = &keywords[0].node; - if !(arg.as_ref().map_or(false, |arg| arg == "maxsize") - && matches!( - value.node, - ExprKind::Constant { - value: Constant::None, - kind: None, - } - )) - { - continue; - } - - let mut diagnostic = Diagnostic::new( - violations::UnnecessaryLRUCacheParams, - Range::new(func.end_location.unwrap(), expr.end_location.unwrap()), - ); - if checker.patch(&RuleCode::UP011) { - if let ExprKind::Attribute { value, ctx, .. } = &func.node { - diagnostic.amend(Fix::replacement( - unparse_expr( - &create_expr(ExprKind::Attribute { - value: value.clone(), - attr: "cache".to_string(), - ctx: ctx.clone(), - }), - checker.stylist, - ), - expr.location, - expr.end_location.unwrap(), - )); - } - } - checker.diagnostics.push(diagnostic); - } -} diff --git a/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011.py.snap b/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011.py.snap new file mode 100644 index 00000000000000..8cb320a7a865fc --- /dev/null +++ b/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011.py.snap @@ -0,0 +1,73 @@ +--- +source: src/rules/pyupgrade/mod.rs +expression: diagnostics +--- +- kind: + LRUCacheWithoutParameters: ~ + location: + row: 5 + column: 20 + end_location: + row: 5 + column: 22 + fix: + content: functools.lru_cache + location: + row: 5 + column: 1 + end_location: + row: 5 + column: 22 + parent: ~ +- kind: + LRUCacheWithoutParameters: ~ + location: + row: 10 + column: 10 + end_location: + row: 10 + column: 12 + fix: + content: lru_cache + location: + row: 10 + column: 1 + end_location: + row: 10 + column: 12 + parent: ~ +- kind: + LRUCacheWithoutParameters: ~ + location: + row: 16 + column: 20 + end_location: + row: 16 + column: 22 + fix: + content: functools.lru_cache + location: + row: 16 + column: 1 + end_location: + row: 16 + column: 22 + parent: ~ +- kind: + LRUCacheWithoutParameters: ~ + location: + row: 21 + column: 20 + end_location: + row: 21 + column: 22 + fix: + content: functools.lru_cache + location: + row: 21 + column: 1 + end_location: + row: 21 + column: 22 + parent: ~ + diff --git a/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011_0.py.snap b/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011_0.py.snap deleted file mode 100644 index 18bc443208b413..00000000000000 --- a/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011_0.py.snap +++ /dev/null @@ -1,168 +0,0 @@ ---- -source: src/rules/pyupgrade/mod.rs -expression: diagnostics ---- -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 5 - column: 10 - end_location: - row: 5 - column: 12 - fix: - content: lru_cache - location: - row: 5 - column: 1 - end_location: - row: 5 - column: 12 - parent: ~ -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 11 - column: 20 - end_location: - row: 11 - column: 22 - fix: - content: functools.lru_cache - location: - row: 11 - column: 1 - end_location: - row: 11 - column: 22 - parent: ~ -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 16 - column: 10 - end_location: - row: 16 - column: 24 - fix: ~ - parent: ~ -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 21 - column: 20 - end_location: - row: 21 - column: 34 - fix: - content: functools.cache - location: - row: 21 - column: 1 - end_location: - row: 21 - column: 34 - parent: ~ -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 27 - column: 10 - end_location: - row: 28 - column: 1 - fix: - content: lru_cache - location: - row: 27 - column: 1 - end_location: - row: 28 - column: 1 - parent: ~ -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 33 - column: 10 - end_location: - row: 35 - column: 1 - fix: - content: lru_cache - location: - row: 33 - column: 1 - end_location: - row: 35 - column: 1 - parent: ~ -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 40 - column: 20 - end_location: - row: 42 - column: 19 - fix: - content: functools.cache - location: - row: 40 - column: 1 - end_location: - row: 42 - column: 19 - parent: ~ -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 47 - column: 20 - end_location: - row: 51 - column: 1 - fix: - content: functools.cache - location: - row: 47 - column: 1 - end_location: - row: 51 - column: 1 - parent: ~ -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 56 - column: 20 - end_location: - row: 62 - column: 1 - fix: - content: functools.cache - location: - row: 56 - column: 1 - end_location: - row: 62 - column: 1 - parent: ~ -- kind: - UnnecessaryLRUCacheParams: ~ - location: - row: 67 - column: 20 - end_location: - row: 72 - column: 1 - fix: - content: functools.cache - location: - row: 67 - column: 1 - end_location: - row: 72 - column: 1 - parent: ~ - diff --git a/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011_1.py.snap b/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011_1.py.snap deleted file mode 100644 index ca9e52f5c58f0e..00000000000000 --- a/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP011_UP011_1.py.snap +++ /dev/null @@ -1,6 +0,0 @@ ---- -source: src/rules/pyupgrade/mod.rs -expression: diagnostics ---- -[] - diff --git a/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP033_UP033.py.snap b/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP033_UP033.py.snap new file mode 100644 index 00000000000000..def3f5f4ffc302 --- /dev/null +++ b/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP033_UP033.py.snap @@ -0,0 +1,66 @@ +--- +source: src/rules/pyupgrade/mod.rs +expression: diagnostics +--- +- kind: + FunctoolsCache: ~ + location: + row: 5 + column: 20 + end_location: + row: 5 + column: 34 + fix: + content: functools.cache + location: + row: 5 + column: 1 + end_location: + row: 5 + column: 34 + parent: ~ +- kind: + FunctoolsCache: ~ + location: + row: 10 + column: 10 + end_location: + row: 10 + column: 24 + fix: ~ + parent: ~ +- kind: + FunctoolsCache: ~ + location: + row: 16 + column: 20 + end_location: + row: 16 + column: 34 + fix: + content: functools.cache + location: + row: 16 + column: 1 + end_location: + row: 16 + column: 34 + parent: ~ +- kind: + FunctoolsCache: ~ + location: + row: 21 + column: 20 + end_location: + row: 21 + column: 34 + fix: + content: functools.cache + location: + row: 21 + column: 1 + end_location: + row: 21 + column: 34 + parent: ~ + diff --git a/src/violations.rs b/src/violations.rs index a329df0bcbe004..ee57946a42bf06 100644 --- a/src/violations.rs +++ b/src/violations.rs @@ -3396,9 +3396,9 @@ impl AlwaysAutofixableViolation for UnnecessaryFutureImport { } define_violation!( - pub struct UnnecessaryLRUCacheParams; + pub struct LRUCacheWithoutParameters; ); -impl AlwaysAutofixableViolation for UnnecessaryLRUCacheParams { +impl AlwaysAutofixableViolation for LRUCacheWithoutParameters { fn message(&self) -> String { "Unnecessary parameters to `functools.lru_cache`".to_string() } @@ -3408,7 +3408,7 @@ impl AlwaysAutofixableViolation for UnnecessaryLRUCacheParams { } fn placeholder() -> Self { - UnnecessaryLRUCacheParams + LRUCacheWithoutParameters } } @@ -3814,6 +3814,23 @@ impl AlwaysAutofixableViolation for FString { } } +define_violation!( + pub struct FunctoolsCache; +); +impl AlwaysAutofixableViolation for FunctoolsCache { + fn message(&self) -> String { + "Use `@functools.cache` instead of `@functools.lru_cache(maxsize=None)`".to_string() + } + + fn autofix_title(&self) -> String { + "Rewrite with `@functools.cache".to_string() + } + + fn placeholder() -> Self { + FunctoolsCache + } +} + // pydocstyle define_violation!(