diff --git a/pyproject.toml b/pyproject.toml index cfabcac72d0..5f73609c6d4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -208,7 +208,6 @@ ignore = [ "T201", # 15 occurences [*] print "FBT001", # 12 occurences [ ] boolean-type-hint-positional-argument "SLF001", # 12 occurences [ ] private-member-access - "SIM118", # 12 occurences [*] in-dict-keys "UP035", # 12 occurences [ ] deprecated-import "S105", # 10 occurences [ ] hardcoded-password-string "B015", # 10 occurences [ ] useless-comparison @@ -226,32 +225,26 @@ ignore = [ "PT015", # 7 occurences [ ] pytest-assert-always-false "N815", # 7 occurences [ ] mixed-case-variable-in-class-scope "PT006", # 6 occurences [*] pytest-parametrize-names-wrong-type - "RET504", # 6 occurences [*] unnecessary-assign "N803", # 6 occurences [ ] invalid-argument-name - "E712", # 6 occurences [*] true-false-comparison "E741", # 6 occurences [ ] ambiguous-variable-name "S113", # 5 occurences [ ] request-without-timeout "PT011", # 5 occurences [ ] pytest-raises-too-broad "E731", # 5 occurences [*] lambda-assignment "RUF005", # 5 occurences [ ] collection-literal-concatenation - "B006", # 4 occurences [*] mutable-argument-default - "PIE794", # 4 occurences [*] duplicate-class-field-definition "PTH103", # 4 occurences [ ] os-makedirs "PLW2901", # 4 occurences [ ] redefined-loop-name "PTH112", # 3 occurences [ ] os-path-isdir - "RUF017", # 3 occurences [*] quadratic-list-summation "ANN002", # 2 occurences [ ] missing-type-args "ASYNC230", # 2 occurences [ ] blocking-open-call-in-async-function "S605", # 2 occurences [ ] start-process-with-a-shell - "C408", # 2 occurences [*] unnecessary-collection-call - "C416", # 2 occurences [*] unnecessary-comprehension - "PIE810", # 2 occurences [*] multiple-starts-ends-with "PTH100", # 2 occurences [ ] os-path-abspath "PTH109", # 2 occurences [ ] os-getcwd # keep those exceptions "C400", # unnecessary-generator-list: explicit list is more readable for non-python users "C401", # unnecessary-generator-set: explicit set is more readable for non-python users + "C408", # unnecessary-collection-call: explicit tuple is more readable for non-python users + "C416", # unnecessary-comprehension: may make the code less readable for non-python users "FIX003", # line-contains-xxx: freedom of speech! "FIX004", # line-contains-xxx: freedom of speech! "PLR1730", # if-stmt-min-max: not clear that it makes the code easier to read diff --git a/tests/apm_tracing_e2e/test_single_span.py b/tests/apm_tracing_e2e/test_single_span.py index 1732ac7cd2b..c19b6167110 100644 --- a/tests/apm_tracing_e2e/test_single_span.py +++ b/tests/apm_tracing_e2e/test_single_span.py @@ -68,7 +68,7 @@ def test_child_span_is_single_span(self): def _assert_single_span_event(event, name, is_root): assert event["operation_name"] == name - assert event["single_span"] == True + assert event["single_span"] is True assert event["ingestion_reason"] == "single_span" parent_id = event["parent_id"] if is_root: diff --git a/tests/appsec/iast/sink/test_hardcoded_passwords.py b/tests/appsec/iast/sink/test_hardcoded_passwords.py index 6f6c58359dd..669521027f2 100644 --- a/tests/appsec/iast/sink/test_hardcoded_passwords.py +++ b/tests/appsec/iast/sink/test_hardcoded_passwords.py @@ -45,9 +45,13 @@ def get_hardcoded_password_vulnerabilities(self): assert spans_meta, "No spans meta found" iast_events = [meta.get("_dd.iast.json") for meta in spans_meta if meta.get("_dd.iast.json")] assert iast_events, "No iast events found" - vulnerabilities = [event.get("vulnerabilities") for event in iast_events if event.get("vulnerabilities")] + + vulnerabilities: list = [] + for event in iast_events: + vulnerabilities.extend(event.get("vulnerabilities", [])) + assert vulnerabilities, "No vulnerabilities found" - vulnerabilities = sum(vulnerabilities, []) # set all the vulnerabilities in a single list + hardcoded_passwords = [vuln for vuln in vulnerabilities if vuln.get("type") == "HARDCODED_PASSWORD"] assert hardcoded_passwords, "No hardcoded passwords found" return hardcoded_passwords diff --git a/tests/appsec/iast/sink/test_hardcoded_secrets.py b/tests/appsec/iast/sink/test_hardcoded_secrets.py index 8312bc28ec6..97ff62a75e0 100644 --- a/tests/appsec/iast/sink/test_hardcoded_secrets.py +++ b/tests/appsec/iast/sink/test_hardcoded_secrets.py @@ -18,9 +18,13 @@ def get_hardcoded_secret_vulnerabilities(): assert spans_meta, "No spans meta found" iast_events = [meta.get("_dd.iast.json") for meta in spans_meta if meta.get("_dd.iast.json")] assert iast_events, "No iast events found" - vulnerabilities = [event.get("vulnerabilities") for event in iast_events if event.get("vulnerabilities")] + + vulnerabilities: list = [] + for event in iast_events: + vulnerabilities.extend(event.get("vulnerabilities", [])) + assert vulnerabilities, "No vulnerabilities found" - vulnerabilities = sum(vulnerabilities, []) # set all the vulnerabilities in a single list + hardcoded_secrets = [vuln for vuln in vulnerabilities if vuln.get("type") == "HARDCODED_SECRET"] assert hardcoded_secrets, "No hardcoded secrets found" return hardcoded_secrets diff --git a/tests/appsec/iast/utils.py b/tests/appsec/iast/utils.py index 23a94f5217d..81b530359bb 100644 --- a/tests/appsec/iast/utils.py +++ b/tests/appsec/iast/utils.py @@ -85,9 +85,13 @@ def get_all_iast_events(): def get_iast_sources(iast_events): - sources = [event.get("sources") for event in iast_events if event.get("sources")] + sources: list = [] + + for event in iast_events: + sources.extend(event.get("sources", [])) + assert sources, "No sources found" - sources = sum(sources, []) # set all the sources in a single list + return sources @@ -365,8 +369,7 @@ def check_test_telemetry_should_execute(self): def get_sources(self, request): iast = get_iast_event(request=request) - sources = iast["sources"] - return sources + return iast["sources"] def validate_request_reported(self, request, source_type=None): if source_type is None: # allow to overwrite source_type for parameter value node's use case diff --git a/tests/appsec/rasp/utils.py b/tests/appsec/rasp/utils.py index 28d9e12786b..8a29b88cb0d 100644 --- a/tests/appsec/rasp/utils.py +++ b/tests/appsec/rasp/utils.py @@ -7,7 +7,7 @@ from utils import interfaces -def validate_span_tags(request, expected_meta=[], expected_metrics=[]): +def validate_span_tags(request, expected_meta=(), expected_metrics=()): """Validate RASP span tags are added when an event is generated""" spans = [s for _, s in interfaces.library.get_root_spans(request=request)] assert spans, "No spans to validate" diff --git a/tests/auto_inject/test_blocklist_auto_inject.py b/tests/auto_inject/test_blocklist_auto_inject.py index 428281c4daf..66e22a427b0 100644 --- a/tests/auto_inject/test_blocklist_auto_inject.py +++ b/tests/auto_inject/test_blocklist_auto_inject.py @@ -131,5 +131,5 @@ def test_builtIn_instrument_args(self, virtual_machine): for command in self.buildIn_args_commands_injected[language]: local_log_file = self._execute_remote_command(ssh_client, command) assert ( - command_injection_skipped(command, local_log_file) == False + command_injection_skipped(command, local_log_file) is False ), f"The command {command} was not instrumented, but it should be instrumented!" diff --git a/tests/debugger/test_debugger_expression_language.py b/tests/debugger/test_debugger_expression_language.py index b26562b3227..abb81e6bf61 100644 --- a/tests/debugger/test_debugger_expression_language.py +++ b/tests/debugger/test_debugger_expression_language.py @@ -662,7 +662,7 @@ def _method_and_language_to_line_number(self, method, language): "Nulls": {"java": [130], "dotnet": [127], "python": [136]}, }.get(method, {}).get(language, []) - def _create_expression_probes(self, methodName, expressions, lines=[]): + def _create_expression_probes(self, methodName, expressions, lines=()): probes = [] expected_message_map = {} prob_types = ["method"] diff --git a/tests/debugger/utils.py b/tests/debugger/utils.py index b1f3280a3a2..0bd68c6d790 100644 --- a/tests/debugger/utils.py +++ b/tests/debugger/utils.py @@ -41,10 +41,9 @@ def extract_probe_ids(probes): return [] -def _get_path(test_name, suffix): +def _get_path(test_name, suffix) -> str: filename = test_name + "_" + _Base_Debugger_Test.tracer["language"] + "_" + suffix + ".json" - path = os.path.join(_CUR_DIR, "approvals", filename) - return path + return os.path.join(_CUR_DIR, "approvals", filename) def write_approval(data, test_name, suffix): diff --git a/tests/docker_ssi/test_docker_ssi.py b/tests/docker_ssi/test_docker_ssi.py index b1c846b0d88..0b0213368c9 100644 --- a/tests/docker_ssi/test_docker_ssi.py +++ b/tests/docker_ssi/test_docker_ssi.py @@ -115,7 +115,7 @@ def test_telemetry_abort(self): assert inject_result is not None, "No telemetry data found for inject.success, inject.skip or inject.error" # The injector detected by itself that the version is not supported - if inject_result == False: + if inject_result is False: return # There is telemetry data about the library entrypoint. We only validate there is data diff --git a/tests/fuzzer/corpus.py b/tests/fuzzer/corpus.py index 6259098cc2d..0e9f6e2c33b 100644 --- a/tests/fuzzer/corpus.py +++ b/tests/fuzzer/corpus.py @@ -163,7 +163,7 @@ def _load_dir(base_dirname): _load_dir(os.path.join(base_dirname, dirname)) for filename in filenames: - if filename.endswith(".json") or filename.endswith(".dump"): + if filename.endswith((".json", ".dump")): _load_file(os.path.join(base_dirname, filename)) if Path(source).is_file(): diff --git a/tests/integrations/utils.py b/tests/integrations/utils.py index eda8342cb43..61c34c25103 100644 --- a/tests/integrations/utils.py +++ b/tests/integrations/utils.py @@ -71,10 +71,6 @@ def _setup(self): setup_db_system = _setup setup_runtime_id = _setup setup_span_kind = _setup - setup_sql_traces = _setup - setup_resource = _setup - setup_db_type = _setup - setup_db_name = _setup setup_error_type_and_stack = _setup setup_error_message = _setup setup_obfuscate_query = _setup @@ -295,8 +291,7 @@ def get_bytes(s): def sha_hash(checkpoint_string): if isinstance(checkpoint_string, str): checkpoint_string = checkpoint_string.encode("utf-8") - hash_obj = hashlib.md5(checkpoint_string).digest()[:8] - return hash_obj + return hashlib.md5(checkpoint_string).digest()[:8] def compute_dsm_hash_nodejs(parent_hash, edge_tags): diff --git a/tests/parametric/conftest.py b/tests/parametric/conftest.py index 35b6d325c39..60b1c3bdce9 100644 --- a/tests/parametric/conftest.py +++ b/tests/parametric/conftest.py @@ -1,4 +1,5 @@ import base64 +from collections.abc import Iterable import contextlib import dataclasses import os @@ -438,7 +439,7 @@ def wait_for_rc_apply_state( time.sleep(0.01) raise AssertionError("No RemoteConfig apply status found, got requests %r" % rc_reqs) - def wait_for_rc_capabilities(self, capabilities: list[int] = [], wait_loops: int = 100): + def wait_for_rc_capabilities(self, capabilities: Iterable[int] = (), wait_loops: int = 100): """Wait for the given RemoteConfig apply state to be received by the test agent.""" rc_reqs = [] capabilities_seen = set() diff --git a/tests/parametric/test_config_consistency.py b/tests/parametric/test_config_consistency.py index 1e61287cb68..fb4f77f1726 100644 --- a/tests/parametric/test_config_consistency.py +++ b/tests/parametric/test_config_consistency.py @@ -311,7 +311,7 @@ def test_setting_trace_rate_limit(self, library_env, test_agent, test_library): @scenarios.parametric @features.tracing_configuration_consistency class Test_Config_Tags: - @parametrize("library_env", [{"DD_TAGS": key} for key in tag_scenarios.keys()]) + @parametrize("library_env", [{"DD_TAGS": key} for key in tag_scenarios]) def test_comma_space_tag_separation(self, library_env, test_agent, test_library): expected_local_tags = [] if "DD_TAGS" in library_env: diff --git a/tests/parametric/test_headers_baggage.py b/tests/parametric/test_headers_baggage.py index 1ccaa3649f1..9a75f7a1dbf 100644 --- a/tests/parametric/test_headers_baggage.py +++ b/tests/parametric/test_headers_baggage.py @@ -33,7 +33,7 @@ def test_headers_baggage_default_D001(self, test_agent, test_library): span = find_only_span(test_agent.wait_for_num_traces(1)) assert span.get("trace_id") == 123456789 assert span.get("parent_id") == 987654321 - assert "baggage" in headers.keys() + assert "baggage" in headers assert headers["baggage"] == "foo=bar" @only_baggage_enabled() @@ -44,9 +44,9 @@ def test_headers_baggage_only_D002(self, test_library): [["x-datadog-trace-id", "123456789"], ["baggage", "foo=bar"]] ) - assert "x-datadog-trace-id" not in headers.keys() - assert "x-datadog-parent-id" not in headers.keys() - assert "baggage" in headers.keys() + assert "x-datadog-trace-id" not in headers + assert "x-datadog-parent-id" not in headers + assert "baggage" in headers assert headers["baggage"] == "foo=bar" @disable_baggage() @@ -60,7 +60,7 @@ def test_baggage_disable_settings_D003(self, test_agent, test_library): span = find_only_span(test_agent.wait_for_num_traces(1)) assert span.get("trace_id") == 123456789 assert span.get("parent_id") == 987654321 - assert "baggage" not in headers.keys() + assert "baggage" not in headers def test_baggage_inject_header_D004(self, test_library): """Testing baggage header injection, proper concatenation of key value pairs, and encoding""" @@ -178,20 +178,20 @@ def test_baggage_malformed_headers_D012(self, test_library, test_agent): [["baggage", "no-equal-sign,foo=gets-dropped-because-previous-pair-is-malformed"]], ) - assert "baggage" not in headers.keys() + assert "baggage" not in headers def test_baggage_malformed_headers_D013(self, test_library): """Ensure that malformed baggage headers are handled properly. Unable to use get_baggage functions because it does not return anything""" with test_library: headers = test_library.dd_make_child_span_and_get_headers([["baggage", "=no-key"]]) - assert "baggage" not in headers.keys() + assert "baggage" not in headers def test_baggage_malformed_headers_D014(self, test_library): with test_library: headers = test_library.dd_make_child_span_and_get_headers([["baggage", "no-value="]]) - assert "baggage" not in headers.keys() + assert "baggage" not in headers def test_baggage_malformed_headers_D015(self, test_library): with test_library: @@ -199,7 +199,7 @@ def test_baggage_malformed_headers_D015(self, test_library): [["baggage", "foo=gets-dropped-because-subsequent-pair-is-malformed,="]], ) - assert "baggage" not in headers.keys() + assert "baggage" not in headers def test_baggageheader_maxitems_inject_D016(self, test_library): """Ensure that baggage headers are not injected when the number of baggage items exceeds the maximum number of items.""" diff --git a/tests/parametric/test_span_sampling.py b/tests/parametric/test_span_sampling.py index a5818cbd202..971b96d62d1 100644 --- a/tests/parametric/test_span_sampling.py +++ b/tests/parametric/test_span_sampling.py @@ -614,7 +614,7 @@ def test_root_span_selected_and_child_dropped_by_sss_when_dropping_policy_is_act We're essentially testing to make sure that the child unsampled span is dropped on the tracer side because of the activate dropping policy. """ - assert test_agent.info()["client_drop_p0s"] == True, "Client drop p0s expected to be enabled" + assert test_agent.info()["client_drop_p0s"] is True, "Client drop p0s expected to be enabled" with test_library: with test_library.dd_start_span(name="parent", service="webserver"): @@ -672,7 +672,7 @@ def test_child_span_selected_and_root_dropped_by_sss_when_dropping_policy_is_act We're essentially testing to make sure that the root unsampled span is dropped on the tracer side because of the activate dropping policy. """ - assert test_agent.info()["client_drop_p0s"] == True, "Client drop p0s expected to be enabled" + assert test_agent.info()["client_drop_p0s"] is True, "Client drop p0s expected to be enabled" with test_library: with test_library.dd_start_span(name="parent", service="webserver") as ps1: @@ -726,7 +726,7 @@ def test_entire_trace_dropped_when_dropping_policy_is_active018(self, test_agent We're essentially testing to make sure that the entire unsampled trace is dropped on the tracer side because of the activate dropping policy. """ - assert test_agent.info()["client_drop_p0s"] == True, "Client drop p0s expected to be enabled" + assert test_agent.info()["client_drop_p0s"] is True, "Client drop p0s expected to be enabled" with test_library: with test_library.dd_start_span(name="parent", service="webserver"): diff --git a/tests/parametric/test_telemetry.py b/tests/parametric/test_telemetry.py index fffb94821ca..c76ef341e49 100644 --- a/tests/parametric/test_telemetry.py +++ b/tests/parametric/test_telemetry.py @@ -579,8 +579,7 @@ def get_app_started_configuration_by_name(test_agent, test_library): configuration = body["payload"]["configuration"] - configuration_by_name = {item["name"]: item for item in configuration} - return configuration_by_name + return {item["name"]: item for item in configuration} return None @@ -628,4 +627,4 @@ def test_telemetry_sca_enabled_not_propagated(self, library_env, test_agent, tes assert cfg_appsec_enabled is not None, f"Missing telemetry config item for '{DD_APPSEC_SCA_ENABLED}'" assert cfg_appsec_enabled.get("value") is None else: - assert DD_APPSEC_SCA_ENABLED not in configuration_by_name.keys() + assert DD_APPSEC_SCA_ENABLED not in configuration_by_name diff --git a/tests/test_config_consistency.py b/tests/test_config_consistency.py index 8234ed0100a..fa21a0ec801 100644 --- a/tests/test_config_consistency.py +++ b/tests/test_config_consistency.py @@ -639,5 +639,4 @@ def parse_log_injection_message(log_message): except json.JSONDecodeError: continue if message.get("dd") and message.get(log_injection_fields[context.library.library]["message"]) == log_message: - dd = message.get("dd") - return dd + return message.get("dd") diff --git a/tests/test_telemetry.py b/tests/test_telemetry.py index 89a172d5a1f..3c034ba56c9 100644 --- a/tests/test_telemetry.py +++ b/tests/test_telemetry.py @@ -281,7 +281,7 @@ def save_data(data, container): ) if len(self.library_requests) != 0: - for s, r in self.library_requests.keys(): + for s, r in self.library_requests: logger.error(f"seq_id: {s}, runtime_id: {r}") raise Exception("The following telemetry messages were not forwarded by the agent") diff --git a/tests/test_the_test/test_conventions.py b/tests/test_the_test/test_conventions.py index 52f4ab0558e..2c53a77c5dd 100644 --- a/tests/test_the_test/test_conventions.py +++ b/tests/test_the_test/test_conventions.py @@ -6,7 +6,7 @@ def test_utils(): # verify that all files in test folder are either a test file, a utils.py file or a conftest.py file for folder, _, files in os.walk("tests"): - if folder.startswith("tests/fuzzer") or folder.startswith("tests/perfs"): + if folder.startswith(("tests/fuzzer", "tests/perfs")): # do not check these folders, they are particular use cases continue