From 6f86f487590fe8f264a3bb26c722bf4be354687b Mon Sep 17 00:00:00 2001 From: Delgan Date: Sat, 18 Jul 2020 16:44:32 +0200 Subject: [PATCH] Refactor usage of "inspect" functions for simplification --- loguru/_logger.py | 14 ++++++-------- tests/test_repr.py | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/loguru/_logger.py b/loguru/_logger.py index 388ee540..390ea973 100644 --- a/loguru/_logger.py +++ b/loguru/_logger.py @@ -74,14 +74,13 @@ import builtins import contextlib import functools -import inspect import itertools import logging import re import sys import warnings from collections import namedtuple -from inspect import isclass +from inspect import isclass, iscoroutinefunction, isgeneratorfunction from multiprocessing import current_process from os.path import basename, splitext from threading import current_thread @@ -790,9 +789,7 @@ def add( encoding = getattr(sink, "encoding", None) terminator = "" exception_prefix = "\n" - elif inspect.iscoroutinefunction(sink) or inspect.iscoroutinefunction( - getattr(sink, "__call__", None) - ): + elif iscoroutinefunction(sink) or iscoroutinefunction(getattr(sink, "__call__", None)): name = getattr(sink, "__name__", None) or repr(sink) if colorize is None: @@ -807,7 +804,8 @@ def add( # running loop in Python 3.5.2 and earlier versions, see python/asyncio#452. if enqueue and loop is None: loop = asyncio.get_event_loop() - coro = sink if inspect.iscoroutinefunction(sink) else sink.__call__ + + coro = sink if iscoroutinefunction(sink) else sink.__call__ wrapped_sink = AsyncSink(coro, loop, error_interceptor) encoding = "utf8" terminator = "\n" @@ -1201,14 +1199,14 @@ def __exit__(self_, type_, value, traceback_): def __call__(_, function): catcher = Catcher(True) - if inspect.iscoroutinefunction(function): + if iscoroutinefunction(function): async def catch_wrapper(*args, **kwargs): with catcher: return await function(*args, **kwargs) return default - elif inspect.isgeneratorfunction(function): + elif isgeneratorfunction(function): def catch_wrapper(*args, **kwargs): with catcher: diff --git a/tests/test_repr.py b/tests/test_repr.py index c7000d9e..a7c57c49 100644 --- a/tests/test_repr.py +++ b/tests/test_repr.py @@ -140,7 +140,7 @@ async def my_async_function(message): def test_coroutine_function_without_name(monkeypatch): async_function = Wrapper(lambda _: None, repr="", name=None) monkeypatch.setattr( - loguru._logger.inspect, + loguru._logger, "iscoroutinefunction", lambda x: x is async_function or iscoroutinefunction(x), ) @@ -155,7 +155,7 @@ def test_coroutine_function_without_name(monkeypatch): def test_coroutine_function_with_empty_name(monkeypatch): async_function = Wrapper(lambda _: None, repr="", name="") monkeypatch.setattr( - loguru._logger.inspect, + loguru._logger, "iscoroutinefunction", lambda x: x is async_function or iscoroutinefunction(x), )