Skip to content

Commit 84ff4bd

Browse files
committed
style: fix error [SIM118] - Use key in dict instead of key in dict.keys().
Used Ruff (vscode and pre-commit) to: - Black-compatible code formatting. - fix all auto-fixable violations. - isort-compatible import sorting. - flake8-simplify manual fixes. Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
1 parent 81134bd commit 84ff4bd

8 files changed

Lines changed: 77 additions & 78 deletions

File tree

src/instana/agent/host.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,7 @@ def can_send(self) -> bool:
123123
logger.debug("Fork detected; Handling like a pro...")
124124
self.handle_fork()
125125

126-
if self.machine.fsm.current in ["wait4init", "good2go"]:
127-
return True
128-
129-
return False
126+
return self.machine.fsm.current in ["wait4init", "good2go"]
130127

131128
def set_from(
132129
self,
@@ -371,7 +368,7 @@ def filter_spans(self, spans: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
371368
service_name = ""
372369

373370
# Set the service name
374-
for span_value in span.data.keys():
371+
for span_value in span.data:
375372
if isinstance(span.data[span_value], dict):
376373
service_name = span_value
377374

@@ -413,13 +410,12 @@ def __is_endpoint_ignored(self, span_attributes: dict) -> bool:
413410

414411
# Check exclude rules
415412
exclude_rules = filters.get("exclude", [])
416-
if any(
417-
matches_rule(rule.get("attributes", []), span_attributes)
418-
for rule in exclude_rules
419-
):
420-
return True
421-
422-
return False
413+
return bool(
414+
any(
415+
matches_rule(rule.get("attributes", []), span_attributes)
416+
for rule in exclude_rules
417+
)
418+
)
423419

424420
def handle_agent_tasks(self, task: Dict[str, Any]) -> None:
425421
"""

src/instana/instrumentation/aws/dynamodb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def create_dynamodb_span(
2323
try:
2424
span.set_attribute("dynamodb.op", args[0])
2525
span.set_attribute("dynamodb.region", instance._client_config.region_name)
26-
if "TableName" in args[1].keys():
26+
if "TableName" in args[1]:
2727
span.set_attribute("dynamodb.table", args[1]["TableName"])
2828
except Exception as exc:
2929
span.record_exception(exc)

src/instana/instrumentation/aws/s3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def create_s3_span(
3535
with tracer.start_as_current_span("s3", context=parent_context) as span:
3636
try:
3737
span.set_attribute("s3.op", args[0])
38-
if "Bucket" in args[1].keys():
38+
if "Bucket" in args[1]:
3939
span.set_attribute("s3.bucket", args[1]["Bucket"])
4040
except Exception as exc:
4141
span.record_exception(exc)

src/instana/util/config.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# (c) Copyright IBM Corp. 2025
22

33
import os
4-
from typing import Any, Dict, List, Sequence, Tuple, Union, Optional
4+
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
55

66
from instana.configurator import config
77
from instana.log import logger
@@ -317,7 +317,7 @@ def parse_span_disabling_str(item: str) -> List[str]:
317317
@param item: String span disabling configuration
318318
@return: List of disabled spans
319319
"""
320-
if item.lower() in SPAN_CATEGORIES or item.lower() in SPAN_TYPE_TO_CATEGORY.keys():
320+
if item.lower() in SPAN_CATEGORIES or item.lower() in SPAN_TYPE_TO_CATEGORY:
321321
return [item.lower()]
322322
else:
323323
logger.debug(f"set_span_disabling_str: Invalid span category/type: {item}")
@@ -335,7 +335,7 @@ def parse_span_disabling_dict(items: Dict[str, bool]) -> Tuple[List[str], List[s
335335
enabled_spans = []
336336

337337
for key, value in items.items():
338-
if key in SPAN_CATEGORIES or key in SPAN_TYPE_TO_CATEGORY.keys():
338+
if key in SPAN_CATEGORIES or key in SPAN_TYPE_TO_CATEGORY:
339339
if is_truthy(value):
340340
disabled_spans.append(key)
341341
else:
@@ -394,9 +394,10 @@ def get_disable_trace_configurations_from_yaml() -> Tuple[List[str], List[str]]:
394394

395395

396396
def get_disable_trace_configurations_from_local() -> Tuple[List[str], List[str]]:
397-
if "tracing" in config:
398-
if tracing_disable_config := config["tracing"].get("disable", None):
399-
return parse_span_disabling(tracing_disable_config)
397+
if "tracing" in config and (
398+
tracing_disable_config := config["tracing"].get("disable", None)
399+
):
400+
return parse_span_disabling(tracing_disable_config)
400401
return [], []
401402

402403

@@ -472,15 +473,15 @@ def parse_technology_stack_trace_config(
472473
tech_stack_config = {}
473474
context = f"for {tech_name}" if tech_name else ""
474475

475-
if level_key in tech_data:
476-
if validated_level := validate_stack_trace_level(tech_data[level_key], context):
477-
tech_stack_config["level"] = validated_level
476+
if level_key in tech_data and (
477+
validated_level := validate_stack_trace_level(tech_data[level_key], context)
478+
):
479+
tech_stack_config["level"] = validated_level
478480

479-
if length_key in tech_data:
480-
if validated_length := validate_stack_trace_length(
481-
tech_data[length_key], context
482-
):
483-
tech_stack_config["length"] = validated_length
481+
if length_key in tech_data and (
482+
validated_length := validate_stack_trace_length(tech_data[length_key], context)
483+
):
484+
tech_stack_config["length"] = validated_length
484485

485486
return tech_stack_config
486487

@@ -498,17 +499,19 @@ def parse_global_stack_trace_config(global_config: Dict[str, Any]) -> Tuple[str,
498499
level = "all"
499500
length = 30
500501

501-
if "stack-trace" in global_config:
502-
if validated_level := validate_stack_trace_level(
502+
if "stack-trace" in global_config and (
503+
validated_level := validate_stack_trace_level(
503504
global_config["stack-trace"], "in YAML config"
504-
):
505-
level = validated_level
505+
)
506+
):
507+
level = validated_level
506508

507-
if "stack-trace-length" in global_config:
508-
if validated_length := validate_stack_trace_length(
509+
if "stack-trace-length" in global_config and (
510+
validated_length := validate_stack_trace_length(
509511
global_config["stack-trace-length"], "in YAML config"
510-
):
511-
length = validated_length
512+
)
513+
):
514+
length = validated_length
512515

513516
return level, length
514517

@@ -544,9 +547,9 @@ def parse_tech_specific_stack_trace_configs(
544547
return tech_config
545548

546549

547-
def get_stack_trace_config_from_yaml() -> (
548-
Tuple[str, int, Dict[str, Dict[str, Union[str, int]]]]
549-
):
550+
def get_stack_trace_config_from_yaml() -> Tuple[
551+
str, int, Dict[str, Dict[str, Union[str, int]]]
552+
]:
550553
"""
551554
Get stack trace configuration from YAML file specified by INSTANA_CONFIG_PATH.
552555

tests/clients/test_redis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _resource(self) -> Generator[None, None, None]:
2626
self.client = redis.Redis(host=testenv["redis_host"], db=testenv["redis_db"])
2727
yield
2828
keys_to_remove = [
29-
k for k in os.environ.keys() if k.startswith("INSTANA_TRACING_FILTER_")
29+
k for k in os.environ if k.startswith("INSTANA_TRACING_FILTER_")
3030
]
3131
for k in keys_to_remove:
3232
del os.environ[k]

tests/propagators/test_http_propagator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,15 @@ def test_w3c_off_x_instana_l_0(
318318
assert not span_ctx.correlation_id
319319

320320
# Assert that the traceparent is propagated when it is enabled
321-
if "traceparent" in carrier_header.keys():
321+
if "traceparent" in carrier_header:
322322
assert span_ctx.traceparent
323323
tp_trace_id = header_to_long_id(carrier_header["traceparent"].split("-")[1])
324324
else:
325325
assert not span_ctx.traceparent
326326
tp_trace_id = span_ctx.trace_id
327327

328328
# Assert that the tracestate is propagated when it is enabled
329-
if "tracestate" in carrier_header.keys():
329+
if "tracestate" in carrier_header:
330330
assert span_ctx.tracestate
331331
else:
332332
assert not span_ctx.tracestate
@@ -352,7 +352,7 @@ def test_w3c_off_x_instana_l_0(
352352
)
353353

354354
# Assert that the tracestate is propagated when it is enabled
355-
if "tracestate" in carrier_header.keys():
355+
if "tracestate" in carrier_header:
356356
assert "tracestate" in downstream_carrier
357357
assert carrier_header["tracestate"] == downstream_carrier["tracestate"]
358358

0 commit comments

Comments
 (0)