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

Improve typing of PropagateHandler and InterceptHandler #971

Merged
merged 1 commit into from
Sep 3, 2023
Merged
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
9 changes: 5 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ Need to propagate `Loguru` messages to standard `logging`?
::

class PropagateHandler(logging.Handler):
def emit(self, record):
def emit(self, record: logging.LogRecord) -> None:
logging.getLogger(record.name).handle(record)

logger.add(PropagateHandler(), format="{message}")
Expand All @@ -416,16 +416,17 @@ Want to intercept standard `logging` messages toward your `Loguru` sinks?
::

class InterceptHandler(logging.Handler):
def emit(self, record):
def emit(self, record: logging.LogRecord) -> None:
# Get corresponding Loguru level if it exists.
level: str | int
Copy link
Contributor

Choose a reason for hiding this comment

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

The typehint can actually be put on the first declaration as it's the first declaration that is being used to determine the type for the same named variable. This works for Mypy, Pyright and Pyre.

        try:
            level: int | str = logger.level(record.levelname).name
        except ValueError:
            level = record.levelno

Copy link
Owner Author

Choose a reason for hiding this comment

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

@quantumpacket Is it considered best practice? I felt it might be confusing for the reader, so I decided to explicitly declare the level types before the assignment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what would consider it best practice or not, it's allowed by all type checkers listed. I guess it's a matter of preference at this point. 🤷‍♂️

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ok, I'll leave it as is then, but thanks for the heads-up.

try:
level = logger.level(record.levelname).name
except ValueError:
level = record.levelno

# Find caller from where originated the logged message.
frame, depth = sys._getframe(6), 6
while frame and frame.f_code.co_filename == logging.__file__:
frame, depth = inspect.currentframe(), 0
while frame and (depth == 0 or frame.f_code.co_filename == logging.__file__):
frame = frame.f_back
depth += 1

Expand Down
6 changes: 3 additions & 3 deletions tests/test_interception.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inspect
import logging
import sys

from loguru import logger

Expand All @@ -15,8 +15,8 @@ def emit(self, record):
level = record.levelno

# Find caller from where originated the logged message.
frame, depth = sys._getframe(6), 6
while frame and frame.f_code.co_filename == logging.__file__:
frame, depth = inspect.currentframe(), 0
while frame and (depth == 0 or frame.f_code.co_filename == logging.__file__):
frame = frame.f_back
depth += 1

Expand Down