From f222f13fcd44c5cb8fbfd39f0237cc6a146982e1 Mon Sep 17 00:00:00 2001 From: Adam Seitz Date: Mon, 6 Nov 2023 08:52:56 -0500 Subject: [PATCH] Declare path-like support in types --- python/gtirb/ir.py | 9 +++++++-- python/tests/test_ir.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/python/gtirb/ir.py b/python/gtirb/ir.py index 2e914f1e2..db8608003 100644 --- a/python/gtirb/ir.py +++ b/python/gtirb/ir.py @@ -10,6 +10,7 @@ """ import itertools +import os import typing from uuid import UUID @@ -171,7 +172,9 @@ def load_protobuf_file(protobuf_file: typing.BinaryIO) -> "IR": return IR._from_protobuf(ir, None) @staticmethod - def load_protobuf(file_name: str) -> "IR": + def load_protobuf( + file_name: typing.Union[str, "os.PathLike[str]"] + ) -> "IR": """Load IR from a Protobuf file at the specified path. :param file_name: The path to the Protobuf file. @@ -193,7 +196,9 @@ def save_protobuf_file(self, protobuf_file: typing.BinaryIO) -> None: protobuf_file.write(PROTOBUF_VERSION.to_bytes(1, byteorder="little")) protobuf_file.write(self._to_protobuf().SerializeToString()) - def save_protobuf(self, file_name: str) -> None: + def save_protobuf( + self, file_name: typing.Union[str, "os.PathLike[str]"] + ) -> None: """Save ``self`` to a Protobuf file at the specified path. :param file_name: The file path at which to diff --git a/python/tests/test_ir.py b/python/tests/test_ir.py index 7c02effa0..3ebe99335 100644 --- a/python/tests/test_ir.py +++ b/python/tests/test_ir.py @@ -1,5 +1,6 @@ import io import os +import pathlib import tempfile import unittest @@ -77,6 +78,19 @@ def test_ir_protobuf_load(self): new_ir.modules[0].aux_data["key"].data, ) + def test_load_pathlib(self): + """ + Ensure `load_protobuf` and `save_protobuf` support path-like objects + """ + ir_path = pathlib.Path(IR_FILE) + new_ir = gtirb.IR.load_protobuf(ir_path) + self.assertTrue(self.ir.deep_eq(new_ir)) + self.assertNotEqual( + self.ir.modules[0].aux_data["key"].data, + new_ir.modules[0].aux_data["key"].data, + ) + new_ir.save_protobuf(ir_path) + class NotGTIRBTest(unittest.TestCase): def test(self):