Skip to content

Commit 2a52478

Browse files
authored
fix: properly close files in lineage queries and tests (#4587)
Closes #4458
1 parent ed390dd commit 2a52478

File tree

7 files changed

+24
-14
lines changed

7 files changed

+24
-14
lines changed

src/sagemaker/lineage/query.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ def _get_legend_line(self, component_name):
335335

336336
def _add_legend(self, path):
337337
"""Embed legend to html file generated by pyvis."""
338-
f = open(path, "r")
339-
content = self.BeautifulSoup(f, "html.parser")
338+
with open(path, "r") as f:
339+
content = self.BeautifulSoup(f, "html.parser")
340340

341341
legend = """
342342
<div style="display: inline-block; font-size: 1vw; font-family: verdana;

tests/data/sip/training.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def main():
7373
)
7474

7575
model_dir = os.environ.get("SM_MODEL_DIR")
76-
pkl.dump(bst, open(model_dir + "/model.bin", "wb"))
76+
with open(model_dir + "/model.bin", "wb") as f:
77+
pkl.dump(bst, f)
7778

7879

7980
if __name__ == "__main__":

tests/integ/sagemaker/lineage/test_lineage_visualize.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ def test_graph_visualize(sagemaker_session, extract_data_from_html):
142142
lq_result.visualize(path="testGraph.html")
143143

144144
# check generated graph info
145-
fo = open("testGraph.html", "r")
146-
lines = fo.readlines()
145+
with open("testGraph.html", "r") as fo:
146+
lines = fo.readlines()
147147
for line in lines:
148148
if "nodes = " in line:
149149
node = line

tests/integ/sagemaker/workflow/test_workflow.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ def test_one_step_ingestion_pipeline(
527527

528528
temp_flow_path = "./ingestion.flow"
529529
with cleanup_feature_group(feature_group):
530-
json.dump(ingestion_only_flow, open(temp_flow_path, "w"))
530+
with open(temp_flow_path, "w") as f:
531+
json.dump(ingestion_only_flow, f)
531532

532533
data_wrangler_processor = DataWranglerProcessor(
533534
role=role,

tests/integ/test_sagemaker_config.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ def expected_merged_config():
6666
expected_merged_config_file_path = os.path.join(
6767
CONFIG_DATA_DIR, "expected_output_config_after_merge.yaml"
6868
)
69-
return yaml.safe_load(open(expected_merged_config_file_path, "r").read())
69+
with open(expected_merged_config_file_path, "r") as f:
70+
return yaml.safe_load(f.read())
7071

7172

7273
@pytest.fixture(scope="module")
@@ -171,7 +172,8 @@ def test_config_download_from_s3_and_merge(
171172
CONFIG_DATA_DIR, "sample_additional_config_for_merge.yaml"
172173
)
173174

174-
config_file_1_as_yaml = open(config_file_1_local_path, "r").read()
175+
with open(config_file_1_local_path, "r") as f:
176+
config_file_1_as_yaml = f.read()
175177
s3_uri_config_1 = os.path.join(s3_uri_prefix, "config_1.yaml")
176178

177179
# Upload S3 files in case they dont already exist

tests/unit/sagemaker/local/test_local_image.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,12 @@ def test_write_config_file(LocalSession, tmpdir):
221221
assert os.path.exists(resource_config_file)
222222
assert os.path.exists(input_data_config_file)
223223

224-
hyperparameters_data = json.load(open(hyperparameters_file))
225-
resource_config_data = json.load(open(resource_config_file))
226-
input_data_config_data = json.load(open(input_data_config_file))
224+
with open(hyperparameters_file) as f:
225+
hyperparameters_data = json.load(f)
226+
with open(resource_config_file) as f:
227+
resource_config_data = json.load(f)
228+
with open(input_data_config_file) as f:
229+
input_data_config_data = json.load(f)
227230

228231
# Validate HyperParameters
229232
for k, v in HYPERPARAMETERS.items():
@@ -280,7 +283,8 @@ def test_write_config_files_input_content_type(LocalSession, tmpdir):
280283
sagemaker_container.write_config_files(host, HYPERPARAMETERS, input_data_config)
281284

282285
assert os.path.exists(input_data_config_file)
283-
parsed_input_config = json.load(open(input_data_config_file))
286+
with open(input_data_config_file) as f:
287+
parsed_input_config = json.load(f)
284288
# Validate Input Data Config
285289
for channel in input_data_config:
286290
assert channel["ChannelName"] in parsed_input_config

tests/unit/sagemaker/serializers/test_serializers.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,15 @@ def test_data_serializer_raw(data_serializer):
345345
input_image = image.read()
346346
input_image_data = data_serializer.serialize(input_image)
347347
validation_image_file_path = os.path.join(DATA_DIR, "", "cuteCat.raw")
348-
validation_image_data = open(validation_image_file_path, "rb").read()
348+
with open(validation_image_file_path, "rb") as f:
349+
validation_image_data = f.read()
349350
assert input_image_data == validation_image_data
350351

351352

352353
def test_data_serializer_file_like(data_serializer):
353354
input_image_file_path = os.path.join(DATA_DIR, "", "cuteCat.jpg")
354355
validation_image_file_path = os.path.join(DATA_DIR, "", "cuteCat.raw")
355356
input_image_data = data_serializer.serialize(input_image_file_path)
356-
validation_image_data = open(validation_image_file_path, "rb").read()
357+
with open(validation_image_file_path, "rb") as f:
358+
validation_image_data = f.read()
357359
assert input_image_data == validation_image_data

0 commit comments

Comments
 (0)