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

Make instances of BoundLoggerLazyProxy BindableLoggers on Python 3.12 #561

Merged
merged 1 commit into from
Oct 16, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/

## [Unreleased](https://github.com/hynek/structlog/compare/23.2.0...HEAD)

### Fixed

- The return value from `get_logger()` (a `BoundLoggerLazyProxy`) now passes `isinstance`-checks against `structlog.typing.BindableLogger` on Python 3.12.



## [23.2.0](https://github.com/hynek/structlog/compare/23.1.0...23.2.0) - 2023-10-09

Expand Down
5 changes: 5 additions & 0 deletions src/structlog/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ class BoundLoggerLazyProxy:
.. versionchanged:: 0.4.0 Added support for *logger_factory_args*.
"""

# fulfill BindableLogger protocol without carrying accidental state
@property
def _context(self) -> dict[str, str]:
return {}

def __init__(
self,
logger: WrappedLogger | None,
Expand Down
2 changes: 1 addition & 1 deletion src/structlog/threadlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def as_immutable(logger: TLLogger) -> TLLogger:
"""
_deprecated()
if isinstance(logger, BoundLoggerLazyProxy):
logger = logger.bind() # type: ignore[assignment]
logger = logger.bind()

try:
ctx = logger._context._tl.dict_.__class__( # type: ignore[union-attr]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get_logger,
wrap_logger,
)
from structlog.typing import BindableLogger


@pytest.fixture(name="proxy")
Expand Down Expand Up @@ -56,6 +57,16 @@ class Foo(metaclass=abc.ABCMeta):
Foo()


def test_lazy_logger_is_an_instance_of_bindable_logger():
"""
The BoundLoggerLazyProxy returned by get_logger fulfills the BindableLogger
protocol.

See https://github.com/hynek/structlog/issues/560
"""
assert isinstance(get_logger(), BindableLogger)


def test_default_context_class():
"""
Default context class is dict.
Expand Down