Skip to content

Commit 87fdbd0

Browse files
authored
Use literal syntax instead of function calls to create data structure (#9516)
It is slower to call e.g. dict() than using the empty literal, because the name dict must be looked up in the global scope in case it has been rebound. Same for the other two types like list() and tuple().
1 parent d914a9c commit 87fdbd0

File tree

22 files changed

+34
-33
lines changed

22 files changed

+34
-33
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ metastore_browser/templates/.*\\.html$|.*\\.jinja2"
145145
hooks:
146146
- id: check-merge-conflict
147147
- id: debug-statements
148+
- id: check-builtin-literals
148149
- id: detect-private-key
149150
- id: end-of-file-fixer
150151
- id: mixed-line-ending

airflow/executors/base_executor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ def get_event_buffer(self, dag_ids=None) -> Dict[TaskInstanceKeyType, EventBuffe
226226
:param dag_ids: to dag_ids to return events for, if None returns all
227227
:return: a dict of events
228228
"""
229-
cleared_events: Dict[TaskInstanceKeyType, EventBufferValueType] = dict()
229+
cleared_events: Dict[TaskInstanceKeyType, EventBufferValueType] = {}
230230
if dag_ids is None:
231231
cleared_events = self.event_buffer
232-
self.event_buffer = dict()
232+
self.event_buffer = {}
233233
else:
234234
for key in list(self.event_buffer.keys()):
235235
dag_id, _, _, _ = key

airflow/jobs/backfill_job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __init__(self, # pylint: disable=too-many-arguments
104104
total_runs=0,
105105
):
106106
self.to_run = to_run or OrderedDict()
107-
self.running = running or dict()
107+
self.running = running or {}
108108
self.skipped = skipped or set()
109109
self.succeeded = succeeded or set()
110110
self.failed = failed or set()

airflow/models/dag.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def __init__(
257257
# set file location to caller source path
258258
back = sys._getframe().f_back
259259
self.fileloc = back.f_code.co_filename if back else ""
260-
self.task_dict: Dict[str, BaseOperator] = dict()
260+
self.task_dict: Dict[str, BaseOperator] = {}
261261

262262
# set timezone from start_date
263263
if start_date and start_date.tzinfo:
@@ -1277,7 +1277,7 @@ def get_task(self, task_id, include_subdags=False):
12771277
raise TaskNotFound("Task {task_id} not found".format(task_id=task_id))
12781278

12791279
def pickle_info(self):
1280-
d = dict()
1280+
d = {}
12811281
d['is_picklable'] = True
12821282
try:
12831283
dttm = timezone.utcnow()

airflow/models/dagcode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def bulk_sync_to_db(cls, filelocs: Iterable[str], session=None):
9292
orm_dag_code.fileloc: orm_dag_code for orm_dag_code in existing_orm_dag_codes
9393
}
9494
else:
95-
existing_orm_dag_codes_map = dict()
95+
existing_orm_dag_codes_map = {}
9696

9797
existing_orm_dag_codes_by_fileloc_hashes = {
9898
orm.fileloc_hash: orm for orm in existing_orm_dag_codes

airflow/providers/amazon/aws/hooks/base_aws.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _get_credentials(self, region_name):
8989
aws_secret_access_key = None
9090
aws_session_token = None
9191
endpoint_url = None
92-
session_kwargs = dict()
92+
session_kwargs = {}
9393

9494
if self.aws_conn_id: # pylint: disable=too-many-nested-blocks
9595
self.log.info("Airflow Connection: aws_conn_id=%s",
@@ -187,7 +187,7 @@ def _get_credentials(self, region_name):
187187
)
188188
sts_client = sts_session.client("sts", config=self.config)
189189

190-
assume_role_kwargs = dict()
190+
assume_role_kwargs = {}
191191
if "assume_role_kwargs" in extra_config:
192192
assume_role_kwargs = extra_config["assume_role_kwargs"]
193193

airflow/providers/amazon/aws/hooks/sagemaker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ def list_training_jobs(
763763
:return: results of the list_training_jobs request
764764
"""
765765

766-
config = dict()
766+
config = {}
767767

768768
if name_contains:
769769
if "NameContains" in kwargs:
@@ -806,7 +806,7 @@ def _list_request(self, partial_func, result_key: str, max_results: Optional[int
806806
next_token = None
807807

808808
while True:
809-
kwargs = dict()
809+
kwargs = {}
810810
if next_token is not None:
811811
kwargs["NextToken"] = next_token
812812

airflow/providers/amazon/aws/operators/datasync.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,16 @@ def __init__(
137137
self.allow_random_task_choice = allow_random_task_choice
138138
self.allow_random_location_choice = allow_random_location_choice
139139

140-
self.create_task_kwargs = create_task_kwargs if create_task_kwargs else dict()
141-
self.create_source_location_kwargs = dict()
140+
self.create_task_kwargs = create_task_kwargs if create_task_kwargs else {}
141+
self.create_source_location_kwargs = {}
142142
if create_source_location_kwargs:
143143
self.create_source_location_kwargs = create_source_location_kwargs
144-
self.create_destination_location_kwargs = dict()
144+
self.create_destination_location_kwargs = {}
145145
if create_destination_location_kwargs:
146146
self.create_destination_location_kwargs = create_destination_location_kwargs
147147

148-
self.update_task_kwargs = update_task_kwargs if update_task_kwargs else dict()
149-
self.task_execution_kwargs = task_execution_kwargs if task_execution_kwargs else dict()
148+
self.update_task_kwargs = update_task_kwargs if update_task_kwargs else {}
149+
self.task_execution_kwargs = task_execution_kwargs if task_execution_kwargs else {}
150150
self.delete_task_after_execution = delete_task_after_execution
151151

152152
# Validations

airflow/providers/google/cloud/operators/bigquery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ def __init__(
10421042
self.google_cloud_storage_conn_id = google_cloud_storage_conn_id
10431043
self.delegate_to = delegate_to
10441044

1045-
self.src_fmt_configs = src_fmt_configs or dict()
1045+
self.src_fmt_configs = src_fmt_configs or {}
10461046
self.labels = labels
10471047
self.encryption_configuration = encryption_configuration
10481048
self.location = location

airflow/providers/google/cloud/operators/bigtable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def __init__(self,
255255
self.instance_id = instance_id
256256
self.table_id = table_id
257257
self.initial_split_keys = initial_split_keys or []
258-
self.column_families = column_families or dict()
258+
self.column_families = column_families or {}
259259
self._validate_inputs()
260260
self.gcp_conn_id = gcp_conn_id
261261
super().__init__(*args, **kwargs)

0 commit comments

Comments
 (0)