Skip to content

Commit f017f4e

Browse files
committed
Ensure that BinaryFileInput is an IOBase
- Rename the open handle property to an `open()` method, to ensure that it has an `open()` method -- though not part of the target interface, this is a common expectation around handling binary streams - Use ABC `register()` to make sure that the class passes isinstance() checks even though it does not subclass/inherit
1 parent bb95d07 commit f017f4e

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

‎src/check_jsonschema/file_wrapper.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- `close()` does nothing on stdin
1414
"""
1515

16+
import io
1617
import os
1718
import stat
1819
import sys
@@ -43,11 +44,10 @@ def check_can_open(self) -> None:
4344
# Open and close the file in case we're opening it for
4445
# reading so that we can catch at least some errors in
4546
# some cases early.
46-
self._open_handle.close()
47-
self._stream = None
47+
self.open()
48+
self.close()
4849

49-
@property
50-
def _open_handle(self) -> t.BinaryIO:
50+
def open(self) -> t.BinaryIO:
5151
if self._stream is None:
5252
self._stream = open(self.name, "rb")
5353
return self._stream
@@ -60,7 +60,7 @@ def close(self) -> None:
6060
self._stream = None
6161

6262
def __getattr__(self, name: str) -> t.Any:
63-
return getattr(self._open_handle, name)
63+
return getattr(self.open(), name)
6464

6565
def __repr__(self) -> str:
6666
return "BinaryFileInput(filename={self.filename!r})"
@@ -77,4 +77,8 @@ def __exit__(
7777
self.close()
7878

7979
def __iter__(self) -> t.Iterator[bytes]:
80-
return iter(self._open_handle)
80+
return iter(self.open())
81+
82+
83+
# register as an IOBase implementation, since pyjson5 relies on an isinstance() check
84+
io.IOBase.register(BinaryFileInput)

0 commit comments

Comments
 (0)