|
10 | 10 | import logging |
11 | 11 | import re |
12 | 12 | import sys |
13 | | -from typing import Optional, Union, List, Dict, Container, Any, Sequence |
14 | | - |
15 | | -if sys.version_info >= (3, 10): |
16 | | - from typing import TypeAlias |
17 | | -else: |
18 | | - from typing_extensions import TypeAlias |
| 13 | +from typing import TypeAlias, Any |
| 14 | +from collections.abc import Container, Sequence |
19 | 15 |
|
20 | 16 | ## Installed |
21 | 17 |
|
|
24 | 20 |
|
25 | 21 | ### CONSTANTS |
26 | 22 | ### ============================================================================ |
27 | | -RESERVED_ATTRS: List[str] = [ |
| 23 | +RESERVED_ATTRS: list[str] = [ |
28 | 24 | "args", |
29 | 25 | "asctime", |
30 | 26 | "created", |
|
71 | 67 |
|
72 | 68 | ## Type Aliases |
73 | 69 | ## ----------------------------------------------------------------------------- |
74 | | -LogData: TypeAlias = Dict[str, Any] |
| 70 | +LogData: TypeAlias = dict[str, Any] |
75 | 71 | """Type alias |
76 | 72 |
|
77 | 73 | *Changed in 4.0*: renamed from `LogRecord` to `LogData` |
|
82 | 78 | ### ============================================================================ |
83 | 79 | def merge_record_extra( |
84 | 80 | record: logging.LogRecord, |
85 | | - target: Dict, |
| 81 | + target: dict[Any, Any], |
86 | 82 | reserved: Container[str], |
87 | | - rename_fields: Optional[Dict[str, str]] = None, |
88 | | -) -> Dict: |
| 83 | + rename_fields: dict[str, str] | None = None, |
| 84 | +) -> dict[Any, Any]: |
89 | 85 | """ |
90 | 86 | Merges extra attributes from LogRecord object into target dictionary |
91 | 87 |
|
@@ -121,25 +117,25 @@ class BaseJsonFormatter(logging.Formatter): |
121 | 117 | *Added in 3.3*: `exc_info_as_array` and `stack_info_as_array` options are added. |
122 | 118 | """ |
123 | 119 |
|
124 | | - _style: Union[logging.PercentStyle, str] # type: ignore[assignment] |
| 120 | + _style: logging.PercentStyle | str # type: ignore[assignment] |
125 | 121 |
|
126 | 122 | ## Parent Methods |
127 | 123 | ## ------------------------------------------------------------------------- |
128 | 124 | # pylint: disable=too-many-arguments,super-init-not-called |
129 | 125 | def __init__( |
130 | 126 | self, |
131 | | - fmt: Optional[Union[str, Sequence[str]]] = None, |
132 | | - datefmt: Optional[str] = None, |
| 127 | + fmt: str | Sequence[str] | None = None, |
| 128 | + datefmt: str | None = None, |
133 | 129 | style: str = "%", |
134 | 130 | validate: bool = True, |
135 | 131 | *, |
136 | 132 | prefix: str = "", |
137 | | - rename_fields: Optional[Dict[str, str]] = None, |
| 133 | + rename_fields: dict[str, str] | None = None, |
138 | 134 | rename_fields_keep_missing: bool = False, |
139 | | - static_fields: Optional[Dict[str, Any]] = None, |
140 | | - reserved_attrs: Optional[Sequence[str]] = None, |
141 | | - timestamp: Union[bool, str] = False, |
142 | | - defaults: Optional[Dict[str, Any]] = None, |
| 135 | + static_fields: dict[str, Any] | None = None, |
| 136 | + reserved_attrs: Sequence[str] | None = None, |
| 137 | + timestamp: bool | str = False, |
| 138 | + defaults: dict[str, Any] | None = None, |
143 | 139 | exc_info_as_array: bool = False, |
144 | 140 | stack_info_as_array: bool = False, |
145 | 141 | ) -> None: |
@@ -243,7 +239,7 @@ def format(self, record: logging.LogRecord) -> str: |
243 | 239 | Args: |
244 | 240 | record: the record to format |
245 | 241 | """ |
246 | | - message_dict: Dict[str, Any] = {} |
| 242 | + message_dict: dict[str, Any] = {} |
247 | 243 | # TODO: logging.LogRecord.msg and logging.LogRecord.message in typeshed |
248 | 244 | # are always type of str. We shouldn't need to override that. |
249 | 245 | if isinstance(record.msg, dict): |
@@ -276,7 +272,7 @@ def format(self, record: logging.LogRecord) -> str: |
276 | 272 |
|
277 | 273 | ## JSON Formatter Specific Methods |
278 | 274 | ## ------------------------------------------------------------------------- |
279 | | - def parse(self) -> List[str]: |
| 275 | + def parse(self) -> list[str]: |
280 | 276 | """Parses format string looking for substitutions |
281 | 277 |
|
282 | 278 | This method is responsible for returning a list of fields (as strings) |
@@ -327,9 +323,9 @@ def serialize_log_record(self, log_data: LogData) -> str: |
327 | 323 |
|
328 | 324 | def add_fields( |
329 | 325 | self, |
330 | | - log_data: Dict[str, Any], |
| 326 | + log_data: dict[str, Any], |
331 | 327 | record: logging.LogRecord, |
332 | | - message_dict: Dict[str, Any], |
| 328 | + message_dict: dict[str, Any], |
333 | 329 | ) -> None: |
334 | 330 | """Extract fields from a LogRecord for logging |
335 | 331 |
|
@@ -402,15 +398,15 @@ def process_log_record(self, log_data: LogData) -> LogData: |
402 | 398 | """ |
403 | 399 | return log_data |
404 | 400 |
|
405 | | - def formatException(self, ei) -> Union[str, list[str]]: # type: ignore |
| 401 | + def formatException(self, ei) -> str | list[str]: # type: ignore[override] |
406 | 402 | """Format and return the specified exception information. |
407 | 403 |
|
408 | 404 | If exc_info_as_array is set to True, This method returns an array of strings. |
409 | 405 | """ |
410 | 406 | exception_info_str = super().formatException(ei) |
411 | 407 | return exception_info_str.splitlines() if self.exc_info_as_array else exception_info_str |
412 | 408 |
|
413 | | - def formatStack(self, stack_info) -> Union[str, list[str]]: # type: ignore |
| 409 | + def formatStack(self, stack_info) -> str | list[str]: # type: ignore[override] |
414 | 410 | """Format and return the specified stack information. |
415 | 411 |
|
416 | 412 | If stack_info_as_array is set to True, This method returns an array of strings. |
|
0 commit comments