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 BindableLogger return Self on binds #642

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

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

## Changed

- `structlog.typing.BindableLogger` protocol now returns `Self` instead of `BindableLogger`.
This adds a dependency on [*typing-extensions*](https://pypi.org/project/typing-extensions/) for Pythons older than 3.11.

[#642](https://github.com/hynek/structlog/pull/642)


## [24.4.0](https://github.com/hynek/structlog/compare/24.3.0...24.4.0) - 2024-07-17

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ classifiers = [
"Topic :: System :: Logging",
"Typing :: Typed",
]
dependencies = []
dependencies = ["typing-extensions; python_version<'3.11'"]

[project.urls]
Documentation = "https://www.structlog.org/"
Expand Down
2 changes: 1 addition & 1 deletion src/structlog/threadlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def tmp_bind(

saved = as_immutable(logger)._context
try:
yield logger.bind(**tmp_values) # type: ignore[misc]
yield logger.bind(**tmp_values)
finally:
logger._context.clear()
logger._context.update(saved)
Expand Down
16 changes: 12 additions & 4 deletions src/structlog/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from __future__ import annotations

import sys

from types import TracebackType
from typing import (
Any,
Expand All @@ -31,6 +33,12 @@
)


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


WrappedLogger = Any
"""
A logger that is wrapped by a bound logger and is ultimately responsible for
Expand Down Expand Up @@ -130,13 +138,13 @@ class BindableLogger(Protocol):

_context: Context

def bind(self, **new_values: Any) -> BindableLogger: ...
def bind(self, **new_values: Any) -> Self: ...

def unbind(self, *keys: str) -> BindableLogger: ...
def unbind(self, *keys: str) -> Self: ...

def try_unbind(self, *keys: str) -> BindableLogger: ...
def try_unbind(self, *keys: str) -> Self: ...

def new(self, **new_values: Any) -> BindableLogger: ...
def new(self, **new_values: Any) -> Self: ...


class FilteringBoundLogger(BindableLogger, Protocol):
Expand Down