Skip to content

Add native type literals to UP018 #5988

@dosisod

Description

@dosisod

See #1348 (comment).

UP018/C408 detects some, but not all of the same errors as Refurb:

# detected by Ruff and Refurb
l = list()
d = dict()
t = tuple()

# only detected by Refurb
i = int()
f = float()
c = complex()
b = bool()

# missleading error message in Ruff, see below
by = bytes()
s = str()

Running in Ruff:

$ ruff x.py
x.py:1:1: E741 Ambiguous variable name: `l`
x.py:1:5: C408 [*] Unnecessary `list` call (rewrite as a literal)
x.py:2:5: C408 [*] Unnecessary `dict` call (rewrite as a literal)
x.py:3:5: C408 [*] Unnecessary `tuple` call (rewrite as a literal)
x.py:10:6: UP018 [*] Unnecessary call to `bytes`
x.py:11:5: UP018 [*] Unnecessary call to `str`

Running in Refurb:

$ refurb x.py
x.py:1:5 [FURB112]: Replace `list()` with `[]`
x.py:2:5 [FURB112]: Replace `dict()` with `{}`
x.py:3:5 [FURB112]: Replace `tuple()` with `()`
x.py:5:5 [FURB112]: Replace `int()` with `0`
x.py:6:5 [FURB112]: Replace `float()` with `0.0`
x.py:7:5 [FURB112]: Replace `complex()` with `0j`
x.py:8:5 [FURB112]: Replace `bool()` with `False`
x.py:10:6 [FURB112]: Replace `bytes()` with `b""`
x.py:11:5 [FURB112]: Replace `str()` with `""`

The "Unnecessary call to bytes/str" message is also misleading because there are no arguments to str()/bytes().

Very few projects according to grep.app are using the int(), str(), etc. form, but still worth adding IMO.

Metadata

Metadata

Assignees

Labels

ruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions