From bfcdc6e27d2b5206cdc8b9d628d92d363263ac22 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Tue, 13 Feb 2024 08:19:11 -0300 Subject: [PATCH] change requests timeout codemod semgrep pattern --- src/core_codemods/add_requests_timeouts.py | 20 ++++++++++---------- tests/codemods/test_add_requests_timeouts.py | 12 ++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/core_codemods/add_requests_timeouts.py b/src/core_codemods/add_requests_timeouts.py index cadbf349..bebaa34a 100644 --- a/src/core_codemods/add_requests_timeouts.py +++ b/src/core_codemods/add_requests_timeouts.py @@ -40,20 +40,20 @@ def on_result_found(self, original_node, updated_node): - pattern-inside: | import requests ... - - pattern: $CALL(...) - - pattern-not: $CALL(..., timeout=$TIMEOUT, ...) + - pattern: requests.$CALL(...) + - pattern-not: requests.$CALL(..., timeout=$TIMEOUT, ...) - metavariable-pattern: metavariable: $CALL patterns: - pattern-either: - - pattern: requests.get - - pattern: requests.post - - pattern: requests.put - - pattern: requests.delete - - pattern: requests.head - - pattern: requests.options - - pattern: requests.patch - - pattern: requests.request + - pattern: get + - pattern: post + - pattern: put + - pattern: delete + - pattern: head + - pattern: options + - pattern: patch + - pattern: request """ ), transformer=LibcstTransformerPipeline(TransformAddRequestsTimeouts), diff --git a/tests/codemods/test_add_requests_timeouts.py b/tests/codemods/test_add_requests_timeouts.py index 9382de8c..ddc546e0 100644 --- a/tests/codemods/test_add_requests_timeouts.py +++ b/tests/codemods/test_add_requests_timeouts.py @@ -84,3 +84,15 @@ def test_not_requests(self, tmpdir): demands.get("https://example.com") """ self.run_and_assert(tmpdir, original, original) + + def test_chaining(self, tmpdir): + # See issue https://github.com/pixee/codemodder-python/issues/244 + original = """ + import requests + requests.get("https://example.com").json() + """ + expected = f""" + import requests + requests.get("https://example.com", timeout={TIMEOUT}).json() + """ + self.run_and_assert(tmpdir, original, expected)