PyCQA / PyCQA/bandit

Enhancement: Remove redundant code using list comprehension

Open
#620 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.