Skip to content

Commit

Permalink
refactor: Only log warning if async mode is used
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Dec 28, 2021
1 parent aebbe4e commit 356e848
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/griffe/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sys
import traceback
from contextlib import suppress
from functools import lru_cache
from pathlib import Path
from typing import Any, Iterator, Sequence, Tuple

Expand All @@ -34,22 +35,23 @@
logger = get_logger(__name__)


async def _read_async(path):
async with aopen(path) as fd:
return await fd.read()
@lru_cache(maxsize=1)
def _get_async_reader():
try: # noqa: WPS503 (false-positive)
from aiofiles import open as aopen
except ModuleNotFoundError:
logger.warning("aiofiles is not installed, fallback to blocking read")

async def _read_async(path): # noqa: WPS430
return path.read_text()

async def _read_sync(path):
return path.read_text()
else:

async def _read_async(path): # noqa: WPS430,WPS440
async with aopen(path) as fd:
return await fd.read()

try:
from aiofiles import open as aopen
except ModuleNotFoundError:
logger.warning("aiofiles is not installed, fallback to blocking read")
read_async = _read_sync
else:
read_async = _read_async
return _read_async


_builtin_modules: set[str] = set(sys.builtin_module_names)
Expand Down Expand Up @@ -282,7 +284,7 @@ async def _load_module_path(
) -> Module:
logger.debug(f"Loading path {module_path}")
try:
code = await read_async(module_path)
code = await _get_async_reader()(module_path)
except OSError:
module = self._create_module(module_name, module_path)
else:
Expand Down

0 comments on commit 356e848

Please sign in to comment.