pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.27k stars 1.13k forks source link

New check: manually accessing ``sys.argv`` value using indexes or range #8365

Open Pierre-Sassoulas opened 1 year ago

Pierre-Sassoulas commented 1 year ago

If sys.argv is accessed directly it means you're not going to have the same level of support for errors than a dedicated library, and argparse is such a lib that is a builtin lib since python 3.2.

Originally posted by @Pierre-Sassoulas in https://github.com/PyCQA/pylint/issues/7710#issuecomment-1450254075

nickdrozd commented 1 year ago

This means unconditionally flagging any use of sys.argv and suggesting argparse instead?

DanielNoord commented 1 year ago

This very arbitrary and something that I would normally advise to make as a plug-in.

As a hack you could also use suggested-module with a non sensical replacement value.

Pierre-Sassoulas commented 1 year ago

Yes, it's clearly an optional extension candidate.

any use of sys.argv and suggesting argparse instead?

Not sys.argv alone, but any access of sys.argv using indexes or range, like my_arg = sys.argv[2], a, b, c = sys.argv[1:]. (and suggest 'argparse or another arg parsing library')

As a hack you could also use suggested-module with a non sensical replacement value.

I did not understand that part, I don't think we have a suggested-module option ?

DanielNoord commented 1 year ago

--preferred-modules 😄

https://pylint.pycqa.org/en/latest/user_guide/messages/warning/preferred-module.html

Pierre-Sassoulas commented 1 year ago

I liked the idea but preferred-module is checking at import time so this can't work atm:

[IMPORTS]
preferred-modules=json:ujson,re:regex,sys.argv:argparse;
"""Test preferred modules."""
# pylint: disable=unused-import

import json  # [preferred-module]
import sys
from re import search  # [preferred-module]

print(sys.argv[1:3])  # [preferred-module] <= not raised 
DanielNoord commented 1 year ago

Ah, I misremembered https://github.com/PyCQA/pylint/pull/8186