Skip to content

Commit 830545a

Browse files
committed
Use list literal instead of list()
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.
1 parent d876531 commit 830545a

File tree

103 files changed

+223
-223
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+223
-223
lines changed

docs/getting_started/rules.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class MyNewRule(CloudFormationLintRule):
7171
def match(self, cfn):
7272
"""Basic Rule Matching"""
7373

74-
matches = list()
74+
matches = []
7575

7676
# Your Rule code goes here
7777

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

214214
# Check max length
@@ -219,7 +219,7 @@ def check_value(self, value, path):
219219
def match(self, cfn):
220220
"""Check SecurityGroup descriptions"""
221221

222-
matches = list()
222+
matches = []
223223

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

examples/rules/PropertiesTagsIncluded.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def get_resources_with_tags(self, region):
3131
resourcespecs = cfnlint.helpers.RESOURCE_SPECS[region]
3232
resourcetypes = resourcespecs['ResourceTypes']
3333

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

46-
matches = list()
46+
matches = []
4747

4848
all_tags = cfn.search_deep_keys('Tags')
4949
all_tags = [x for x in all_tags if x[0] == 'Resources']

examples/rules/PropertiesTagsRequired.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class PropertiesTagsRequired(CloudFormationLintRule):
2828
def match(self, cfn):
2929
"""Check Tags for required keys"""
3030

31-
matches = list()
31+
matches = []
3232

3333
required_tags = ['CostCenter', 'ApplicationName']
3434

src/cfnlint/__init__.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def is_rule_enabled(self, rule_id):
164164

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

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

260260
def run(self, filename, cfn):
261261
"""Run rules"""
262-
matches = list()
262+
matches = []
263263
for rule in self.rules:
264264
try:
265265
matches.extend(rule.matchall(filename, cfn))
@@ -423,7 +423,7 @@ def get_mappings(self):
423423
def get_resource_names(self):
424424
"""Get all the Resource Names"""
425425
LOGGER.debug('Get the names of all resources from template...')
426-
results = list()
426+
results = []
427427
resources = self.template.get('Resources', {})
428428
if isinstance(resources, dict):
429429
for resourcename, _ in resources.items():
@@ -434,7 +434,7 @@ def get_resource_names(self):
434434
def get_parameter_names(self):
435435
"""Get all Parameter Names"""
436436
LOGGER.debug('Get names of all parameters from template...')
437-
results = list()
437+
results = []
438438
parameters = self.template.get('Parameters', {})
439439
if isinstance(parameters, dict):
440440
for parametername, _ in parameters.items():
@@ -524,15 +524,15 @@ def _get_sub_resource_properties(self, keys, properties, path):
524524
if results:
525525
return results
526526
elif isinstance(properties, list):
527-
matches = list()
527+
matches = []
528528
for index, item in enumerate(properties):
529529
results = None
530530
if isinstance(item, dict):
531531
if len(item) == 1:
532532
for sub_key, sub_value in item.items():
533533
if sub_key in cfnlint.helpers.CONDITION_FUNCTIONS:
534534
cond_values = self.get_condition_values(sub_value)
535-
results = list()
535+
results = []
536536
for cond_value in cond_values:
537537
result_path = path[:] + [index, sub_key] + cond_value['Path']
538538
results.extend(
@@ -551,12 +551,12 @@ def _get_sub_resource_properties(self, keys, properties, path):
551551
matches.extend(results)
552552
return matches
553553

554-
return list()
554+
return []
555555

556556
def get_resource_properties(self, keys):
557557
"""Filter keys of template"""
558558
LOGGER.debug('Get Properties from a resource: %s', keys)
559-
matches = list()
559+
matches = []
560560
resourcetype = keys.pop(0)
561561
for resource_name, resource_value in self.get_resources(resourcetype).items():
562562
path = ['Resources', resource_name, 'Properties']
@@ -570,7 +570,7 @@ def get_resource_properties(self, keys):
570570
# pylint: disable=dangerous-default-value
571571
def _search_deep_keys(self, searchText, cfndict, path):
572572
"""Search deep for keys and get their values"""
573-
keys = list()
573+
keys = []
574574
if isinstance(cfndict, dict):
575575
for key in cfndict:
576576
pathprop = path[:]
@@ -606,7 +606,7 @@ def search_deep_keys(self, searchText):
606606
def get_condition_values(self, template, path=[]):
607607
"""Evaluates conditions and brings back the values"""
608608
LOGGER.debug('Get condition values...')
609-
matches = list()
609+
matches = []
610610
if not isinstance(template, list):
611611
return matches
612612
if not len(template) == 3:
@@ -656,7 +656,7 @@ def get_values(self, obj, key, path=[]):
656656
657657
"""
658658
LOGGER.debug('Get the value for key %s in %s', key, obj)
659-
matches = list()
659+
matches = []
660660

661661
if not isinstance(obj, dict):
662662
return None
@@ -774,7 +774,7 @@ def check_resource_property(self, resource_type, resource_property,
774774
check_join=None, check_sub=None, **kwargs):
775775
""" Check Resource Properties """
776776
LOGGER.debug('Check property %s for %s', resource_property, resource_type)
777-
matches = list()
777+
matches = []
778778
resources = self.get_resources(resource_type=resource_type)
779779
for resource_name, resource_object in resources.items():
780780
properties = resource_object.get('Properties', {})
@@ -800,7 +800,7 @@ def check_value(self, obj, key, path,
800800
Check the value
801801
"""
802802
LOGGER.debug('Check value %s for %s', key, obj)
803-
matches = list()
803+
matches = []
804804
values_obj = self.get_values(obj=obj, key=key)
805805
new_path = path[:] + [key]
806806
if not values_obj:
@@ -882,14 +882,14 @@ def transform(self):
882882
def run(self):
883883
"""Run rules"""
884884
LOGGER.debug('Run scan of template...')
885-
matches = list()
885+
matches = []
886886
if self.cfn.template is not None:
887887
matches.extend(
888888
self.rules.run(
889889
self.filename, self.cfn))
890890

891891
# uniq the list of incidents
892-
return_matches = list()
892+
return_matches = []
893893
for _, match in enumerate(matches):
894894
if not any(match == u for u in return_matches):
895895
return_matches.append(match)

src/cfnlint/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def run_checks(filename, template, rules, regions):
286286
LOGGER.error('Supported regions are %s', REGIONS)
287287
exit(32)
288288

289-
matches = list()
289+
matches = []
290290

291291
runner = cfnlint.Runner(rules, filename, template, regions)
292292
matches.extend(runner.transform())

src/cfnlint/rules/conditions/Configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Configuration(CloudFormationLintRule):
3737
def match(self, cfn):
3838
"""Check CloudFormation Conditions"""
3939

40-
matches = list()
40+
matches = []
4141

4242
conditions = cfn.template.get('Conditions', {})
4343
if conditions:

src/cfnlint/rules/conditions/Used.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Used(CloudFormationLintRule):
3030
def match(self, cfn):
3131
"""Check CloudFormation Conditions"""
3232

33-
matches = list()
34-
ref_conditions = list()
33+
matches = []
34+
ref_conditions = []
3535

3636
conditions = cfn.template.get('Conditions', {})
3737
if conditions:

src/cfnlint/rules/functions/Base64.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Base64(CloudFormationLintRule):
3030
def match(self, cfn):
3131
"""Check CloudFormation Base64"""
3232

33-
matches = list()
33+
matches = []
3434

3535
base64_objs = cfn.search_deep_keys('Fn::Base64')
3636
for base64_obj in base64_objs:

src/cfnlint/rules/functions/Cidr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Cidr(CloudFormationLintRule):
3131

3232
def check_parameter_count(self, cfn, parameter_name):
3333
"""Check Count Parameter if used"""
34-
matches = list()
34+
matches = []
3535
parameter_obj = cfn.get_parameters().get(parameter_name, {})
3636
if parameter_obj:
3737
tree = ['Parameters', parameter_name]
@@ -56,7 +56,7 @@ def check_parameter_count(self, cfn, parameter_name):
5656

5757
def check_parameter_size_mask(self, cfn, parameter_name):
5858
"""Check SizeMask Parameter if used"""
59-
matches = list()
59+
matches = []
6060
parameter_obj = cfn.get_parameters().get(parameter_name, {})
6161
if parameter_obj:
6262
tree = ['Parameters', parameter_name]
@@ -84,7 +84,7 @@ def check_parameter_size_mask(self, cfn, parameter_name):
8484
def match(self, cfn):
8585
"""Check CloudFormation Cidr"""
8686

87-
matches = list()
87+
matches = []
8888

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

src/cfnlint/rules/functions/DynamicReferenceSecureString.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, ):
5757

