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

Typing: return Self in stdlib.BoundLogger #694

Merged
merged 5 commits into from
Jan 27, 2025
Merged
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
15 changes: 11 additions & 4 deletions src/structlog/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
from functools import partial
from typing import Any, Callable, Collection, Dict, Iterable, Sequence, cast


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


from . import _config
from ._base import BoundLoggerBase
from ._frames import _find_first_app_frame_and_name, _format_stack
Expand Down Expand Up @@ -155,13 +162,13 @@ class BoundLogger(BoundLoggerBase):

_logger: logging.Logger

def bind(self, **new_values: Any) -> BoundLogger:
def bind(self, **new_values: Any) -> Self:
"""
Return a new logger with *new_values* added to the existing ones.
"""
return super().bind(**new_values)

def unbind(self, *keys: str) -> BoundLogger:
def unbind(self, *keys: str) -> Self:
"""
Return a new logger with *keys* removed from the context.

Expand All @@ -170,15 +177,15 @@ def unbind(self, *keys: str) -> BoundLogger:
"""
return super().unbind(*keys)

def try_unbind(self, *keys: str) -> BoundLogger:
def try_unbind(self, *keys: str) -> Self:
"""
Like :meth:`unbind`, but best effort: missing keys are ignored.

.. versionadded:: 18.2.0
"""
return super().try_unbind(*keys)

def new(self, **new_values: Any) -> BoundLogger:
def new(self, **new_values: Any) -> Self:
"""
Clear context and binds *initial_values* using `bind`.

Expand Down
Loading