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 native type literals to UP018 #5988

Closed
dosisod opened this issue Jul 22, 2023 · 1 comment · Fixed by #6013
Closed

Add native type literals to UP018 #5988

dosisod opened this issue Jul 22, 2023 · 1 comment · Fixed by #6013
Assignees
Labels
rule Implementing or modifying a lint rule

Comments

@dosisod
Copy link
Contributor

dosisod commented Jul 22, 2023

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.

@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Jul 23, 2023
@dhruvmanila dhruvmanila self-assigned this Jul 23, 2023
@dhruvmanila
Copy link
Member

Complex seems a bit difficult (no pun intended) as it can accept 0, 1 or 2 arguments and it can accept them as keyword arguments as well:

complex()
# 0j

complex(1)
# (1+0j)

complex(1, 2)
# (1+2j)

complex(real=1, imag=2)
# (1+2j)

complex(1, imag=2)
# (1+2j)

charliermarsh pushed a commit that referenced this issue Jul 24, 2023
## Summary

This pull request add supports for `int`, `float` and `bool` types in
`UP018`
rule to convert empty call to the default value of the type or remove
the call
if a value of the same type is provided as an argument.

## Test Plan

Added tests for `int`, `float` and `bool` types.

Partially resolves #5988
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants