Skip to content

Enhancement: Remove redundant code using list comprehension #620

@acharles7

Description

@acharles7

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions