Skip to content

Commit 48750c1

Browse files
committed
Fix RUF015 unnecessary-iterable-allocation-for-first-element
1 parent 8429f83 commit 48750c1

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

newrelic/hooks/application_gearman.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _bind_params(submitted_connections, *args, **kwargs):
6666
if not submitted_connections:
6767
return wrapped(*args, **kwargs)
6868

69-
first_connection = list(submitted_connections)[0]
69+
first_connection = next(iter(submitted_connections))
7070

7171
url = f"gearman://{first_connection.gearman_host}:{first_connection.gearman_port}"
7272

newrelic/hooks/framework_graphql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def traverse_deepest_unique_path(fields, fragments):
196196

197197
# list(fragments.values())[0] 's index is OK because the previous line
198198
# ensures that there is only one field in the list
199-
full_fragment_selection_set = list(fragments.values())[0].selection_set.selections
199+
full_fragment_selection_set = next(iter(fragments.values())).selection_set.selections
200200
fragment_selection_set = filter_ignored_fields(full_fragment_selection_set)
201201

202202
if len(fragment_selection_set) != 1:

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ ignore = [
137137
"S", # flake8-bandit (security checks are not necessary in tests)
138138
"F401", # unused-import
139139
"F811", # redefined-while-unused (pytest fixtures trigger this)
140+
"RUF012", # mutable-class-default
141+
"RUF015", # unnecessary-iterable-allocation-for-first-element (more readable in tests)
140142
"E731", # lambda-assignment (acceptable in tests)
141143
"RUF012", # mutable-class-default
144+
"RUF015", # unnecessary-iterable-allocation-for-first-element (more readable in tests)
142145
"PLR2004", # magic-value-comparison (comparing to constant values)
143146
"ASYNC251", # blocking-sleep-in-async-function (acceptable in tests)
144147
"B904", # raise-without-from-inside-except (not necessary in tests)

tests/agent_features/test_log_events.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ def set_trace_ids():
5656
trace.guid = "abcdefgh"
5757

5858

59+
def active_session():
60+
txn = current_transaction()
61+
return list(txn.application._agent._applications.values())[0]._active_session
62+
63+
5964
def exercise_record_log_event():
6065
set_trace_ids()
6166

@@ -407,9 +412,7 @@ def test():
407412
@override_application_settings({"labels": TEST_LABELS, "application_logging.forwarding.labels.enabled": True})
408413
@background_task()
409414
def test_label_forwarding_enabled():
410-
txn = current_transaction()
411-
session = list(txn.application._agent._applications.values())[0]._active_session
412-
415+
session = active_session()
413416
common = session.get_log_events_common_block()
414417
# Excluded label should not appear, and other labels should be prefixed with 'tag.'
415418
assert common == {"tags.testlabel1": "A", "tags.testlabel2": "B", "tags.testlabelexclude": "C"}
@@ -424,9 +427,7 @@ def test_label_forwarding_enabled():
424427
)
425428
@background_task()
426429
def test_label_forwarding_enabled_exclude():
427-
txn = current_transaction()
428-
session = list(txn.application._agent._applications.values())[0]._active_session
429-
430+
session = active_session()
430431
common = session.get_log_events_common_block()
431432
# Excluded label should not appear, and other labels should be prefixed with 'tags.'
432433
assert common == {"tags.testlabel1": "A", "tags.testlabel2": "B"}
@@ -435,9 +436,7 @@ def test_label_forwarding_enabled_exclude():
435436
@override_application_settings({"labels": TEST_LABELS, "application_logging.forwarding.labels.enabled": False})
436437
@background_task()
437438
def test_label_forwarding_disabled():
438-
txn = current_transaction()
439-
session = list(txn.application._agent._applications.values())[0]._active_session
440-
439+
session = active_session()
441440
common = session.get_log_events_common_block()
442441
# No labels should appear
443442
assert common == {}
@@ -453,9 +452,7 @@ def test_label_forwarding_disabled():
453452
)
454453
@background_task()
455454
def test_global_custom_attribute_forwarding_enabled():
456-
txn = current_transaction()
457-
session = list(txn.application._agent._applications.values())[0]._active_session
458-
455+
session = active_session()
459456
common = session.get_log_events_common_block()
460457
# Both attrs should appear
461458
assert common == {"custom_attr_1": "value 1", "custom_attr_2": "value 2"}
@@ -464,9 +461,7 @@ def test_global_custom_attribute_forwarding_enabled():
464461
@override_application_settings({"application_logging.forwarding.custom_attributes": [("custom_attr_1", "a" * 256)]})
465462
@background_task()
466463
def test_global_custom_attribute_forwarding_truncation():
467-
txn = current_transaction()
468-
session = list(txn.application._agent._applications.values())[0]._active_session
469-
464+
session = active_session()
470465
common = session.get_log_events_common_block()
471466
# Attribute value should be truncated to the max user attribute length
472467
assert common == {"custom_attr_1": "a" * 255}
@@ -477,9 +472,7 @@ def test_global_custom_attribute_forwarding_truncation():
477472
)
478473
@background_task()
479474
def test_global_custom_attribute_forwarding_max_num_attrs():
480-
txn = current_transaction()
481-
session = list(txn.application._agent._applications.values())[0]._active_session
482-
475+
session = active_session()
483476
common = session.get_log_events_common_block()
484477
# Should be truncated to the max number of user attributes
485478
assert common == {f"custom_attr_{i + 1}": "value" for i in range(128)}

0 commit comments

Comments
 (0)