Skip to content

Commit 7608a01

Browse files
committed
Fix Mypy unit tests due to deprecated "force_uppercase_builtins" option
The latest version of Mypy removes this configuration parameter, which we used to standardize the expected output across the different tested versions of Python. As a workaround, this feature is reimplemented using a basic regex. This'll likely become more complicated if they later force a change in the syntax of "Union" as well. Note that we're patching the expected output (differs for each Python version) in the test cases, not the actual Mypy output.
1 parent 87d70d0 commit 7608a01

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

tests/conftest.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import multiprocessing
88
import os
99
import pathlib
10+
import re
1011
import sys
1112
import threading
1213
import time
@@ -64,12 +65,21 @@ def _fix_positional_only_args(item: YamlTestItem):
6465
# Also patch the "severity" attribute because there is a parsing bug in the plugin.
6566
output.severity = output.severity.replace(", /", "")
6667

68+
def _fix_builtins_uppercase(item: YamlTestItem):
69+
"""Adapt case of builtins from the expected output for Python 3.6 to 3.8."""
70+
for output in item.expected_output:
71+
for builtin in ["dict", "list", "set", "tuple"]:
72+
# Quite hacky, but should work for all tested cases.
73+
pattern = re.compile(r"(?<![a-zA-Z0-9.])" + builtin + r"\[")
74+
replacement = builtin.capitalize() + "["
75+
output.message = pattern.sub(replacement, output.message)
76+
output.severity = pattern.sub(replacement, output.severity)
77+
6778
def _add_mypy_config(item: YamlTestItem):
6879
"""Add some extra options to the mypy configuration for Python 3.7+."""
6980
item.additional_mypy_config += "\n".join(
7081
[
7182
"show_error_codes = false",
72-
"force_uppercase_builtins = true",
7383
"force_union_syntax = true",
7484
]
7585
)
@@ -85,6 +95,9 @@ def pytest_collection_modifyitems(config, items):
8595
else:
8696
_fix_positional_only_args(item)
8797

98+
if sys.version_info < (3, 9):
99+
_fix_builtins_uppercase(item)
100+
88101

89102
@contextlib.contextmanager
90103
def new_event_loop_context():

