From 2a3d3fa0fd04a0434273e2a4f3729fc40758a5cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= <16805946+edgarrmondragon@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:18:16 -0600 Subject: [PATCH] refactor: Use context manager to read gzip batch files (#2628) * refactor: Use context manager to read gzip batch files * Remove redundant variable --- singer_sdk/sinks/core.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/singer_sdk/sinks/core.py b/singer_sdk/sinks/core.py index 5a936a634..10c278ccb 100644 --- a/singer_sdk/sinks/core.py +++ b/singer_sdk/sinks/core.py @@ -611,7 +611,7 @@ def _after_process_record(self, context: dict) -> None: # SDK developer overrides: - def preprocess_record(self, record: dict, context: dict) -> dict: # noqa: ARG002, PLR6301 + def preprocess_record(self, record: dict, context: dict) -> dict: # noqa: PLR6301, ARG002 """Process incoming record and return a modified result. Args: @@ -743,12 +743,15 @@ def process_batch_files( tail, mode="rb", ) as file: - context_file = ( - gzip_open(file) if encoding.compression == "gzip" else file - ) - context = { - "records": [deserialize_json(line) for line in context_file] # type: ignore[attr-defined] - } + if encoding.compression == "gzip": + with gzip_open(file) as context_file: + context = { + "records": [ + deserialize_json(line) for line in context_file + ] + } + else: + context = {"records": [deserialize_json(line) for line in file]} self.process_batch(context) elif ( importlib.util.find_spec("pyarrow") @@ -760,8 +763,7 @@ def process_batch_files( tail, mode="rb", ) as file: - context_file = file - table = pq.read_table(context_file) + table = pq.read_table(file) context = {"records": table.to_pylist()} self.process_batch(context) else: