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

C410, C411, and C418 have false positives for calls with extra arguments #15810

Closed
dscorbett opened this issue Jan 29, 2025 · 0 comments · Fixed by #15838
Closed

C410, C411, and C418 have false positives for calls with extra arguments #15810

dscorbett opened this issue Jan 29, 2025 · 0 comments · Fixed by #15838
Labels
bug Something isn't working rule Implementing or modifying a lint rule

Comments

@dscorbett
Copy link

Description

unnecessary-literal-within-list-call (C410), unnecessary-list-call (C411), and unnecessary-literal-within-dict-call (C418) have false positives in Ruff 0.9.3 when the list or dict call has too many positional arguments or (for C411) there is a keyword argument. Their fixes suppress TypeErrors.

$ cat >c41.py <<'# EOF'
try:
    print(list([1], [2]))
except TypeError as e:
    print(e)
try:
    print(list([x for x in "XYZ"], []))
except TypeError as e:
    print(e)
try:
    print(list([x for x in "XYZ"], foo=[]))
except TypeError as e:
    print(e)
try:
    print(dict({"A": 1}, {"B": 2}))
except TypeError as e:
    print(e)
# EOF

$ python c41.py
list expected at most 1 argument, got 2
list expected at most 1 argument, got 2
list() takes no keyword arguments
dict expected at most 1 argument, got 2

$ ruff --isolated check  --select C410,C411,C418 c41.py --unsafe-fixes --fix
Found 4 errors (4 fixed, 0 remaining).

$ cat c41.py
try:
    print([1])
except TypeError as e:
    print(e)
try:
    print([x for x in "XYZ"])
except TypeError as e:
    print(e)
try:
    print([x for x in "XYZ"])
except TypeError as e:
    print(e)
try:
    print({"A": 1})
except TypeError as e:
    print(e)

$ python c41.py
[1]
['X', 'Y', 'Z']
['X', 'Y', 'Z']
{'A': 1}
@dylwil3 dylwil3 added bug Something isn't working rule Implementing or modifying a lint rule labels Jan 30, 2025
dylwil3 added a commit that referenced this issue Jan 30, 2025
… (kw)args for `C410`,`C411`, and `C418` (#15838)

Both `list` and `dict` expect only a single positional argument. Giving
more positional arguments, or a keyword argument, is a `TypeError` and
neither the lint rule nor its fix make sense in that context.

Closes #15810
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rule Implementing or modifying a lint rule
Projects
None yet
2 participants