Skip to content

Commit

Permalink
Make BindableLogger return Self on binds
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Aug 25, 2024
1 parent 5618064 commit 5f51b06
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
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

0 comments on commit 5f51b06

Please sign in to comment.