Skip to content

Commit 16845ed

Browse files
committed
Make BindableLogger return Self on binds
1 parent 485ca19 commit 16845ed

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/
1515

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

18+
## Changed
19+
20+
- `structlog.typing.BindableLogger` protocol now returns `Self` instead of `BindableLogger`.
21+
This adds a dependency on [*typing-extensions*](https://pypi.org/project/typing-extensions/) for Pythons older than 3.11.
22+
23+
[#642](https://github.com/hynek/structlog/pull/642)
24+
1825

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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ classifiers = [
2626
"Topic :: System :: Logging",
2727
"Typing :: Typed",
2828
]
29-
dependencies = []
29+
dependencies = ["typing-extensions; python_version<'3.11'"]
3030

3131
[project.urls]
3232
Documentation = "https://www.structlog.org/"

src/structlog/threadlocal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def tmp_bind(
152152

153153
saved = as_immutable(logger)._context
154154
try:
155-
yield logger.bind(**tmp_values) # type: ignore[misc]
155+
yield logger.bind(**tmp_values)
156156
finally:
157157
logger._context.clear()
158158
logger._context.update(saved)

src/structlog/typing.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
from __future__ import annotations
1616

17+
import sys
18+
1719
from types import TracebackType
1820
from typing import (
1921
Any,
@@ -31,6 +33,12 @@
3133
)
3234

3335

36+
if sys.version_info >= (3, 11):
37+
from typing import Self
38+
else:
39+
from typing_extensions import Self
40+
41+
3442
WrappedLogger = Any
3543
"""
3644
A logger that is wrapped by a bound logger and is ultimately responsible for
@@ -130,13 +138,13 @@ class BindableLogger(Protocol):
130138

131139
_context: Context
132140

133-
def bind(self, **new_values: Any) -> BindableLogger: ...
141+
def bind(self, **new_values: Any) -> Self: ...
134142

135-
def unbind(self, *keys: str) -> BindableLogger: ...
143+
def unbind(self, *keys: str) -> Self: ...
136144

137-
def try_unbind(self, *keys: str) -> BindableLogger: ...
145+
def try_unbind(self, *keys: str) -> Self: ...
138146

139-
def new(self, **new_values: Any) -> BindableLogger: ...
147+
def new(self, **new_values: Any) -> Self: ...
140148

141149

142150
class FilteringBoundLogger(BindableLogger, Protocol):

0 commit comments

Comments
 (0)