Skip to content

Commit

Permalink
Declare path-like support in types
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjseitz committed Nov 10, 2023
1 parent bd27eae commit f222f13
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions python/gtirb/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""

import itertools
import os
import typing
from uuid import UUID

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions python/tests/test_ir.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import os
import pathlib
import tempfile
import unittest

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f222f13

Please sign in to comment.