Skip to content

Commit

Permalink
Add explicit type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
mosquito committed Mar 8, 2024
1 parent 021ccd9 commit 2272d61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
7 changes: 7 additions & 0 deletions aiomisc/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def time_ns() -> int:
from typing_extensions import final # type: ignore


try:
from typing import TypeAlias
except ImportError:
from typing_extensions import TypeAlias


if sys.version_info >= (3, 10):
from typing import ParamSpec
else:
Expand Down Expand Up @@ -118,6 +124,7 @@ def sock_set_reuseport(sock: socket.socket, reuse_port: bool) -> None:
"EventLoopMixin",
"ParamSpec",
"Protocol",
"TypeAlias",
"entry_pont_iterator",
"event_loop_policy",
"final",
Expand Down
27 changes: 14 additions & 13 deletions aiomisc/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
Optional, TextIO, TypeVar, Union, overload,
)

from .compat import EventLoopMixin
from .compat import EventLoopMixin, TypeAlias


T = TypeVar("T", bound=Any)
FilePath: TypeAlias = Union[str, Path]


def proxy_method_async(
Expand Down Expand Up @@ -232,7 +233,7 @@ async def writelines(self, lines: List[AnyStr]) -> None:
await self.__execute_in_thread(self.fp.writelines, lines)


AsyncFileIOBase = AsyncFileIO
AsyncFileIOBase: TypeAlias = AsyncFileIO


class AsyncBinaryIO(AsyncFileIO[bytes]):
Expand Down Expand Up @@ -265,8 +266,8 @@ def buffer(self) -> AsyncBinaryIO:


# Aliases
AsyncBytesFileIO = AsyncBinaryIO
AsyncTextFileIO = AsyncTextIO
AsyncBytesFileIO: TypeAlias = AsyncBinaryIO
AsyncTextFileIO: TypeAlias = AsyncTextIO


class AsyncGzipBinaryIO(AsyncBytesFileIO):
Expand Down Expand Up @@ -312,22 +313,21 @@ class Compression(Enum):
LZMA = (AsyncLzmaBinaryIO, AsyncLzmaTextIO)


AsyncFileType = Union[AsyncTextIO, AsyncBinaryIO]

# Deprecated excluded from __all__
AsyncFileT = AsyncFileType
AsyncFileType: TypeAlias = Union[
AsyncFileIO[AnyStr], AsyncTextIO, AsyncBinaryIO,
]

BinaryModes = Literal[
BinaryModes: TypeAlias = Literal[
"rb", "wb", "ab", "xb", "rb+", "wb+", "ab+", "xb+", "br", "br+",
"bw+", "ba+", "bx+",
]

TextModes = Literal["r", "w", "a", "x", "r+", "w+", "a+", "x+"]
TextModes: TypeAlias = Literal["r", "w", "a", "x", "r+", "w+", "a+", "x+"]


@overload
def async_open(
fname: Union[str, Path],
fname: FilePath,
mode: BinaryModes,
compression: Compression = Compression.NONE,
encoding: str = sys.getdefaultencoding(),
Expand All @@ -338,7 +338,8 @@ def async_open(

@overload
def async_open(
fname: Union[str, Path], mode: Union[TextModes],
fname: FilePath,
mode: TextModes,
compression: Compression = Compression.NONE,
encoding: str = sys.getdefaultencoding(),
*args: Any, **kwargs: Any,
Expand All @@ -347,7 +348,7 @@ def async_open(


def async_open(
fname: Union[str, Path], mode: str = "r",
fname: FilePath, mode: str = "r",
compression: Compression = Compression.NONE,
encoding: str = sys.getdefaultencoding(),
*args: Any, **kwargs: Any,
Expand Down

0 comments on commit 2272d61

Please sign in to comment.