aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorDaniel Mensinger <daniel@mensinger-ka.de>2021-06-05 09:44:35 +0200
committerDaniel Mensinger <daniel@mensinger-ka.de>2021-06-05 12:35:48 +0200
commit27bb5f536a0f6d2b97a3e46348dc9f515a46a651 (patch)
treed23f3d9e74c2876003ba6cbc0c651de6449df55c /mesonbuild
parent969ee9d85bb85b7aab2d21ebd1368c4ab2e90771 (diff)
downloadmeson-27bb5f536a0f6d2b97a3e46348dc9f515a46a651.zip
meson-27bb5f536a0f6d2b97a3e46348dc9f515a46a651.tar.gz
meson-27bb5f536a0f6d2b97a3e46348dc9f515a46a651.tar.bz2
typing: mlog use StringProtocol
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/_typing.py3
-rw-r--r--mesonbuild/mlog.py13
2 files changed, 14 insertions, 2 deletions
diff --git a/mesonbuild/_typing.py b/mesonbuild/_typing.py
index a8faa43..a01be33 100644
--- a/mesonbuild/_typing.py
+++ b/mesonbuild/_typing.py
@@ -33,6 +33,9 @@ from typing_extensions import Protocol
T = typing.TypeVar('T')
+class StringProtocol(Protocol):
+ def __str__(self) -> str: ...
+
class ImmutableListProtocol(Protocol[T]):
"""A protocol used in cases where a list is returned, but should not be
diff --git a/mesonbuild/mlog.py b/mesonbuild/mlog.py
index 230b6f7..5c76677 100644
--- a/mesonbuild/mlog.py
+++ b/mesonbuild/mlog.py
@@ -21,6 +21,9 @@ import typing as T
from contextlib import contextmanager
from pathlib import Path
+if T.TYPE_CHECKING:
+ from ._typing import StringProtocol
+
"""This is (mostly) a standalone module used to write logging
information about Meson runs. Some output goes to screen,
some to logging dir and some goes to both."""
@@ -136,7 +139,7 @@ class AnsiDecorator:
def __str__(self) -> str:
return self.get_text(colorize_console())
-TV_Loggable = T.Union[str, AnsiDecorator]
+TV_Loggable = T.Union[str, AnsiDecorator, 'StringProtocol']
TV_LoggableList = T.List[TV_Loggable]
class AnsiText:
@@ -269,7 +272,13 @@ def log_once(*args: TV_Loggable, is_error: bool = False,
This considers ansi decorated values by the values they wrap without
regard for the AnsiDecorator itself.
"""
- t = tuple(a.text if isinstance(a, AnsiDecorator) else a for a in args)
+ def to_str(x: TV_Loggable) -> str:
+ if isinstance(x, str):
+ return x
+ if isinstance(x, AnsiDecorator):
+ return x.text
+ return str(x)
+ t = tuple(to_str(a) for a in args)
if t in _logged_once:
return
_logged_once.add(t)