Skip to content

Commit 6fb6e7a

Browse files
committed
Add annotations future import to src/* for consistency
1 parent 414475d commit 6fb6e7a

17 files changed

+120
-98
lines changed

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@
5454

5555
nitpick_ignore = [
5656
("py:class", "Context"),
57+
("py:class", "EventDict"),
5758
("py:class", "ILogObserver"),
5859
("py:class", "PlainFileObserver"),
5960
("py:class", "Processor"),
61+
("py:class", "Styles"),
6062
("py:class", "WrappedLogger"),
61-
("py:class", "structlog.dev._Styles"),
6263
("py:class", "structlog.threadlocal.TLLogger"),
6364
("py:class", "structlog.types.EventDict"),
6465
]

src/structlog/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Structured Logging for Python
88
"""
99

10+
from __future__ import annotations
11+
1012
from structlog import (
1113
contextvars,
1214
dev,

src/structlog/_base.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
Logger wrapper and helper class.
88
"""
99

10-
from typing import Any, Dict, Iterable, Mapping, Optional, Sequence, Tuple
10+
from __future__ import annotations
11+
12+
from typing import Any, Iterable, Mapping, Sequence
1113

1214
from structlog.exceptions import DropEvent
1315

@@ -65,7 +67,7 @@ def __eq__(self, other: Any) -> bool:
6567
def __ne__(self, other: Any) -> bool:
6668
return not self.__eq__(other)
6769

68-
def bind(self, **new_values: Any) -> "BoundLoggerBase":
70+
def bind(self, **new_values: Any) -> BoundLoggerBase:
6971
"""
7072
Return a new logger with *new_values* added to the existing ones.
7173
"""
@@ -75,7 +77,7 @@ def bind(self, **new_values: Any) -> "BoundLoggerBase":
7577
self._context.__class__(self._context, **new_values),
7678
)
7779

78-
def unbind(self, *keys: str) -> "BoundLoggerBase":
80+
def unbind(self, *keys: str) -> BoundLoggerBase:
7981
"""
8082
Return a new logger with *keys* removed from the context.
8183
@@ -87,7 +89,7 @@ def unbind(self, *keys: str) -> "BoundLoggerBase":
8789

8890
return bl
8991

90-
def try_unbind(self, *keys: str) -> "BoundLoggerBase":
92+
def try_unbind(self, *keys: str) -> BoundLoggerBase:
9193
"""
9294
Like :meth:`unbind`, but best effort: missing keys are ignored.
9395
@@ -99,7 +101,7 @@ def try_unbind(self, *keys: str) -> "BoundLoggerBase":
99101

100102
return bl
101103

102-
def new(self, **new_values: Any) -> "BoundLoggerBase":
104+
def new(self, **new_values: Any) -> BoundLoggerBase:
103105
"""
104106
Clear context and binds *initial_values* using `bind`.
105107
@@ -114,8 +116,8 @@ def new(self, **new_values: Any) -> "BoundLoggerBase":
114116
# Helper methods for sub-classing concrete BoundLoggers.
115117

116118
def _process_event(
117-
self, method_name: str, event: Optional[str], event_kw: Dict[str, Any]
118-
) -> Tuple[Sequence[Any], Mapping[str, Any]]:
119+
self, method_name: str, event: str | None, event_kw: dict[str, Any]
120+
) -> tuple[Sequence[Any], Mapping[str, Any]]:
119121
"""
120122
Combines creates an ``event_dict`` and runs the chain.
121123
@@ -175,7 +177,7 @@ def _process_event(
175177
)
176178

177179
def _proxy_to_logger(
178-
self, method_name: str, event: Optional[str] = None, **event_kw: Any
180+
self, method_name: str, event: str | None = None, **event_kw: Any
179181
) -> Any:
180182
"""
181183
Run processor chain on event & call *method_name* on wrapped logger.

src/structlog/_config.py

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@
77
Global state department. Don't reload this module or everything breaks.
88
"""
99

10+
from __future__ import annotations
1011

1112
import sys
1213
import warnings
1314

14-
from typing import (
15-
Any,
16-
Callable,
17-
Dict,
18-
Iterable,
19-
Optional,
20-
Sequence,
21-
Type,
22-
cast,
23-
)
15+
from typing import Any, Callable, Iterable, Sequence, Type, cast
2416

2517
from ._log_levels import make_filtering_bound_logger
2618
from ._loggers import PrintLoggerFactory
@@ -62,7 +54,7 @@ class _Configuration:
6254

6355
is_configured: bool = False
6456
default_processors: Iterable[Processor] = _BUILTIN_DEFAULT_PROCESSORS[:]
65-
default_context_class: Type[Context] = _BUILTIN_DEFAULT_CONTEXT_CLASS
57+
default_context_class: type[Context] = _BUILTIN_DEFAULT_CONTEXT_CLASS
6658
default_wrapper_class: Any = _BUILTIN_DEFAULT_WRAPPER_CLASS
6759
logger_factory: Callable[
6860
..., WrappedLogger
@@ -87,7 +79,7 @@ def is_configured() -> bool:
8779
return _CONFIG.is_configured
8880

8981

90-
def get_config() -> Dict[str, Any]:
82+
def get_config() -> dict[str, Any]:
9183
"""
9284
Get a dictionary with the current configuration.
9385
@@ -146,12 +138,12 @@ def get_logger(*args: Any, **initial_values: Any) -> Any:
146138

147139
def wrap_logger(
148140
logger: WrappedLogger,
149-
processors: Optional[Iterable[Processor]] = None,
150-
wrapper_class: Optional[Type[BindableLogger]] = None,
151-
context_class: Optional[Type[Context]] = None,
152-
cache_logger_on_first_use: Optional[bool] = None,
153-
logger_factory_args: Optional[Iterable[Any]] = None,
154-
**initial_values: Any
141+
processors: Iterable[Processor] | None = None,
142+
wrapper_class: type[BindableLogger] | None = None,
143+
context_class: type[Context] | None = None,
144+
cache_logger_on_first_use: bool | None = None,
145+
logger_factory_args: Iterable[Any] | None = None,
146+
**initial_values: Any,
155147
) -> Any:
156148
"""
157149
Create a new bound logger for an arbitrary *logger*.
@@ -189,11 +181,11 @@ def wrap_logger(
189181

190182

191183
def configure(
192-
processors: Optional[Iterable[Processor]] = None,
193-
wrapper_class: Optional[Type[BindableLogger]] = None,
194-
context_class: Optional[Type[Context]] = None,
195-
logger_factory: Optional[Callable[..., WrappedLogger]] = None,
196-
cache_logger_on_first_use: Optional[bool] = None,
184+
processors: Iterable[Processor] | None = None,
185+
wrapper_class: type[BindableLogger] | None = None,
186+
context_class: type[Context] | None = None,
187+
logger_factory: Callable[..., WrappedLogger] | None = None,
188+
cache_logger_on_first_use: bool | None = None,
197189
) -> None:
198190
"""
199191
Configures the **global** defaults.
@@ -239,11 +231,11 @@ def configure(
239231

240232

241233
def configure_once(
242-
processors: Optional[Iterable[Processor]] = None,
243-
wrapper_class: Optional[Type[BindableLogger]] = None,
244-
context_class: Optional[Type[Context]] = None,
245-
logger_factory: Optional[Callable[..., WrappedLogger]] = None,
246-
cache_logger_on_first_use: Optional[bool] = None,
234+
processors: Iterable[Processor] | None = None,
235+
wrapper_class: type[BindableLogger] | None = None,
236+
context_class: type[Context] | None = None,
237+
logger_factory: Callable[..., WrappedLogger] | None = None,
238+
cache_logger_on_first_use: bool | None = None,
247239
) -> None:
248240
"""
249241
Configures if structlog isn't configured yet.
@@ -298,11 +290,11 @@ class BoundLoggerLazyProxy:
298290
def __init__(
299291
self,
300292
logger: WrappedLogger,
301-
wrapper_class: Optional[Type[BindableLogger]] = None,
302-
processors: Optional[Iterable[Processor]] = None,
303-
context_class: Optional[Type[Context]] = None,
304-
cache_logger_on_first_use: Optional[bool] = None,
305-
initial_values: Optional[Dict[str, Any]] = None,
293+
wrapper_class: type[BindableLogger] | None = None,
294+
processors: Iterable[Processor] | None = None,
295+
context_class: type[Context] | None = None,
296+
cache_logger_on_first_use: bool | None = None,
297+
initial_values: dict[str, Any] | None = None,
306298
logger_factory_args: Any = None,
307299
) -> None:
308300
self._logger = logger
@@ -398,13 +390,13 @@ def __getattr__(self, name: str) -> Any:
398390

399391
return getattr(bl, name)
400392

401-
def __getstate__(self) -> Dict[str, Any]:
393+
def __getstate__(self) -> dict[str, Any]:
402394
"""
403395
Our __getattr__ magic makes this necessary.
404396
"""
405397
return self.__dict__
406398

407-
def __setstate__(self, state: Dict[str, Any]) -> None:
399+
def __setstate__(self, state: dict[str, Any]) -> None:
408400
"""
409401
Our __getattr__ magic makes this necessary.
410402
"""

src/structlog/_frames.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
# 2.0, and the MIT License. See the LICENSE file in the root of this
44
# repository for complete details.
55

6+
from __future__ import annotations
7+
68
import sys
79
import traceback
810

911
from io import StringIO
1012
from types import FrameType
11-
from typing import List, Optional, Tuple
1213

1314
from .types import ExcInfo
1415

@@ -31,8 +32,8 @@ def _format_exception(exc_info: ExcInfo) -> str:
3132

3233

3334
def _find_first_app_frame_and_name(
34-
additional_ignores: Optional[List[str]] = None,
35-
) -> Tuple[FrameType, str]:
35+
additional_ignores: list[str] | None = None,
36+
) -> tuple[FrameType, str]:
3637
"""
3738
Remove all intra-structlog calls and return the relevant app frame.
3839

src/structlog/_generic.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
Generic bound logger that can wrap anything.
88
"""
99

10+
from __future__ import annotations
11+
1012
from functools import partial
11-
from typing import Any, Dict
13+
from typing import Any
1214

1315
from structlog._base import BoundLoggerBase
1416

@@ -38,13 +40,13 @@ def __getattr__(self, method_name: str) -> Any:
3840

3941
return wrapped
4042

41-
def __getstate__(self) -> Dict[str, Any]:
43+
def __getstate__(self) -> dict[str, Any]:
4244
"""
4345
Our __getattr__ magic makes this necessary.
4446
"""
4547
return self.__dict__
4648

47-
def __setstate__(self, state: Dict[str, Any]) -> None:
49+
def __setstate__(self, state: dict[str, Any]) -> None:
4850
"""
4951
Our __getattr__ magic makes this necessary.
5052
"""

src/structlog/_greenlets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
Fails to import if not running under greenlet.
1010
"""
1111

12+
from __future__ import annotations
13+
1214
from typing import Any
1315
from weakref import WeakKeyDictionary
1416

src/structlog/_log_levels.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
Extracted log level data used by both stdlib and native log level filters.
88
"""
99

10+
from __future__ import annotations
11+
1012
import logging
1113

12-
from typing import Any, Callable, Dict, Type
14+
from typing import Any, Callable
1315

1416
from ._base import BoundLoggerBase
1517
from .types import EventDict, FilteringBoundLogger
@@ -77,7 +79,7 @@ def exception(self: FilteringBoundLogger, event: str, **kw: Any) -> Any:
7779
return self.error(event, **kw)
7880

7981

80-
def make_filtering_bound_logger(min_level: int) -> Type[FilteringBoundLogger]:
82+
def make_filtering_bound_logger(min_level: int) -> type[FilteringBoundLogger]:
8183
"""
8284
Create a new `FilteringBoundLogger` that only logs *min_level* or higher.
8385
@@ -110,7 +112,7 @@ def make_filtering_bound_logger(min_level: int) -> Type[FilteringBoundLogger]:
110112
return _LEVEL_TO_FILTERING_LOGGER[min_level]
111113

112114

113-
def _make_filtering_bound_logger(min_level: int) -> Type[FilteringBoundLogger]:
115+
def _make_filtering_bound_logger(min_level: int) -> type[FilteringBoundLogger]:
114116
"""
115117
Create a new `FilteringBoundLogger` that only logs *min_level* or higher.
116118
@@ -137,7 +139,7 @@ def log(self: Any, level: int, event: str, **kw: Any) -> Any:
137139
name = _LEVEL_TO_NAME[level]
138140
return self._proxy_to_logger(name, event, **kw)
139141

140-
meths: Dict[str, Callable] = {"log": log}
142+
meths: dict[str, Callable] = {"log": log}
141143
for lvl, name in _LEVEL_TO_NAME.items():
142144
meths[name] = make_method(lvl)
143145

src/structlog/_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Generic utilities.
88
"""
99

10+
from __future__ import annotations
11+
1012
import errno
1113
import sys
1214

src/structlog/contextvars.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
See :doc:`contextvars`.
1616
"""
1717

18+
from __future__ import annotations
19+
1820
import contextlib
1921
import contextvars
2022

21-
from typing import Any, Dict, Generator, Mapping
23+
from typing import Any, Generator, Mapping
2224

2325
import structlog
2426

@@ -31,10 +33,10 @@
3133
# For proper isolation, we have to use a dict of ContextVars instead of a
3234
# single ContextVar with a dict.
3335
# See https://github.com/hynek/structlog/pull/302 for details.
34-
_CONTEXT_VARS: Dict[str, contextvars.ContextVar[Any]] = {}
36+
_CONTEXT_VARS: dict[str, contextvars.ContextVar[Any]] = {}
3537

3638

37-
def get_contextvars() -> Dict[str, Any]:
39+
def get_contextvars() -> dict[str, Any]:
3840
"""
3941
Return a copy of the ``structlog``-specific context-local context.
4042
@@ -50,7 +52,7 @@ def get_contextvars() -> Dict[str, Any]:
5052
return rv
5153

5254

53-
def get_merged_contextvars(bound_logger: BindableLogger) -> Dict[str, Any]:
55+
def get_merged_contextvars(bound_logger: BindableLogger) -> dict[str, Any]:
5456
"""
5557
Return a copy of the current context-local context merged with the context
5658
from *bound_logger*.
@@ -100,7 +102,7 @@ def clear_contextvars() -> None:
100102
k.set(Ellipsis)
101103

102104

103-
def bind_contextvars(**kw: Any) -> "Mapping[str, contextvars.Token[Any]]":
105+
def bind_contextvars(**kw: Any) -> Mapping[str, contextvars.Token[Any]]:
104106
r"""
105107
Put keys and values into the context-local context.
106108
@@ -129,7 +131,7 @@ def bind_contextvars(**kw: Any) -> "Mapping[str, contextvars.Token[Any]]":
129131
return rv
130132

131133

132-
def reset_contextvars(**kw: "contextvars.Token[Any]") -> None:
134+
def reset_contextvars(**kw: contextvars.Token[Any]) -> None:
133135
r"""
134136
Reset contextvars corresponding to the given Tokens.
135137

0 commit comments

Comments
 (0)