Skip to content

Commit 6f86f48

Browse files
committed
Refactor usage of "inspect" functions for simplification
1 parent cfd1e80 commit 6f86f48

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

loguru/_logger.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,13 @@
7474
import builtins
7575
import contextlib
7676
import functools
77-
import inspect
7877
import itertools
7978
import logging
8079
import re
8180
import sys
8281
import warnings
8382
from collections import namedtuple
84-
from inspect import isclass
83+
from inspect import isclass, iscoroutinefunction, isgeneratorfunction
8584
from multiprocessing import current_process
8685
from os.path import basename, splitext
8786
from threading import current_thread
@@ -790,9 +789,7 @@ def add(
790789
encoding = getattr(sink, "encoding", None)
791790
terminator = ""
792791
exception_prefix = "\n"
793-
elif inspect.iscoroutinefunction(sink) or inspect.iscoroutinefunction(
794-
getattr(sink, "__call__", None)
795-
):
792+
elif iscoroutinefunction(sink) or iscoroutinefunction(getattr(sink, "__call__", None)):
796793
name = getattr(sink, "__name__", None) or repr(sink)
797794

798795
if colorize is None:
@@ -807,7 +804,8 @@ def add(
807804
# running loop in Python 3.5.2 and earlier versions, see python/asyncio#452.
808805
if enqueue and loop is None:
809806
loop = asyncio.get_event_loop()
810-
coro = sink if inspect.iscoroutinefunction(sink) else sink.__call__
807+
808+
coro = sink if iscoroutinefunction(sink) else sink.__call__
811809
wrapped_sink = AsyncSink(coro, loop, error_interceptor)
812810
encoding = "utf8"
813811
terminator = "\n"
@@ -1201,14 +1199,14 @@ def __exit__(self_, type_, value, traceback_):
12011199
def __call__(_, function):
12021200
catcher = Catcher(True)
12031201

1204-
if inspect.iscoroutinefunction(function):
1202+
if iscoroutinefunction(function):
12051203

12061204
async def catch_wrapper(*args, **kwargs):
12071205
with catcher:
12081206
return await function(*args, **kwargs)
12091207
return default
12101208

1211-
elif inspect.isgeneratorfunction(function):
1209+
elif isgeneratorfunction(function):
12121210

12131211
def catch_wrapper(*args, **kwargs):
12141212
with catcher:

tests/test_repr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async def my_async_function(message):
140140
def test_coroutine_function_without_name(monkeypatch):
141141
async_function = Wrapper(lambda _: None, repr="<AsyncFunctionWithout>", name=None)
142142
monkeypatch.setattr(
143-
loguru._logger.inspect,
143+
loguru._logger,
144144
"iscoroutinefunction",
145145
lambda x: x is async_function or iscoroutinefunction(x),
146146
)
@@ -155,7 +155,7 @@ def test_coroutine_function_without_name(monkeypatch):
155155
def test_coroutine_function_with_empty_name(monkeypatch):
156156
async_function = Wrapper(lambda _: None, repr="<AsyncFunctionEmpty>", name="")
157157
monkeypatch.setattr(
158-
loguru._logger.inspect,
158+
loguru._logger,
159159
"iscoroutinefunction",
160160
lambda x: x is async_function or iscoroutinefunction(x),
161161
)

0 commit comments

Comments
 (0)