Cherry pick upstream commit to fix python 3.11 FTBFS (Closes: #1017233, #1017483)
Nilesh Patra
1 year, 8 months ago
0 | From 4fe21f66991abeb1905e24c3bc3c634543d959a2 Mon Sep 17 00:00:00 2001 | |
1 | From: Delgan <delgan.py@gmail.com> | |
2 | Date: Sun, 17 Jul 2022 09:18:56 +0200 | |
3 | Subject: [PATCH] Fix "repr()" tests failing on Python 3.11 | |
4 | ||
5 | --- | |
6 | tests/test_repr.py | 87 +++++++++++++++++++--------------------------- | |
7 | 1 file changed, 36 insertions(+), 51 deletions(-) | |
8 | ||
9 | --- a/tests/test_repr.py | |
10 | +++ b/tests/test_repr.py | |
11 | @@ -1,34 +1,11 @@ | |
12 | -import builtins | |
13 | import logging | |
14 | import pathlib | |
15 | import re | |
16 | import sys | |
17 | -from inspect import iscoroutinefunction | |
18 | ||
19 | -import loguru | |
20 | from loguru import logger | |
21 | ||
22 | ||
23 | -class Wrapper: | |
24 | - def __init__(self, wrapped, *, repr, name): | |
25 | - self._wrapped = wrapped | |
26 | - self._repr = repr | |
27 | - self._name = name | |
28 | - self.raised = False | |
29 | - | |
30 | - def __repr__(self): | |
31 | - return self._repr | |
32 | - | |
33 | - def __getattr__(self, name): | |
34 | - if name == "__name__": | |
35 | - if self._name is None: | |
36 | - self.raised = True | |
37 | - raise AttributeError | |
38 | - else: | |
39 | - return self._name | |
40 | - return getattr(self._wrapped, name) | |
41 | - | |
42 | - | |
43 | def test_no_handler(): | |
44 | assert repr(logger) == "<loguru.logger handlers=[]>" | |
45 | ||
46 | @@ -112,22 +89,30 @@ | |
47 | assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=my_function)]>" | |
48 | ||
49 | ||
50 | -def test_function_without_name(monkeypatch): | |
51 | - function = Wrapper(lambda _: None, repr="<FunctionWithout>", name=None) | |
52 | - monkeypatch.setattr(builtins, "callable", lambda x: x is function or callable(x)) | |
53 | +def test_callable_without_name(): | |
54 | + class Function: | |
55 | + def __call__(self): | |
56 | + pass | |
57 | + | |
58 | + def __repr__(self): | |
59 | + return "<FunctionWithout>" | |
60 | ||
61 | - logger.add(function) | |
62 | + logger.add(Function()) | |
63 | assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=<FunctionWithout>)]>" | |
64 | - assert function.raised | |
65 | ||
66 | ||
67 | -def test_function_with_empty_name(monkeypatch): | |
68 | - function = Wrapper(lambda _: None, repr="<FunctionEmpty>", name="") | |
69 | - monkeypatch.setattr(builtins, "callable", lambda x: x is function or callable(x)) | |
70 | +def test_callable_with_empty_name(): | |
71 | + class Function: | |
72 | + __name__ = "" | |
73 | + | |
74 | + def __call__(self): | |
75 | + pass | |
76 | + | |
77 | + def __repr__(self): | |
78 | + return "<FunctionEmpty>" | |
79 | ||
80 | - logger.add(function) | |
81 | + logger.add(Function()) | |
82 | assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=<FunctionEmpty>)]>" | |
83 | - assert not function.raised | |
84 | ||
85 | ||
86 | def test_coroutine_function(): | |
87 | @@ -138,32 +123,32 @@ | |
88 | assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=my_async_function)]>" | |
89 | ||
90 | ||
91 | -def test_coroutine_function_without_name(monkeypatch): | |
92 | - async_function = Wrapper(lambda _: None, repr="<AsyncFunctionWithout>", name=None) | |
93 | - monkeypatch.setattr( | |
94 | - loguru._logger, | |
95 | - "iscoroutinefunction", | |
96 | - lambda x: x is async_function or iscoroutinefunction(x), | |
97 | - ) | |
98 | +def test_coroutine_callable_without_name(): | |
99 | + class CoroutineFunction: | |
100 | + async def __call__(self): | |
101 | + pass | |
102 | ||
103 | - logger.add(async_function) | |
104 | + def __repr__(self): | |
105 | + return "<AsyncFunctionWithout>" | |
106 | + | |
107 | + logger.add(CoroutineFunction()) | |
108 | assert ( | |
109 | repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=<AsyncFunctionWithout>)]>" | |
110 | ) | |
111 | - assert async_function.raised | |
112 | ||
113 | ||
114 | -def test_coroutine_function_with_empty_name(monkeypatch): | |
115 | - async_function = Wrapper(lambda _: None, repr="<AsyncFunctionEmpty>", name="") | |
116 | - monkeypatch.setattr( | |
117 | - loguru._logger, | |
118 | - "iscoroutinefunction", | |
119 | - lambda x: x is async_function or iscoroutinefunction(x), | |
120 | - ) | |
121 | +def test_coroutine_function_with_empty_name(): | |
122 | + class CoroutineFunction: | |
123 | + __name__ = "" | |
124 | + | |
125 | + def __call__(self): | |
126 | + pass | |
127 | + | |
128 | + def __repr__(self): | |
129 | + return "<AsyncFunctionEmpty>" | |
130 | ||
131 | - logger.add(async_function) | |
132 | + logger.add(CoroutineFunction()) | |
133 | assert repr(logger) == "<loguru.logger handlers=[(id=0, level=10, sink=<AsyncFunctionEmpty>)]>" | |
134 | - assert not async_function.raised | |
135 | ||
136 | ||
137 | def test_standard_handler(): |