Skip to content

Commit

Permalink
Typing: return Self in stdlib.BoundLogger (#694)
Browse files Browse the repository at this point in the history
* Typing: use Self in BoundLogger

* Fix missing import

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix Self import for Python < 3.11

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hynek Schlawack <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent 42452dc commit 03b5143
Showing 1 changed file with 11 additions and 4 deletions.
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

0 comments on commit 03b5143

Please sign in to comment.