Skip to content

Commit

Permalink
Use list literal instead of list()
Browse files Browse the repository at this point in the history
dict, tuple, and list literals are actually single instructions in the CPython virtual machine, so it's always faster and better form to use them over the type constructors.
  • Loading branch information
adamchainz committed Sep 10, 2018
1 parent d876531 commit 830545a
Show file tree
Hide file tree
Showing 103 changed files with 223 additions and 223 deletions.
6 changes: 3 additions & 3 deletions docs/getting_started/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MyNewRule(CloudFormationLintRule):
def match(self, cfn):
"""Basic Rule Matching"""

matches = list()
matches = []

# Your Rule code goes here

Expand Down Expand Up @@ -208,7 +208,7 @@ The following snippet is a simple example on checking specific property values:
```python
def check_value(self, value, path):
"""Check SecurityGroup descriptions"""
matches = list()
matches = []
full_path = ('/'.join(str(x) for x in path))

# Check max length
Expand All @@ -219,7 +219,7 @@ def check_value(self, value, path):
def match(self, cfn):
"""Check SecurityGroup descriptions"""

matches = list()
matches = []

resources = cfn.get_resources(['AWS::EC2::SecurityGroup'])

Expand Down
4 changes: 2 additions & 2 deletions examples/rules/PropertiesTagsIncluded.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_resources_with_tags(self, region):
resourcespecs = cfnlint.helpers.RESOURCE_SPECS[region]
resourcetypes = resourcespecs['ResourceTypes']

matches = list()
matches = []
for resourcetype, resourceobj in resourcetypes.items():
propertiesobj = resourceobj.get('Properties')
if propertiesobj:
Expand All @@ -43,7 +43,7 @@ def get_resources_with_tags(self, region):
def match(self, cfn):
"""Check Tags for required keys"""

matches = list()
matches = []

all_tags = cfn.search_deep_keys('Tags')
all_tags = [x for x in all_tags if x[0] == 'Resources']
Expand Down
2 changes: 1 addition & 1 deletion examples/rules/PropertiesTagsRequired.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PropertiesTagsRequired(CloudFormationLintRule):
def match(self, cfn):
"""Check Tags for required keys"""

matches = list()
matches = []

required_tags = ['CostCenter', 'ApplicationName']

Expand Down
32 changes: 16 additions & 16 deletions src/cfnlint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def is_rule_enabled(self, rule_id):

def resource_property(self, filename, cfn, path, properties, resource_type, property_type):
"""Run loops in resource checks for embedded properties"""
matches = list()
matches = []
property_spec = cfnlint.helpers.RESOURCE_SPECS['us-east-1'].get('PropertyTypes')
if property_type == 'Tag':
property_spec_name = 'Tag'
Expand Down Expand Up @@ -221,7 +221,7 @@ def resource_property(self, filename, cfn, path, properties, resource_type, prop

def run_resource(self, filename, cfn, resource_type, resource_properties, path):
"""Run loops in resource checks for embedded properties"""
matches = list()
matches = []
resource_spec = cfnlint.helpers.RESOURCE_SPECS['us-east-1'].get('ResourceTypes')
if resource_properties and resource_type in resource_spec:
resource_spec_properties = resource_spec.get(resource_type, {}).get('Properties')
Expand Down Expand Up @@ -259,7 +259,7 @@ def run_resource(self, filename, cfn, resource_type, resource_properties, path):

def run(self, filename, cfn):
"""Run rules"""
matches = list()
matches = []
for rule in self.rules:
try:
matches.extend(rule.matchall(filename, cfn))
Expand Down Expand Up @@ -423,7 +423,7 @@ def get_mappings(self):
def get_resource_names(self):
"""Get all the Resource Names"""
LOGGER.debug('Get the names of all resources from template...')
results = list()
results = []
resources = self.template.get('Resources', {})
if isinstance(resources, dict):
for resourcename, _ in resources.items():
Expand All @@ -434,7 +434,7 @@ def get_resource_names(self):
def get_parameter_names(self):
"""Get all Parameter Names"""
LOGGER.debug('Get names of all parameters from template...')
results = list()
results = []
parameters = self.template.get('Parameters', {})
if isinstance(parameters, dict):
for parametername, _ in parameters.items():
Expand Down Expand Up @@ -524,15 +524,15 @@ def _get_sub_resource_properties(self, keys, properties, path):
if results:
return results
elif isinstance(properties, list):
matches = list()
matches = []
for index, item in enumerate(properties):
results = None
if isinstance(item, dict):
if len(item) == 1:
for sub_key, sub_value in item.items():
if sub_key in cfnlint.helpers.CONDITION_FUNCTIONS:
cond_values = self.get_condition_values(sub_value)
results = list()
results = []
for cond_value in cond_values:
result_path = path[:] + [index, sub_key] + cond_value['Path']
results.extend(
Expand All @@ -551,12 +551,12 @@ def _get_sub_resource_properties(self, keys, properties, path):
matches.extend(results)
return matches

return list()
return []

def get_resource_properties(self, keys):
"""Filter keys of template"""
LOGGER.debug('Get Properties from a resource: %s', keys)
matches = list()
matches = []
resourcetype = keys.pop(0)
for resource_name, resource_value in self.get_resources(resourcetype).items():
path = ['Resources', resource_name, 'Properties']
Expand All @@ -570,7 +570,7 @@ def get_resource_properties(self, keys):
# pylint: disable=dangerous-default-value
def _search_deep_keys(self, searchText, cfndict, path):
"""Search deep for keys and get their values"""
keys = list()
keys = []
if isinstance(cfndict, dict):
for key in cfndict:
pathprop = path[:]
Expand Down Expand Up @@ -606,7 +606,7 @@ def search_deep_keys(self, searchText):
def get_condition_values(self, template, path=[]):
"""Evaluates conditions and brings back the values"""
LOGGER.debug('Get condition values...')
matches = list()
matches = []
if not isinstance(template, list):
return matches
if not len(template) == 3:
Expand Down Expand Up @@ -656,7 +656,7 @@ def get_values(self, obj, key, path=[]):
"""
LOGGER.debug('Get the value for key %s in %s', key, obj)
matches = list()
matches = []

if not isinstance(obj, dict):
return None
Expand Down Expand Up @@ -774,7 +774,7 @@ def check_resource_property(self, resource_type, resource_property,
check_join=None, check_sub=None, **kwargs):
""" Check Resource Properties """
LOGGER.debug('Check property %s for %s', resource_property, resource_type)
matches = list()
matches = []
resources = self.get_resources(resource_type=resource_type)
for resource_name, resource_object in resources.items():
properties = resource_object.get('Properties', {})
Expand All @@ -800,7 +800,7 @@ def check_value(self, obj, key, path,
Check the value
"""
LOGGER.debug('Check value %s for %s', key, obj)
matches = list()
matches = []
values_obj = self.get_values(obj=obj, key=key)
new_path = path[:] + [key]
if not values_obj:
Expand Down Expand Up @@ -882,14 +882,14 @@ def transform(self):
def run(self):
"""Run rules"""
LOGGER.debug('Run scan of template...')
matches = list()
matches = []
if self.cfn.template is not None:
matches.extend(
self.rules.run(
self.filename, self.cfn))

# uniq the list of incidents
return_matches = list()
return_matches = []
for _, match in enumerate(matches):
if not any(match == u for u in return_matches):
return_matches.append(match)
Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def run_checks(filename, template, rules, regions):
LOGGER.error('Supported regions are %s', REGIONS)
exit(32)

matches = list()
matches = []

runner = cfnlint.Runner(rules, filename, template, regions)
matches.extend(runner.transform())
Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/rules/conditions/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Configuration(CloudFormationLintRule):
def match(self, cfn):
"""Check CloudFormation Conditions"""

matches = list()
matches = []

conditions = cfn.template.get('Conditions', {})
if conditions:
Expand Down
4 changes: 2 additions & 2 deletions src/cfnlint/rules/conditions/Used.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Used(CloudFormationLintRule):
def match(self, cfn):
"""Check CloudFormation Conditions"""

matches = list()
ref_conditions = list()
matches = []
ref_conditions = []

conditions = cfn.template.get('Conditions', {})
if conditions:
Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/rules/functions/Base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Base64(CloudFormationLintRule):
def match(self, cfn):
"""Check CloudFormation Base64"""

matches = list()
matches = []

base64_objs = cfn.search_deep_keys('Fn::Base64')
for base64_obj in base64_objs:
Expand Down
6 changes: 3 additions & 3 deletions src/cfnlint/rules/functions/Cidr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Cidr(CloudFormationLintRule):

def check_parameter_count(self, cfn, parameter_name):
"""Check Count Parameter if used"""
matches = list()
matches = []
parameter_obj = cfn.get_parameters().get(parameter_name, {})
if parameter_obj:
tree = ['Parameters', parameter_name]
Expand All @@ -56,7 +56,7 @@ def check_parameter_count(self, cfn, parameter_name):

def check_parameter_size_mask(self, cfn, parameter_name):
"""Check SizeMask Parameter if used"""
matches = list()
matches = []
parameter_obj = cfn.get_parameters().get(parameter_name, {})
if parameter_obj:
tree = ['Parameters', parameter_name]
Expand Down Expand Up @@ -84,7 +84,7 @@ def check_parameter_size_mask(self, cfn, parameter_name):
def match(self, cfn):
"""Check CloudFormation Cidr"""

matches = list()
matches = []

cidr_objs = cfn.search_deep_keys('Fn::Cidr')

Expand Down
12 changes: 6 additions & 6 deletions src/cfnlint/rules/functions/DynamicReferenceSecureString.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, ):

def check_dyn_ref_value(self, value, path):
"""Chec item type"""
matches = list()
matches = []

if isinstance(value, six.string_types):
if re.match(cfnlint.helpers.REGEX_DYN_REF_SSM_SECURE, value):
Expand All @@ -69,7 +69,7 @@ def check_dyn_ref_value(self, value, path):

def check_value(self, value, path, **kwargs):
"""Check Value"""
matches = list()
matches = []
item_type = kwargs.get('item_type', {})
if item_type in ['Map']:
if isinstance(value, dict):
Expand All @@ -85,7 +85,7 @@ def check_value(self, value, path, **kwargs):
# Need to disable for the function to work
def check_sub(self, value, path, **kwargs):
"""Check Sub Function Dynamic References"""
matches = list()
matches = []
if isinstance(value, list):
if isinstance(value[0], six.string_types):
matches.extend(self.check_dyn_ref_value(value[0], path[:] + [0]))
Expand All @@ -96,7 +96,7 @@ def check_sub(self, value, path, **kwargs):

def check(self, cfn, properties, specs, property_type, path):
"""Check itself"""
matches = list()
matches = []

for prop in properties:
if prop in specs:
Expand Down Expand Up @@ -125,7 +125,7 @@ def check(self, cfn, properties, specs, property_type, path):

def match_resource_sub_properties(self, properties, property_type, path, cfn):
"""Match for sub properties"""
matches = list()
matches = []

property_specs = self.property_specs.get(property_type, {}).get('Properties', {})
matches.extend(self.check(cfn, properties, property_specs, property_type, path))
Expand All @@ -134,7 +134,7 @@ def match_resource_sub_properties(self, properties, property_type, path, cfn):

def match_resource_properties(self, properties, resource_type, path, cfn):
"""Check CloudFormation Properties"""
matches = list()
matches = []
resource_specs = self.resource_specs.get(resource_type, {}).get('Properties', {})
matches.extend(self.check(cfn, properties, resource_specs, resource_type, path))

Expand Down
4 changes: 2 additions & 2 deletions src/cfnlint/rules/functions/FindInMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def check_dict(self, obj, tree):
Check that obj is a dict with Ref as the only key
Mappings only support Ref inside them
"""
matches = list()
matches = []

if isinstance(obj, dict):
if len(obj) == 1:
Expand All @@ -62,7 +62,7 @@ def check_dict(self, obj, tree):
def match(self, cfn):
"""Check CloudFormation GetAtt"""

matches = list()
matches = []

findinmaps = cfn.search_deep_keys('Fn::FindInMap')
mappings = cfn.get_mappings()
Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/rules/functions/GetAtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self):
def match(self, cfn):
"""Check CloudFormation GetAtt"""

matches = list()
matches = []

getatts = cfn.search_deep_keys('Fn::GetAtt')
valid_getatts = cfn.get_valid_getatts()
Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/rules/functions/GetAz.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GetAz(CloudFormationLintRule):
def match(self, cfn):
"""Check CloudFormation GetAz"""

matches = list()
matches = []

getaz_objs = cfn.search_deep_keys('Fn::GetAZs')

Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/rules/functions/If.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class If(CloudFormationLintRule):
def match(self, cfn):
"""Check CloudFormation Conditions"""

matches = list()
matches = []

# Build the list of functions
iftrees = cfn.search_deep_keys('Fn::If')
Expand Down
4 changes: 2 additions & 2 deletions src/cfnlint/rules/functions/IfExist.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class IfExist(CloudFormationLintRule):
def match(self, cfn):
"""Check CloudFormation Conditions"""

matches = list()
matches = []
if_conditions = {}

# Build the list of functions
Expand All @@ -43,7 +43,7 @@ def match(self, cfn):
if_condition = iftree[-1]
# Conditions can be referenced multiple times, check them once
if if_condition not in if_conditions:
if_conditions[if_condition] = list()
if_conditions[if_condition] = []

if_conditions[if_condition].append(iftree)

Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/rules/functions/ImportValue.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ImportValue(CloudFormationLintRule):
def match(self, cfn):
"""Check CloudFormation ImportValue"""

matches = list()
matches = []

iv_objs = cfn.search_deep_keys('Fn::ImportValue')

Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/rules/functions/Join.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Join(CloudFormationLintRule):
def match(self, cfn):
"""Check CloudFormation Join"""

matches = list()
matches = []

join_objs = cfn.search_deep_keys('Fn::Join')

Expand Down
Loading

0 comments on commit 830545a

Please sign in to comment.