Skip to content

Commit 355bf50

Browse files
committed
chore: implement self-review comments
1 parent f148252 commit 355bf50

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

haystack/logging.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,55 @@
1616
class PatchedLogger(typing.Protocol):
1717
"""Class which enables using type checkers to find wrong logger usage."""
1818

19-
def debug(self, msg: str, *, _: Any = None, **kwargs: Any) -> Any:
19+
def debug(self, msg: str, *, _: Any = None, **kwargs: Any) -> None:
2020
...
2121

22-
def info(self, msg: str, *, _: Any = None, **kwargs: Any) -> Any:
22+
def info(self, msg: str, *, _: Any = None, **kwargs: Any) -> None:
2323
...
2424

25-
def warn(self, msg: str, *, _: Any = None, **kwargs: Any) -> Any:
25+
def warn(self, msg: str, *, _: Any = None, **kwargs: Any) -> None:
2626
...
2727

28-
def warning(self, msg: str, *, _: Any = None, **kwargs: Any) -> Any:
28+
def warning(self, msg: str, *, _: Any = None, **kwargs: Any) -> None:
2929
...
3030

31-
def error(self, msg: str, *, _: Any = None, **kwargs: Any) -> Any:
31+
def error(self, msg: str, *, _: Any = None, **kwargs: Any) -> None:
3232
...
3333

34-
def critical(self, msg: str, *, _: Any = None, **kwargs: Any) -> Any:
34+
def critical(self, msg: str, *, _: Any = None, **kwargs: Any) -> None:
3535
...
3636

37-
def exception(self, msg: str, *, _: Any = None, **kwargs: Any) -> Any:
37+
def exception(self, msg: str, *, _: Any = None, **kwargs: Any) -> None:
3838
...
3939

40-
def fatal(self, msg: str, *, _: Any = None, **kwargs: Any) -> Any:
40+
def fatal(self, msg: str, *, _: Any = None, **kwargs: Any) -> None:
4141
...
4242

43-
def log(self, level: int, msg: str, *, _: Any = None, **kwargs: Any) -> Any:
43+
def log(self, level: int, msg: str, *, _: Any = None, **kwargs: Any) -> None:
4444
...
4545

4646
def setLevel(self, level: int) -> None:
4747
...
4848

4949

50-
def patched_log_method(func: typing.Callable) -> typing.Callable:
50+
def patch_log_method_to_kwargs_only(func: typing.Callable) -> typing.Callable:
5151
"""A decorator to make sure that a function is only called with keyword arguments."""
5252

5353
@functools.wraps(func)
54-
def log_only_with_kwargs(msg, *, _: Any = None, **kwargs: Any) -> Any:
54+
def log_only_with_kwargs(msg, *, _: Any = None, **kwargs: Any) -> Any: # we need the `_` to avoid a syntax error
5555
existing_extra = kwargs.pop("extra", {})
5656
return func(msg, extra={**existing_extra, **kwargs})
5757

5858
return log_only_with_kwargs
5959

6060

61-
def patched_log_with_level_method(func: typing.Callable) -> typing.Callable:
61+
def patch_log_with_level_method_to_kwargs_only(func: typing.Callable) -> typing.Callable:
6262
"""A decorator to make sure that a function is only called with keyword arguments."""
6363

6464
@functools.wraps(func)
65-
def log_only_with_kwargs(level, msg, *, _: Any = None, **kwargs: Any) -> Any:
65+
def log_only_with_kwargs(
66+
level, msg, *, _: Any = None, **kwargs: Any # we need the `_` to avoid a syntax error
67+
) -> Any:
6668
existing_extra = kwargs.pop("extra", {})
6769
return func(level, msg, extra={**existing_extra, **kwargs})
6870

@@ -85,15 +87,15 @@ def getLogger(name: str) -> PatchedLogger:
8587
# We enforce keyword-arguments because
8688
# - it brings in consistency
8789
# - it makes structure logging effective, not just an available feature
88-
logger.debug = patched_log_method(logger.debug) # type: ignore
89-
logger.info = patched_log_method(logger.info) # type: ignore
90-
logger.warn = patched_log_method(logger.warn) # type: ignore
91-
logger.warning = patched_log_method(logger.warning) # type: ignore
92-
logger.error = patched_log_method(logger.error) # type: ignore
93-
logger.critical = patched_log_method(logger.critical) # type: ignore
94-
logger.exception = patched_log_method(logger.exception) # type: ignore
95-
logger.fatal = patched_log_method(logger.fatal) # type: ignore
96-
logger.log = patched_log_with_level_method(logger.log) # type: ignore
90+
logger.debug = patch_log_method_to_kwargs_only(logger.debug) # type: ignore
91+
logger.info = patch_log_method_to_kwargs_only(logger.info) # type: ignore
92+
logger.warn = patch_log_method_to_kwargs_only(logger.warn) # type: ignore
93+
logger.warning = patch_log_method_to_kwargs_only(logger.warning) # type: ignore
94+
logger.error = patch_log_method_to_kwargs_only(logger.error) # type: ignore
95+
logger.critical = patch_log_method_to_kwargs_only(logger.critical) # type: ignore
96+
logger.exception = patch_log_method_to_kwargs_only(logger.exception) # type: ignore
97+
logger.fatal = patch_log_method_to_kwargs_only(logger.fatal) # type: ignore
98+
logger.log = patch_log_with_level_method_to_kwargs_only(logger.log) # type: ignore
9799

98100
logger.makeRecord = patch_make_records_to_use_kwarg_string_interpolation(logger.makeRecord) # type: ignore
99101

haystack/tracing/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
from .opentelemetry import OpenTelemetryTracer
2-
from .tracer import Span, Tracer, auto_enable_tracing, disable_tracing, enable_tracing, is_tracing_enabled, tracer
1+
from haystack.tracing.tracer import ( # noqa: I001 (otherwise we end up with partial imports)
2+
Span,
3+
Tracer,
4+
auto_enable_tracing,
5+
disable_tracing,
6+
enable_tracing,
7+
is_tracing_enabled,
8+
tracer,
9+
)
10+
from haystack.tracing.opentelemetry import OpenTelemetryTracer

haystack/tracing/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def coerce_tag_value(value: Any) -> Union[bool, str, int, float]:
2828
serializable = _serializable_value(value)
2929
return json.dumps(serializable)
3030
except Exception as error:
31-
logger.debug("Failed to coerce tag value to string: {error}", error=error, exc_info=True)
31+
logger.debug("Failed to coerce tag value to string: {error}", error=error)
3232

3333
# Our last resort is to convert the value to a string
3434
return str(value)

0 commit comments

Comments
 (0)