5858
def check_dyn_ref_value(self, value, path):
5959
"""Chec item type"""
60-
matches = list()
60+
matches = []
6161

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

7070
def check_value(self, value, path, **kwargs):
7171
"""Check Value"""
72-
matches = list()
72+
matches = []
7373
item_type = kwargs.get('item_type', {})
7474
if item_type in ['Map']:
7575
if isinstance(value, dict):
@@ -85,7 +85,7 @@ def check_value(self, value, path, **kwargs):
8585
# Need to disable for the function to work
8686
def check_sub(self, value, path, **kwargs):
8787
"""Check Sub Function Dynamic References"""
88-
matches = list()
88+
matches = []
8989
if isinstance(value, list):
9090
if isinstance(value[0], six.string_types):
9191
matches.extend(self.check_dyn_ref_value(value[0], path[:] + [0]))
@@ -96,7 +96,7 @@ def check_sub(self, value, path, **kwargs):
9696

9797
def check(self, cfn, properties, specs, property_type, path):
9898
"""Check itself"""
99-
matches = list()
99+
matches = []
100100

101101
for prop in properties:
102102
if prop in specs:
@@ -125,7 +125,7 @@ def check(self, cfn, properties, specs, property_type, path):
125125

126126
def match_resource_sub_properties(self, properties, property_type, path, cfn):
127127
"""Match for sub properties"""
128-
matches = list()
128+
matches = []
129129

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

135135
def match_resource_properties(self, properties, resource_type, path, cfn):
136136
"""Check CloudFormation Properties"""
137-
matches = list()
137+
matches = []
138138
resource_specs = self.resource_specs.get(resource_type, {}).get('Properties', {})
139139
matches.extend(self.check(cfn, properties, resource_specs, resource_type, path))
140140

0 commit comments

Comments
 (0)