Skip to content

Commit ec8300b

Browse files
committed
Fix typing of some methods in fs.iotools and fs.memoryfs file APIs
1 parent 221b9e5 commit ec8300b

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

fs/iotools.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
from __future__ import print_function
55
from __future__ import unicode_literals
66

7+
import array
78
import io
89
import typing
910
from io import SEEK_SET, SEEK_CUR
1011

1112
from .mode import Mode
1213

1314
if typing.TYPE_CHECKING:
15+
import mmap
1416
from io import RawIOBase
1517
from typing import (
1618
Any,
@@ -88,8 +90,11 @@ def truncate(self, size=None):
8890
return self._f.truncate(size)
8991

9092
def write(self, data):
91-
# type: (bytes) -> int
92-
count = self._f.write(data)
93+
# type: (Union[bytes, bytearray, memoryview, array.array[Any], mmap.mmap]) -> int
94+
if isinstance(data, array.array):
95+
count = self._f.write(data.tobytes())
96+
else:
97+
count = self._f.write(data) # type: ignore
9398
return len(data) if count is None else count
9499

95100
@typing.no_type_check
@@ -130,17 +135,20 @@ def readinto1(self, b):
130135
b[:bytes_read] = data
131136
return bytes_read
132137

133-
def readline(self, limit=-1):
134-
# type: (int) -> bytes
135-
return self._f.readline(limit)
138+
def readline(self, limit=None):
139+
# type: (Optional[int]) -> bytes
140+
return self._f.readline(-1 if limit is None else limit)
136141

137-
def readlines(self, hint=-1):
138-
# type: (int) -> List[bytes]
139-
return self._f.readlines(hint)
142+
def readlines(self, hint=None):
143+
# type: (Optional[int]) -> List[bytes]
144+
return self._f.readlines(-1 if hint is None else hint)
140145

141-
def writelines(self, sequence):
142-
# type: (Iterable[Union[bytes, bytearray]]) -> None
143-
return self._f.writelines(sequence)
146+
def writelines(self, lines):
147+
# type: (Iterable[Union[bytes, bytearray, memoryview, array.array[Any], mmap.mmap]]) -> None
148+
_lines = (
149+
line.tobytes() if isinstance(line, array.array) else line for line in lines
150+
)
151+
return self._f.writelines(typing.cast("Iterable[bytes]", _lines))
144152

145153
def __iter__(self):
146154
# type: () -> Iterator[bytes]

fs/memoryfs.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
from ._typing import overload
2525

2626
if typing.TYPE_CHECKING:
27+
import array
28+
import mmap
2729
from typing import (
2830
Any,
2931
BinaryIO,
3032
Collection,
3133
Dict,
34+
Iterable,
3235
Iterator,
3336
List,
3437
Optional,
@@ -116,8 +119,8 @@ def next(self):
116119

117120
__next__ = next
118121

119-
def readline(self, size=-1):
120-
# type: (int) -> bytes
122+
def readline(self, size=None):
123+
# type: (Optional[int]) -> bytes
121124
if not self._mode.reading:
122125
raise IOError("File not open for reading")
123126
with self._seek_lock():
@@ -131,7 +134,7 @@ def close(self):
131134
self._dir_entry.remove_open_file(self)
132135
super(_MemoryFile, self).close()
133136

134-
def read(self, size=-1):
137+
def read(self, size=None):
135138
# type: (Optional[int]) -> bytes
136139
if not self._mode.reading:
137140
raise IOError("File not open for reading")
@@ -190,15 +193,15 @@ def writable(self):
190193
return self._mode.writing
191194

192195
def write(self, data):
193-
# type: (bytes) -> int
196+
# type: (Union[bytes, bytearray, memoryview, array.array[Any], mmap.mmap]) -> int
194197
if not self._mode.writing:
195198
raise IOError("File not open for writing")
196199
with self._seek_lock():
197200
self.on_modify()
198201
return self._bytes_io.write(data)
199202

200-
def writelines(self, sequence): # type: ignore
201-
# type: (List[bytes]) -> None
203+
def writelines(self, sequence):
204+
# type: (Iterable[Union[bytes, bytearray, memoryview, array.array[Any], mmap.mmap]]) -> None
202205
# FIXME(@althonos): For some reason the stub for IOBase.writelines
203206
# is List[Any] ?! It should probably be Iterable[ByteString]
204207
with self._seek_lock():

fs/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def format_directory(path, levels):
133133
try:
134134
directory = sorted(
135135
fs.filterdir(path, exclude_dirs=exclude, files=filter),
136-
key=sort_key_dirs_first if dirs_first else sort_key,
136+
key=sort_key_dirs_first if dirs_first else sort_key, # type: ignore
137137
)
138138
except Exception as error:
139139
prefix = (

0 commit comments

Comments
 (0)