Skip to content

Commit

Permalink
Fix "repr()" tests failing on Python 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Jul 17, 2022
1 parent d0b14ce commit 4fe21f6
Showing 1 changed file with 36 additions and 51 deletions.
87 changes: 36 additions & 51 deletions tests/test_repr.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
import builtins
import logging
import pathlib
import re
import sys
from inspect import iscoroutinefunction

import loguru
from loguru import logger


class Wrapper:
def __init__(self, wrapped, *, repr, name):
self._wrapped = wrapped
self._repr = repr
self._name = name
self.raised = False

def __repr__(self):
return self._repr

def __getattr__(self, name):
if name == "__name__":
if self._name is None:
self.raised = True
raise AttributeError
else:
return self._name
return getattr(self._wrapped, name)


def test_no_handler():
assert repr(logger) == "<loguru.logger handlers=[]>"

Expand Down Expand Up @@ -112,22 +89,30 @@ def my_function(message):
assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=my_function)]>"


def test_function_without_name(monkeypatch):
function = Wrapper(lambda _: None, repr="<FunctionWithout>", name=None)
monkeypatch.setattr(builtins, "callable", lambda x: x is function or callable(x))
def test_callable_without_name():
class Function:
def __call__(self):
pass

def __repr__(self):
return "<FunctionWithout>"

logger.add(function)
logger.add(Function())
assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=<FunctionWithout>)]>"
assert function.raised


def test_function_with_empty_name(monkeypatch):
function = Wrapper(lambda _: None, repr="<FunctionEmpty>", name="")
monkeypatch.setattr(builtins, "callable", lambda x: x is function or callable(x))
def test_callable_with_empty_name():
class Function:
__name__ = ""

def __call__(self):
pass

def __repr__(self):
return "<FunctionEmpty>"

logger.add(function)
logger.add(Function())
assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=<FunctionEmpty>)]>"
assert not function.raised


def test_coroutine_function():
Expand All @@ -138,32 +123,32 @@ async def my_async_function(message):
assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=my_async_function)]>"


def test_coroutine_function_without_name(monkeypatch):
async_function = Wrapper(lambda _: None, repr="<AsyncFunctionWithout>", name=None)
monkeypatch.setattr(
loguru._logger,
"iscoroutinefunction",
lambda x: x is async_function or iscoroutinefunction(x),
)
def test_coroutine_callable_without_name():
class CoroutineFunction:
async def __call__(self):
pass

def __repr__(self):
return "<AsyncFunctionWithout>"

logger.add(async_function)
logger.add(CoroutineFunction())
assert (
repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=<AsyncFunctionWithout>)]>"
)
assert async_function.raised


def test_coroutine_function_with_empty_name(monkeypatch):
async_function = Wrapper(lambda _: None, repr="<AsyncFunctionEmpty>", name="")
monkeypatch.setattr(
loguru._logger,
"iscoroutinefunction",
lambda x: x is async_function or iscoroutinefunction(x),
)
def test_coroutine_function_with_empty_name():
class CoroutineFunction:
__name__ = ""

def __call__(self):
pass

def __repr__(self):
return "<AsyncFunctionEmpty>"

logger.add(async_function)
logger.add(CoroutineFunction())
assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=<AsyncFunctionEmpty>)]>"
assert not async_function.raised


def test_standard_handler():
Expand Down

0 comments on commit 4fe21f6

Please sign in to comment.