Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable alternative to run Kedro without Rich again. #4090

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kedro/io/data_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
generate_timestamp,
)
from kedro.io.memory_dataset import MemoryDataset
from kedro.logging import _format_rich, _has_rich_handler
from kedro.utils import _format_rich, _has_rich_handler

Patterns = Dict[str, Dict[str, Any]]

Expand Down
12 changes: 0 additions & 12 deletions kedro/logging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import sys
from functools import lru_cache
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -55,14 +54,3 @@ def __init__(self, *args: Any, **kwargs: Any):
# fixed on their side at some point, but until then we disable it.
# See https://github.com/Textualize/rich/issues/2455
rich.traceback.install(**traceback_install_kwargs) # type: ignore[arg-type]


@lru_cache(maxsize=None)
def _has_rich_handler(logger: logging.Logger) -> bool:
"""Returns true if the logger has a RichHandler attached."""
return any(isinstance(handler, RichHandler) for handler in logger.handlers)


def _format_rich(value: str, markup: str) -> str:
"""Format string with rich markup"""
return f"[{markup}]{value}[/{markup}]"
17 changes: 17 additions & 0 deletions kedro/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
of kedro package.
"""
import importlib
import logging
import os
from functools import lru_cache
from pathlib import Path
from typing import Any, Union

Expand Down Expand Up @@ -78,3 +80,18 @@ def _find_kedro_project(current_dir: Path) -> Any: # pragma: no cover
if _is_project(parent_dir):
return parent_dir
return None


@lru_cache(maxsize=None)
def _has_rich_handler(logger: logging.Logger) -> bool:
"""Returns true if the logger has a RichHandler attached."""
try:
from rich.logging import RichHandler
Copy link
Contributor

@noklam noklam Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it needed to move it from logging to utils?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was checking if rich is installed at all, because if it isn't, the handler is not going to be there, making the function automatically return false. If it is installed, the user might still not be using it on their logging config, so we move to checking if it's present.

This approach is not working though, I'm verifying it right now.

except ImportError:
return False
return any(isinstance(handler, RichHandler) for handler in logger.handlers)


def _format_rich(value: str, markup: str) -> str:
"""Format string with rich markup"""
return f"[{markup}]{value}[/{markup}]"
17 changes: 13 additions & 4 deletions tests/framework/project/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import importlib
import logging
import sys
from pathlib import Path
from unittest import mock

import pytest
import yaml

from kedro.framework.project import LOGGING, configure_logging, configure_project
from kedro.logging import RichHandler, _format_rich, _has_rich_handler
from kedro.utils import _format_rich, _has_rich_handler


@pytest.fixture
Expand Down Expand Up @@ -156,10 +158,17 @@ def test_rich_format():

def test_has_rich_handler():
test_logger = logging.getLogger("test_logger")
assert not _has_rich_handler(test_logger)
with mock.patch("builtins.__import__", side_effect=ImportError):
assert not _has_rich_handler(test_logger)
_has_rich_handler.cache_clear()
test_logger.addHandler(RichHandler())
assert _has_rich_handler(test_logger)

if importlib.util.find_spec("rich"):
from rich.logging import RichHandler

test_logger.addHandler(RichHandler())
assert _has_rich_handler(test_logger)
else:
assert not _has_rich_handler(test_logger)


def test_default_logging_info_emission(monkeypatch, tmp_path, caplog):
Expand Down