Browse Source

Typehint File

pull/6863/head
Rapptz 4 years ago
parent
commit
2e12f6de9c
  1. 27
      discord/file.py

27
discord/file.py

@ -22,13 +22,17 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
""" """
import os.path from __future__ import annotations
from typing import Optional, TYPE_CHECKING, Union
import os
import io import io
__all__ = ( __all__ = (
'File', 'File',
) )
class File: class File:
r"""A parameter object used for :meth:`abc.Messageable.send` r"""A parameter object used for :meth:`abc.Messageable.send`
for sending file objects. for sending file objects.
@ -40,7 +44,7 @@ class File:
Attributes Attributes
----------- -----------
fp: Union[:class:`str`, :class:`io.BufferedIOBase`] fp: Union[:class:`os.PathLike`, :class:`io.BufferedIOBase`]
A file-like object opened in binary mode and read mode A file-like object opened in binary mode and read mode
or a filename representing a file in the hard drive to or a filename representing a file in the hard drive to
open. open.
@ -62,9 +66,18 @@ class File:
__slots__ = ('fp', 'filename', 'spoiler', '_original_pos', '_owner', '_closer') __slots__ = ('fp', 'filename', 'spoiler', '_original_pos', '_owner', '_closer')
def __init__(self, fp, filename=None, *, spoiler=False): if TYPE_CHECKING:
self.fp = fp fp: io.BufferedIOBase
filename: Optional[str]
spoiler: bool
def __init__(
self,
fp: Union[str, bytes, os.PathLike, io.BufferedIOBase],
filename: Optional[str] = None,
*,
spoiler: bool = False,
):
if isinstance(fp, io.IOBase): if isinstance(fp, io.IOBase):
if not (fp.seekable() and fp.readable()): if not (fp.seekable() and fp.readable()):
raise ValueError(f'File buffer {fp!r} must be seekable and readable') raise ValueError(f'File buffer {fp!r} must be seekable and readable')
@ -96,7 +109,7 @@ class File:
self.spoiler = spoiler or (self.filename is not None and self.filename.startswith('SPOILER_')) self.spoiler = spoiler or (self.filename is not None and self.filename.startswith('SPOILER_'))
def reset(self, *, seek=True): def reset(self, *, seek: bool = True) -> None:
# The `seek` parameter is needed because # The `seek` parameter is needed because
# the retry-loop is iterated over multiple times # the retry-loop is iterated over multiple times
# starting from 0, as an implementation quirk # starting from 0, as an implementation quirk
@ -108,7 +121,7 @@ class File:
if seek: if seek:
self.fp.seek(self._original_pos) self.fp.seek(self._original_pos)
def close(self): def close(self) -> None:
self.fp.close = self._closer self.fp.close = self._closer
if self._owner: if self._owner:
self._closer() self._closer()

Loading…
Cancel
Save