Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RUF010 #8

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
rev: v0.2.2
hooks:
- id: ruff-format
- id: ruff
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.3.0 (2024-02-18)

- Add `RUF010` check.

## 0.2.0 (2024-02-17)

- Add `RUF018` check.
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ to enable the plugin.

## Checks

### RUF010 Use explicit conversion flag

Checks for `str()`, `repr()`, and `ascii()` as explicit conversions within
f-strings.

For example, replace

```python
f"{ascii(foo)}, {repr(bar)}, {str(baz)}"
```

with

```python
f"{foo!a}, {bar!r}, {baz!s}"
```

or, often (such as where `__str__` and `__format__` are equivalent),

```python
f"{foo!a}, {bar!r}, {baz}"
```

Derived from [explicit-f-string-type-conversion (RUF010)](https://docs.astral.sh/ruff/rules/explicit-f-string-type-conversion/).

### RUF018 Avoid assignment expressions in `assert` statements

Checks for named assignment expressions in `assert` statements. When Python is
Expand Down
36 changes: 18 additions & 18 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "flake8-ruff"
version = "0.2.0"
version = "0.3.0"
description = "A Flake8 plugin that implements miscellaneous checks from Ruff."
license = "MIT"
authors = ["Tom Kuson <[email protected]>"]
Expand Down
20 changes: 20 additions & 0 deletions src/flake8_ruff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ast
import typing

RUF010 = "RUF010 Use explicit conversion flag"
RUF018 = "RUF018 Avoid assignment expressions in assert statements"
RUF020 = "RUF020 {} | T is equivalent to T"
RUF025 = "RUF025 Unnecessary dict comprehension for iterable; use dict.fromkeys instead"
Expand Down Expand Up @@ -37,6 +38,24 @@ def visit(self, node: ast.AST) -> None:
super().visit(node)
self._stack.pop()

def visit_FormattedValue(self, node: ast.FormattedValue) -> None:
if (
node.conversion == -1
and isinstance(node.value, ast.Call)
and not node.value.keywords
and len(node.value.args) == 1
and not isinstance(
node.value.args[0], (ast.Dict, ast.DictComp, ast.Set, ast.SetComp)
)
and isinstance(node.value.func, ast.Name)
and node.value.func.id in {"ascii", "repr", "str"}
):
self.errors.append((
node.lineno,
node.col_offset,
RUF010,
))

def visit_DictComp(self, node: ast.DictComp) -> None:
if (
len(node.generators) == 1
Expand Down Expand Up @@ -79,6 +98,7 @@ def visit_Subscript(self, node: ast.Subscript) -> None:

def visit_Assert(self, node: ast.Assert) -> None:
if isinstance(node.test, ast.NamedExpr):
# TODO(tom): Determine why col_offset is different on Python 3.12
self.errors.append((
node.lineno,
node.col_offset,
Expand Down
40 changes: 40 additions & 0 deletions tests/test_flake8_ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,46 @@ def run(source: str) -> list[tuple[int, int, str]]:
return [(line, col, msg) for (line, col, msg, type_) in Plugin(tree).run()]


def test_ruf010_ascii() -> None:
src = """\
f"abc {ascii(bar)} xyz"
"""
expected_msg = "RUF010 Use explicit conversion flag"
assert expected_msg == run(src)[0][2]


def test_ruf010_repr() -> None:
src = """\
f"abc {repr(123)} xyz"
"""
expected_msg = "RUF010 Use explicit conversion flag"
assert expected_msg == run(src)[0][2]


def test_ruf010_str() -> None:
src = """\
f"abc {(str(123))} xyz"
"""
expected_msg = "RUF010 Use explicit conversion flag"
assert expected_msg == run(src)[0][2]


def test_ruf010_set() -> None:
src = """\
f"abc {str({})} xyz"
"""
expected: list[tuple[int, int, str]] = []
assert expected == run(src)


def test_ruf010_dict() -> None:
src = """\
f"abc {str({k: v for k, v in enumerate(foo)})} xyz"
"""
expected: list[tuple[int, int, str]] = []
assert expected == run(src)


def test_ruf018() -> None:
src = """\
assert (x := 1), "message"
Expand Down
Loading