tests/typesafety/test_logger.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
level = logger.level("INFO")
206206
reveal_type(level)
207207
out: |
208-
main:4: note: Revealed type is "Tuple[builtins.str, builtins.int, builtins.str, builtins.str, fallback=loguru.Level]"
208+
main:4: note: Revealed type is "tuple[builtins.str, builtins.int, builtins.str, builtins.str, fallback=loguru.Level]"
209209
210210
- case: level_set
211211
main: |
@@ -214,7 +214,7 @@
214214
level = logger.level("FOO", no=11, icon="!", color="<blue>")
215215
reveal_type(level)
216216
out: |
217-
main:4: note: Revealed type is "Tuple[builtins.str, builtins.int, builtins.str, builtins.str, fallback=loguru.Level]"
217+
main:4: note: Revealed type is "tuple[builtins.str, builtins.int, builtins.str, builtins.str, fallback=loguru.Level]"
218218
219219
- case: level_update
220220
main: |
@@ -223,7 +223,7 @@
223223
level = logger.level("INFO", color="<blue>")
224224
reveal_type(level)
225225
out: |
226-
main:4: note: Revealed type is "Tuple[builtins.str, builtins.int, builtins.str, builtins.str, fallback=loguru.Level]"
226+
main:4: note: Revealed type is "tuple[builtins.str, builtins.int, builtins.str, builtins.str, fallback=loguru.Level]"
227227
228228
- case: enable_and_disable_logger
229229
main: |
@@ -285,9 +285,9 @@
285285
out: |
286286
main:2: error: No overload variant of "add" of "Logger" matches argument types "Callable[[Any], None]", "int"
287287
main:2: note: Possible overload variants:
288-
main:2: note: def add(self, sink: Union[TextIO, Writable, Callable[[Message], None], Handler], *, level: Union[str, int] = ..., format: Union[str, Callable[[Record], str]] = ..., filter: Union[str, Callable[[Record], bool], Dict[Optional[str], Union[str, int, bool]], None] = ..., colorize: Optional[bool] = ..., serialize: bool = ..., backtrace: bool = ..., diagnose: bool = ..., enqueue: bool = ..., context: Union[str, BaseContext, None] = ..., catch: bool = ...) -> int
289-
main:2: note: def add(self, sink: Callable[[Message], Awaitable[None]], *, level: Union[str, int] = ..., format: Union[str, Callable[[Record], str]] = ..., filter: Union[str, Callable[[Record], bool], Dict[Optional[str], Union[str, int, bool]], None] = ..., colorize: Optional[bool] = ..., serialize: bool = ..., backtrace: bool = ..., diagnose: bool = ..., enqueue: bool = ..., catch: bool = ..., context: Union[str, BaseContext, None] = ..., loop: Optional[AbstractEventLoop] = ...) -> int
290-
main:2: note: def add(self, sink: Union[str, PathLike[str]], *, level: Union[str, int] = ..., format: Union[str, Callable[[Record], str]] = ..., filter: Union[str, Callable[[Record], bool], Dict[Optional[str], Union[str, int, bool]], None] = ..., colorize: Optional[bool] = ..., serialize: bool = ..., backtrace: bool = ..., diagnose: bool = ..., enqueue: bool = ..., context: Union[str, BaseContext, None] = ..., catch: bool = ..., rotation: Union[str, int, time, timedelta, Callable[[Message, TextIO], bool], List[Union[str, int, time, timedelta, Callable[[Message, TextIO], bool]]], None] = ..., retention: Union[str, int, timedelta, Callable[[List[str]], None], None] = ..., compression: Union[str, Callable[[str], None], None] = ..., delay: bool = ..., watch: bool = ..., mode: str = ..., buffering: int = ..., encoding: str = ..., errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) -> int
288+
main:2: note: def add(self, sink: Union[TextIO, Writable, Callable[[Message], None], Handler], *, level: Union[str, int] = ..., format: Union[str, Callable[[Record], str]] = ..., filter: Union[str, Callable[[Record], bool], dict[Optional[str], Union[str, int, bool]], None] = ..., colorize: Optional[bool] = ..., serialize: bool = ..., backtrace: bool = ..., diagnose: bool = ..., enqueue: bool = ..., context: Union[str, BaseContext, None] = ..., catch: bool = ...) -> int
289+
main:2: note: def add(self, sink: Callable[[Message], Awaitable[None]], *, level: Union[str, int] = ..., format: Union[str, Callable[[Record], str]] = ..., filter: Union[str, Callable[[Record], bool], dict[Optional[str], Union[str, int, bool]], None] = ..., colorize: Optional[bool] = ..., serialize: bool = ..., backtrace: bool = ..., diagnose: bool = ..., enqueue: bool = ..., catch: bool = ..., context: Union[str, BaseContext, None] = ..., loop: Optional[AbstractEventLoop] = ...) -> int
290+
main:2: note: def add(self, sink: Union[str, PathLike[str]], *, level: Union[str, int] = ..., format: Union[str, Callable[[Record], str]] = ..., filter: Union[str, Callable[[Record], bool], dict[Optional[str], Union[str, int, bool]], None] = ..., colorize: Optional[bool] = ..., serialize: bool = ..., backtrace: bool = ..., diagnose: bool = ..., enqueue: bool = ..., context: Union[str, BaseContext, None] = ..., catch: bool = ..., rotation: Union[str, int, time, timedelta, Callable[[Message, TextIO], bool], list[Union[str, int, time, timedelta, Callable[[Message, TextIO], bool]]], None] = ..., retention: Union[str, int, timedelta, Callable[[list[str]], None], None] = ..., compression: Union[str, Callable[[str], None], None] = ..., delay: bool = ..., watch: bool = ..., mode: str = ..., buffering: int = ..., encoding: str = ..., errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) -> int
291291
292292
- case: invalid_logged_object_formatting
293293
main: |
@@ -310,11 +310,11 @@
310310
extra=[1],
311311
)
312312
out: |
313-
main:3: error: List item 0 has incompatible type "Dict[str, str]"; expected "Union[BasicHandlerConfig, FileHandlerConfig, AsyncHandlerConfig]"
313+
main:3: error: List item 0 has incompatible type "dict[str, str]"; expected "Union[BasicHandlerConfig, FileHandlerConfig, AsyncHandlerConfig]"
314314
main:4: error: Extra key "baz" for TypedDict "LevelConfig"
315315
main:5: error: Argument "patcher" to "configure" of "Logger" has incompatible type "int"; expected "Optional[Callable[[Record], None]]"
316-
main:6: error: List item 0 has incompatible type "Dict[str, str]"; expected "Tuple[Optional[str], bool]"
317-
main:7: error: Argument "extra" to "configure" of "Logger" has incompatible type "List[int]"; expected "Optional[Dict[Any, Any]]"
316+
main:6: error: List item 0 has incompatible type "dict[str, str]"; expected "tuple[Optional[str], bool]"
317+
main:7: error: Argument "extra" to "configure" of "Logger" has incompatible type "list[int]"; expected "Optional[dict[Any, Any]]"
318318
319319
- case: reinstall
320320
main: |

0 commit comments

Comments
 (0)