Skip to content

Commit

Permalink
Move @functools.cache rewrites to their own rule
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 17, 2023
1 parent 70ea4b2 commit 84154ba
Show file tree
Hide file tree
Showing 19 changed files with 424 additions and 390 deletions.
8 changes: 8 additions & 0 deletions BREAKING_CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Breaking Changes

## 0.0.225

### `@functools.cache` rewrites have been moved to a standalone rule (`UP033`) ([#1938](https://github.com/charliermarsh/ruff/pull/1938))

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))
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | 🛠 |
Expand All @@ -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)

Expand Down
67 changes: 67 additions & 0 deletions resources/test/fixtures/pyupgrade/UP011.py
Original file line number Diff line number Diff line change
@@ -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
103 changes: 0 additions & 103 deletions resources/test/fixtures/pyupgrade/UP011_0.py

This file was deleted.

15 changes: 0 additions & 15 deletions resources/test/fixtures/pyupgrade/UP011_1.py

This file was deleted.

67 changes: 67 additions & 0 deletions resources/test/fixtures/pyupgrade/UP033.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,7 @@
"UP03",
"UP030",
"UP032",
"UP033",
"W",
"W2",
"W29",
Expand Down
7 changes: 6 additions & 1 deletion src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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(
Expand Down
Loading

0 comments on commit 84154ba

Please sign in to comment.