Enhancement: Remove redundant code using list comprehension
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 8.3k
- Forks
- 835
- Avg merge
- 5d 3h
- Merged PRs (30d)
- 1
Description
Is your feature request related to a problem? Please describe.
Currently, In bandit/blacklists/imports.py There is redundant appending code for every blacklist items, that we can easily remove using list comprehension. i.e. appending blacklists items dict to list i.e
def gen_blacklist():
sets = []
sets.append(utils.build_conf_dict(
'import_telnetlib', 'B401', ['telnetlib'],
'A telnet-related module is being imported. Telnet is '
'considered insecure. Use SSH or some other encrypted protocol.',
'HIGH'
))
sets.append(utils.build_conf_dict(
'import_ftplib', 'B402', ['ftplib'],
'A FTP-related module is being imported. FTP is considered '
'insecure. Use SSH/SFTP/SCP or some other encrypted protocol.',
'HIGH'
))
...
...
return {'Import': sets, 'ImportFrom': sets, 'Call': sets}
Describe the solution you'd like
This can be achieved by using list comprehension i.e.
def gen_blacklist2():
BLACKLISTS = [
['import_telnetlib',
'B401',
['telnetlib'],
'A telnet-related module is being imported. Telnet is '
'considered insecure. Use SSH or some other encrypted protocol.',
'HIGH'],
['import_ftplib',
'B402',
['ftplib'],
'A FTP-related module is being imported. FTP is considered '
'insecure. Use SSH/SFTP/SCP or some other encrypted protocol.',
'HIGH'],
...
sets = [utils.build_conf_dict(*blacklist) for blacklist in BLACKLISTS]
return {'Import': sets, 'ImportFrom': sets, 'Call': sets}
If this is done for purpose then ignore this issue otherwise it is nice to have code in list comprehension for easy to read and understand.
I am happy to make PR. Let me know if its a good idea to have list comprehension
Describe alternatives you've considered
dictionary comprehension
Additional context
There are other files in the bandit module that requires improvement.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in bandit/blacklists/imports.py at gen_blacklist and review how each blacklist entry is assembled with utils.build_conf_dict. Refactor the repeated appending structure as proposed, then verify that the returned Import, ImportFrom, and Call collections still contain the same blacklist dictionaries.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- security, tooling
- Issue type
- Refactor
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100