Skip to content

Commit

Permalink
Enable linting, actually (#170)
Browse files Browse the repository at this point in the history
* Enable linting, actually

* Tweak
  • Loading branch information
Tinche authored Aug 8, 2023
1 parent e3571c5 commit 13e604c
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ test:
pdm run pytest -x --ff tests

lint:
pdm run flake8 src tests && pdm run black --check --verbose src tests
pdm run flake8 src tests && pdm run black --check src tests
38 changes: 24 additions & 14 deletions src/aiofiles/os.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
"""Async executor versions of file functions from the os module."""
import asyncio
from functools import partial, wraps
import os


def wrap(func):
@wraps(func)
async def run(*args, loop=None, executor=None, **kwargs):
if loop is None:
loop = asyncio.get_running_loop()
pfunc = partial(func, *args, **kwargs)
return await loop.run_in_executor(executor, pfunc)

return run


from . import ospath as path
from .ospath import wrap

__all__ = [
"path",
"stat",
"statvfs",
"rename",
"renames",
"replace",
"remove",
"unlink",
"mkdir",
"makedirs",
"rmdir",
"removedirs",
"link",
"symlink",
"readlink",
"listdir",
"scandir",
"access",
"sendfile",
"wrap",
]


stat = wrap(os.stat)
Expand Down
16 changes: 14 additions & 2 deletions src/aiofiles/ospath.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
"""Async executor versions of file functions from the os.path module."""

from .os import wrap
import asyncio
from functools import partial, wraps
from os import path


def wrap(func):
@wraps(func)
async def run(*args, loop=None, executor=None, **kwargs):
if loop is None:
loop = asyncio.get_running_loop()
pfunc = partial(func, *args, **kwargs)
return await loop.run_in_executor(executor, pfunc)

return run


exists = wrap(path.exists)
isfile = wrap(path.isfile)
isdir = wrap(path.isdir)
Expand Down
8 changes: 2 additions & 6 deletions src/aiofiles/tempfile/temptypes.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
"""Async wrappers for spooled temp files and temp directory objects"""

# Imports
import asyncio
from types import coroutine
from functools import partial

from ..base import AsyncBase
from ..threadpool.utils import (
cond_delegate_to_executor,
delegate_to_executor,
proxy_property_directly,
cond_delegate_to_executor,
)
from functools import partial


@delegate_to_executor("fileno", "rollover")
Expand Down
45 changes: 26 additions & 19 deletions src/aiofiles/threadpool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
"""Handle files using a thread pool executor."""
import asyncio
import sys
from types import coroutine

from functools import partial, singledispatch
from io import (
FileIO,
TextIOBase,
BufferedIOBase,
BufferedRandom,
BufferedReader,
BufferedWriter,
BufferedRandom,
BufferedIOBase,
FileIO,
TextIOBase,
)
from functools import partial, singledispatch
from types import coroutine

from ..base import AiofilesContextManager
from .binary import (
AsyncBufferedIOBase,
AsyncBufferedReader,
AsyncFileIO,
AsyncIndirectBufferedIOBase,
AsyncIndirectBufferedReader,
AsyncIndirectFileIO,
)
from .text import AsyncTextIOWrapper, AsyncTextIndirectIOWrapper
from ..base import AiofilesContextManager
from .text import AsyncTextIndirectIOWrapper, AsyncTextIOWrapper

sync_open = open

Expand All @@ -48,7 +45,7 @@ def open(
opener=None,
*,
loop=None,
executor=None
executor=None,
):
return AiofilesContextManager(
_open(
Expand Down Expand Up @@ -78,7 +75,7 @@ def _open(
opener=None,
*,
loop=None,
executor=None
executor=None,
):
"""Open an asyncio file."""
if loop is None:
Expand Down Expand Up @@ -126,9 +123,19 @@ def _(file, *, loop=None, executor=None):
return AsyncFileIO(file, loop=loop, executor=executor)


stdin = AsyncTextIndirectIOWrapper('sys.stdin', None, None, indirect=lambda: sys.stdin)
stdout = AsyncTextIndirectIOWrapper('sys.stdout', None, None, indirect=lambda: sys.stdout)
stderr = AsyncTextIndirectIOWrapper('sys.stderr', None, None, indirect=lambda: sys.stderr)
stdin_bytes = AsyncIndirectBufferedIOBase('sys.stdin.buffer', None, None, indirect=lambda: sys.stdin.buffer)
stdout_bytes = AsyncIndirectBufferedIOBase('sys.stdout.buffer', None, None, indirect=lambda: sys.stdout.buffer)
stderr_bytes = AsyncIndirectBufferedIOBase('sys.stderr.buffer', None, None, indirect=lambda: sys.stderr.buffer)
stdin = AsyncTextIndirectIOWrapper("sys.stdin", None, None, indirect=lambda: sys.stdin)
stdout = AsyncTextIndirectIOWrapper(
"sys.stdout", None, None, indirect=lambda: sys.stdout
)
stderr = AsyncTextIndirectIOWrapper(
"sys.stderr", None, None, indirect=lambda: sys.stderr
)
stdin_bytes = AsyncIndirectBufferedIOBase(
"sys.stdin.buffer", None, None, indirect=lambda: sys.stdin.buffer
)
stdout_bytes = AsyncIndirectBufferedIOBase(
"sys.stdout.buffer", None, None, indirect=lambda: sys.stdout.buffer
)
stderr_bytes = AsyncIndirectBufferedIOBase(
"sys.stderr.buffer", None, None, indirect=lambda: sys.stderr.buffer
)
6 changes: 1 addition & 5 deletions src/aiofiles/threadpool/binary.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from ..base import AsyncBase, AsyncIndirectBase
from .utils import (
delegate_to_executor,
proxy_method_directly,
proxy_property_directly,
)
from .utils import delegate_to_executor, proxy_method_directly, proxy_property_directly


@delegate_to_executor(
Expand Down
6 changes: 1 addition & 5 deletions src/aiofiles/threadpool/text.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from ..base import AsyncBase, AsyncIndirectBase
from .utils import (
delegate_to_executor,
proxy_method_directly,
proxy_property_directly,
)
from .utils import delegate_to_executor, proxy_method_directly, proxy_property_directly


@delegate_to_executor(
Expand Down
15 changes: 8 additions & 7 deletions tests/test_os.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Tests for asyncio's os module."""
import aiofiles.os
import asyncio
import os
import platform
from os import stat
from os.path import join, dirname, exists, isdir
from os.path import dirname, exists, isdir, join
from pathlib import Path

import pytest
import platform

import aiofiles.os


@pytest.mark.asyncio
Expand Down Expand Up @@ -116,8 +118,7 @@ async def test_replace():
reason="sendfile() syscall doesn't allow file->file",
)
@pytest.mark.skipif(
platform.system() == "Darwin",
reason="sendfile() doesn't work on mac",
platform.system() == "Darwin", reason="sendfile() doesn't work on mac"
)
@pytest.mark.asyncio
async def test_sendfile_file(tmpdir):
Expand Down Expand Up @@ -395,7 +396,7 @@ async def test_listdir_dir_with_a_file_and_a_dir():
async def test_listdir_non_existing_dir():
"""Test the listdir call when the dir doesn't exist."""
some_dir = join(dirname(__file__), "resources", "some_dir")
with pytest.raises(FileNotFoundError) as excinfo:
with pytest.raises(FileNotFoundError):
await aiofiles.os.listdir(some_dir)


Expand Down Expand Up @@ -445,7 +446,7 @@ async def test_scandir_dir_with_only_one_dir():
async def test_scandir_non_existing_dir():
"""Test the scandir call when the dir doesn't exist."""
some_dir = join(dirname(__file__), "resources", "some_dir")
with pytest.raises(FileNotFoundError) as excinfo:
with pytest.raises(FileNotFoundError):
await aiofiles.os.scandir(some_dir)


Expand Down
7 changes: 5 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ python =
3.8: py38
3.9: py39
3.10: py310
3.11: py311
3.11: py311, lint
3.12: py312
pypy-3.7: pypy3

[tox]
envlist = py37, py38, py39, py310, py311, py312, pypy3
envlist = py37, py38, py39, py310, py311, py312, pypy3, lint
isolated_build = true
skipsdist = true

Expand All @@ -33,3 +33,6 @@ commands =
passenv = CI
package = wheel
wheel_build_env = .pkg

[flake8]
max-line-length = 88

0 comments on commit 13e604c

Please sign in to comment.