diff --git a/CHANGELOG.md b/CHANGELOG.md index cdbbc45b..977556f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,11 @@ 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. + ## [24.4.0](https://github.com/hynek/structlog/compare/24.3.0...24.4.0) - 2024-07-17 diff --git a/pyproject.toml b/pyproject.toml index d9a3cc92..4e0c2a6d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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/" diff --git a/src/structlog/threadlocal.py b/src/structlog/threadlocal.py index c9e412e4..7c827c2e 100644 --- a/src/structlog/threadlocal.py +++ b/src/structlog/threadlocal.py @@ -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) diff --git a/src/structlog/typing.py b/src/structlog/typing.py index 8a07d60a..222e0496 100644 --- a/src/structlog/typing.py +++ b/src/structlog/typing.py @@ -14,6 +14,8 @@ from __future__ import annotations +import sys + from types import TracebackType from typing import ( Any, @@ -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 @@ -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):